You are on page 1of 5

Ing.

Civil

METODOS NUMÉRICOS
LAB 8

Apellidos Nombres Grupo

1. Escriba los comandos Matlab que permitan hallar la integral mostrando su resultado.
3𝜋/4
∫ cos⁡(√𝑥 𝑑𝑥
𝜋/4

2. Halle el área de una región limitada por dos curvas. Una de las curvas obtenida por
interpolación numérica (spline), y la otra curva, sea una función definida explícitamente
mediante una expresión matemática.

1. Programa
function A=trap(f,a,b,n)
A=0;
h=(b-a)/n;
for p=a:h:b-h
Q=(f(p)+f(p+h))*h/2;
A=A+Q;
End

function A=romberg(f,a,b,e)
R(1,1)=trap(f,a,b,1);
R(2,1)=trap(f,a,b,2);
R(2,2)=(4*R(2,1)-R(1,1))/3;
k=2;
while abs(R(k,k)-R(k,k-1))>=e
k=k+1;
R(k,1)=trap(f,a,b,2^(k-1));
for j=2:k;
R(k,j)=((4^(j-1)*R(k,j-1)-R(k-1,j-1))/(4^(j-1)-1));
end
end
A=R(k,k);
end

comandos
>> f=@(x) cos(x^0.5);
>> romberg(f,pi/4,3*pi/4,0.00001)

ans =

0.5018

>> x=0:0.25:1

x=

0 0.2500 0.5000 0.7500 1.0000


Ing. Civil

>> y=x^0.5
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

>> y=x.^0.5

y=

0 0.5000 0.7071 0.8660 1.0000

>> j=0:0.01:1;
>> t=interp1(x,y,j,'spline');
>> q=@(w) w^2;
>> h=@(q,t) q-t

h=

@(q,t)q-t

>> romberg(h,0,1,0.0001
romberg(h,0,1,0.0001
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

Did you mean:


>> romberg(h,0,1,0.0001)
Error using @(q,t)q-t
Not enough input arguments.

Error in trap (line 5)


Q=(f(p)+f(p+h))*h/2;

Error in romberg (line 2)


R(1,1)=trap(f,a,b,1);

>> romberg(q,0,1,0.0001)

ans =

0.3333

>> romberg(t,0,1,0.0001)
Attempted to access f(0); index must be a positive integer or logical.

Error in trap (line 5)


Q=(f(p)+f(p+h))*h/2;
Ing. Civil

Error in romberg (line 2)


R(1,1)=trap(f,a,b,1);

>> romberg(t,0,1,0.0001)
Attempted to access f(0); index must be a positive integer or logical.

Error in trap (line 5)


Q=(f(p)+f(p+h)).*h/2;

Error in romberg (line 2)


R(1,1)=trap(f,a,b,1);

>> romberg(t,0,1,0.0001)
Attempted to access f(0); index must be a positive integer or logical.

Error in trap (line 5)


Q=(f(p)+f(p+h)).*h./2;

Error in romberg (line 2)


R(1,1)=trap(f,a,b,1);

>> y=x^0.5;
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

>> y=x.^0.5;
>>
>>
>>
>> y

y=

0 0.5000 0.7071 0.8660 1.0000

>> z=interp1(x,y,j,'spline')

z=

Columns 1 through 13

0 0.0294 0.0578 0.0854 0.1121 0.1378 0.1628 0.1869 0.2101


0.2326 0.2544 0.2753 0.2955

Columns 14 through 26
Ing. Civil

0.3150 0.3338 0.3520 0.3694 0.3862 0.4024 0.4180 0.4330 0.4475


0.4614 0.4748 0.4876 0.5000

Columns 27 through 39

0.5119 0.5234 0.5344 0.5450 0.5553 0.5651 0.5746 0.5838 0.5926


0.6012 0.6095 0.6175 0.6253

Columns 40 through 52

0.6328 0.6402 0.6474 0.6544 0.6613 0.6681 0.6747 0.6813 0.6878


0.6942 0.7007 0.7071 0.7135

Columns 53 through 65

0.7200 0.7264 0.7329 0.7393 0.7458 0.7523 0.7587 0.7652 0.7716


0.7780 0.7845 0.7909 0.7973

Columns 66 through 78

0.8037 0.8100 0.8163 0.8227 0.8289 0.8352 0.8414 0.8476 0.8538


0.8599 0.8660 0.8721 0.8781

Columns 79 through 91

0.8841 0.8900 0.8958 0.9017 0.9074 0.9131 0.9188 0.9244 0.9299


0.9354 0.9408 0.9462 0.9515

Columns 92 through 101

0.9567 0.9618 0.9669 0.9718 0.9767 0.9816 0.9863 0.9910 0.9955


1.0000

>> romberg(z,0,1,0.0001)
Attempted to access f(0); index must be a positive integer or logical.

Error in trap (line 5)


Q=(f(p)+f(p+h)).*h./2;

Error in romberg (line 2)


R(1,1)=trap(f,a,b,1);

>> h=@(z) z

h=

@(z)z
Ing. Civil

>> romberg(z,0,1,0.00001)
Attempted to access f(0); index must be a positive integer or logical.

Error in trap (line 5)


Q=(f(p)+f(p+h)).*h./2;

Error in romberg (line 2)


R(1,1)=trap(f,a,b,1);

>> romberg(h,0,1,0.00001)

ans =

0.5000

You might also like