Vous êtes sur la page 1sur 14

Integración numérica

Regla del trapecio:

Y=P(x): polinomio de lagrange


(𝑥 − 𝑥𝑖) 𝑥 − 𝑥0
𝑃(𝑥) = 𝑓(𝑥𝑜) ∗ + 𝑓(𝑥𝑖) ∗
(𝑥𝑜 − 𝑥𝑖) 𝑥𝑖 − 𝑥0
𝑏 𝑥𝑖 𝑥
𝑥 − 𝑥𝑖 𝑥 − 𝑥0
∫ 𝑓(𝑥)𝑑𝑥 ≈ ∫ 𝑝(𝑥)𝑑𝑥 = ∫ [𝑓(𝑥0) + 𝑓(𝑥𝑖) ] 𝑑𝑥
𝑎 𝑥𝑜 𝑥0 𝑥𝑜 − 𝑥𝑖 𝑥𝑖 − 𝑥0

Área del trapecio


𝑏
ℎ 𝑥1 − 𝑥0
∫ 𝑓(𝑥)𝑑𝑥 ≈ [𝑓(𝑥0) + 𝑓(𝑥𝑖)]; 𝑑𝑜𝑛𝑑𝑒 ℎ =
𝑎 2 2

Error del método de lagrange

(x0,f(x0)),(x1,f(x1)),…,(xn,f(xn))
1
𝑓(𝑥) = 𝑃(𝑥) + ∗ 𝑓 𝑛+1 (𝛿(𝑥))(𝑥 − 𝑥0)(𝑥 − 𝑥1) … (𝑥 − 𝑥𝑛)
(𝑛 + 1)!
𝛿(𝑥) 𝜖 [𝑥0, 𝑥𝑛]

Teorema 1: método del trapecio


𝑏
ℎ ℎ3
∫ 𝑓(𝑥)𝑑𝑥 = [𝑓(𝑥0) + 𝑓(𝑥1)] − ∗ 𝑓′′(𝛿)
𝑎 2 3

x i=a+h(i)
x0=a
x1=a+h
x2=a+2h
i=0
i=1
h=(b-a)/n

𝑏
ℎ ℎ ℎ
∫ 𝑓(𝑥)𝑑𝑥 ≈ [𝑓(𝑥0) + 𝑓(𝑥1)] + [𝑓(𝑥1) + 𝑓(𝑥2)] … [𝑓(𝑥𝑛 − 1) + 𝑓(𝑥𝑛)]
𝑎 2 2 2
𝑏 𝑛−1

∫ 𝑓(𝑥)𝑑𝑥 ≈ [𝑓(𝑥0) + 𝑓(𝑥𝑛) + 2 ∑ 𝑓(𝑥𝑖)]
𝑎 2
𝑖=1

Teorema 2: regla compuesta del trapecio

𝑏 𝑛−1
ℎ 𝑏−𝑎
∫ 𝑓(𝑥)𝑑𝑥 ≈ [𝑓(𝑥0) + 𝑓(𝑥𝑛) + 2 ∑ 𝑓(𝑥𝑖)], ∗ ℎ2 (𝑓′′(𝑢))
𝑎 2 𝑛
𝑖=1

Xi=a+hi h=(b-a)/n ;n=numero de intervalos

Ejemplo 1: usando el método del trapecio compuesto, hallar la integral

Manualmente:

En programa matlab:

function I=trapeciocompuesto(f,a,b,n)
h=(b-a)/n;
s=0;
for i=1:n-1
s=s+f(a+i*h);
end
I=h*(f(a)+f(b)+2*s)/2;
end
>> f=@(x) sqrt(x).*exp(-x);

>> trapeciocompuesto(f,0,2,8)

ans =0.286

Ejemplo 2: usando el método resicivo trapecio hallar la integral


2
∫0 √𝑥 ∗ 𝑒 −𝑥 𝑑𝑥 ; 𝑛 = 8 falta el programa trapeciorecursivo

Algoritmo de romberg
𝑏
∫ 𝑓(𝑥)𝑑𝑥 = 𝑇(𝑓, ℎ) + 𝑒𝑟𝑟𝑜𝑟
𝑎


Trapecio compuesto 𝑇(𝑓, ℎ) = ℎ ∑𝑛−1
𝑖=0 𝑓(𝑥𝑖) + 2 [𝑓(𝑥0) + 𝑓(𝑥𝑛)]

Si envés de h tomamos,2^n
𝑛−1 ℎ X0=a xn=b
𝑅(𝑛, 0) = ℎ ∑ 𝑓(𝑎 + 𝑖ℎ) + 2 [𝑓(𝑥0) + 𝑓(𝑥𝑛)]
𝑖=1

(𝑛−1)−1
𝑓(𝑎 + 2𝑖ℎ) + [𝑓(𝑎) + 𝑓(𝑏)]
𝑅(𝑛 − 1,0) = ℎ ∑
𝑖=1

H=(a-b)/2^n h=(b-a)/(2^((n-1)+1)=(b-a)/(2^(n-1)*2)
𝑅
2h=(b-a)/2^(n-1)  𝑅(𝑛, 0) − (𝑛 − 1,0) = ℎ ∑𝑘=1 𝑓(0 + (2𝑘 − 1)𝑛
2

2𝑛 −1
𝑅
𝑅(𝑛, 0) = (𝑛 − 1,0) + ℎ ∑ 𝑓(𝑎 + (2𝑘 − 1)𝑛)
2
𝑘=1

n R(n,o) T(f,(b-a)/2^n)
0 R(0,0) n=2^0=1
1 R(1,0) n=2^1=2 R=(1,1)
2 R(2,0) n=2^2=4 R=(2,1) R=(2,2)
3 R(3,0) n=2^3=8 R=(3,1) R=(3,2) R=(3,3)
4 R(4,0) n=2^4=16 R=(4,1) R=(4,2) R=(3,4) R=(4,4)
2
Ejemplo 1: usando el método de romberg hallar la integral ∫0 ln(√𝑥 + 1)𝑑𝑥 , desarrollando 3 filas
en la tabla

function I=romberg(f,a,b,n)
R=zeros(n,n);
for j=1:n
for i=j:n
if j==1
R(i,j)=trapeciocompuesto(f,a,b,2^(i-1))
else R(i,j)=(4^(j-1)*R(i,j-1)-R(i-1,j-1))/(4^(j-1)-1);
end
end
end
disp(R);
end
>> f=@(x) log(sqrt(x)+1);

>> romberg(f,0,2,3)
R=
0.8814 0 0
0 0 0
0 0 0
R=
0.8814 0 0
1.1338 0 0
0 0 0
R=
0.8814 0 0
1.1338 0 0
1.2341 0 0

0.8814 0 0
1.1338 1.2180 0
1.2341 1.2676 1.2709
Ejemplo 2:definir el archivo.m y luego escribir las ordenes en matlab que permitan
3
∫1 √𝑥 + 1 ∗ 𝑓(𝑥)𝑑(𝑥)donde f(x)es el polinomio que interpola en los puntos

x 1 1.5 2 3.2
y 3 5 2 7 >> g=@(x) g(x);
>> I=romberg(g,1,3,5)
R=
function I=romberg(f,a,b,n) 3.9477 0 0 0 0
R=zeros(n,n); 0 0 0 0 0
for j=1:n 0 0 0 0 0
for i=j:n 0 0 0 0 0
if j==1 0 0 0 0 0
R=
R(i,j)=trapeciocompuesto(f,a,b,2^(i-1)) 3.9477 0 0 0 0
else R(i,j)=(4^(j-1)*R(i,j-1)-R(i-1,j- 3.9738 0 0 0 0
1))/(4^(j-1)-1); 0 0 0 0 0
end 0 0 0 0 0
end 0 0 0 0 0
end
R=
disp(R);
3.9477 0 0 0 0
end
3.9738 0 0 0 0
3.4166 0 0 0 0
function w=f(x) 0 0 0 0 0
xx=[1 1.5 2 3.2]; 0 0 0 0 0
yy=[3 5 2 7]; R=
w=interp1(xx,yy,x,'spline'); 3.9477 0 0 0 0
3.9738 0 0 0 0
3.4166 0 0 0 0
function y=g(x) 3.2538 0 0 0 0
y=sqrt(x-1).*f(x); 0 0 0 0 0
R=
3.9477 0 0 0 0
3.9738 0 0 0 0
3.4166 0 0 0 0
3.2538 0 0 0 0
3.2208 0 0 0 0

3.9477 0 0 0 0
3.9738 3.9826 0 0 0
3.4166 3.2308 3.1807 0 0
3.2538 3.1996 3.1975 3.1978 0
3.2208 3.2098 3.2105 3.2107 3.2108

Cuadratura gaussiana
𝑏
∫ 𝑓(𝑥)𝑑𝑥 Quad(f,a,b)
𝑎
2
Ejemplo 1:hallar la siguiente integral ∫0 𝑥 ∗ ln(𝑥 + 1) 𝑑𝑥 usando el método de la cuadratura
gaussiana

>> f=@(x) x.*log(x+1);

>> quad(f,0,2)

ans =

1.6479
Ejemplo 2: definir los archivos.m y luego escribir las ordenes en matlab que permitan calcular la
𝑒
integral ∫0 𝑒 −𝑡 𝑓(𝑡) 𝑑𝑡 donde x=f(t)es el polinomio que interpola con los puntos mostrar el
resultado de la integral con 4 decimales de precisión.
t 0 0.7 1.8 2.8
x -1 4 1 6

function x=integ1(t)
tt=[0 0.7 1.8 2.8];
xx=[-1 4 1 6];
x=interp1(tt,xx,t,'spline');
x=x.*exp(-abs(t));
>> f=@(x) integ1(x);
>> quad(f,0,exp(1))
ans = 2.1965

2
Ejemplo3: escribir los comandos en matlab que permite hallar la integral ∫−1 𝑥𝑃(𝑥)𝑑𝑥 donde P^-1
es el polinomio tipo spline, que interpola los puntos.

x -1 0 1 2
y 4 -1 3 1
function y=integrando(x)
xx=[-1 0 1 2];
yy=[4 -1 3 1];
y=interp1(xx,yy,x,'spline').*x;
>> g=@(x) integrando(x);

>> quad(g,-1,2)

ans = 4.8750

Ejemplo4: definir los archivos.m luego escribir las ordenes en matlab que permitan calcular la
3
integral ∫1 √𝑥 − 1 ∗ 𝑓(𝑥)𝑑𝑥 donde f(x)es el polinomio que interpola los puntos .
function y=integrando(x)
xx=[1 1.5 2 3.2];
yy=[3 5 2 7];
y=interp1(xx,yy,x,'spline').*sqrt(x-1);
>> g=@(x) integrando(x);

>> quad(g,1,3)

ans =3.2192
1
Ejemplo 4’’: ∫0 cos(𝑥 2 ) 𝑑𝑥
>> f=@(x) cos(x.^2);
>> quad(f,0,1)
ans = 0.9045
Ejemplo 5: escribir los archivos.m y luego las ordenes en matlab que permitan calcular, la integral
𝑝𝑖
∫0 𝑔(𝑡)𝑑𝑡 donde g(t) =sen(3t)*f(t) y f(t) es el polinomio que interpola los puntos.

function x=integrando(t)
tt=[0 0.8 1.8 3.4];
xx=[1 5 4 7];
x=interp1(tt,xx,t,'spline').*sin(3.*t);
>> g=@(t) integrando(t);

>> quad(g,0,pi)

ans = 2.1093

1
Ejemplo 6: ∫0 𝑒 −√𝑥 𝑑𝑥

>> f=@(x) exp(-sqrt(x));

>> quad(f,0,1)

ans =

0.5285

Ecuaciones diferencias numéricas

Resolver el problema de valor inicial (PVI)


Dx/dy=f(1,x) variable independiente x
𝑑𝑦
= 𝑓(𝑥, 𝑦) X(0)=x0 variable dependiente y
𝑑𝑥
Sujeto a y(x0)=y0

Método de Euler

Método Taylor de orden 1

H=ti-ti-1
PVI Yi=yi-1+ hf(xi-1;yi-1)=-tx^2

X(0)=x0

H=ti-ti-1 t0=0 [0,i]

Xi=xi-1 +hf(xi-1;xi-1)
Ejemplo1:

function x=MEuler(f,t,x0)
n=length(t);
x(1)=x0;
for i=2:n
h=t(i)-t(i-1);
x(i)=x(i-1)+h*f(t(i-1),x(i-1));
end
end
>> f=@(t,x) -t.*x.^2;

>> t=0:0.1:1;

>> x0=2;

>> MEuler(f,t,x0)

ans =

2.0000 2.0000 1.9600 1.8832 1.7768 1.6505 1.5143 1.3767 1.2440 1.1202 1.0073

Método de Taylor de orden 2


𝑑𝑥 F:R^2 R
= 𝑓(𝑡, 𝑥)
𝑑𝑦
(tx)1 z=f(t,x)
X(t0)=x0 modelacion matemática

′ (𝑡)
ℎ2
𝑥(𝑡 + ℎ) = 𝑥(𝑡) + ℎ𝑥 + ∗ 𝑥 ′′ (𝑡) + 𝜀
2
X’(t)=dx/dt

X’’(t)=d2x/dt2

Si x’(t)=f(t,x) entonces por la regla de la cadena tenemos que

𝜕 2 𝑥 𝜕𝑓 𝜕𝑓 𝜕𝑥 𝜕𝑓 𝜕𝑓 ′
= + ∗ = + (𝑥 (𝑡))
𝜕𝑡 2 𝜕𝑡 𝜕𝑥 𝜕𝑡 𝜕𝑡 𝜕𝑥
Ejemplo1:

function y=taylor2(f,df,xx,y0)
h=xx(2)-xx(1);
n=length(xx);
y(1)=y0;
for k=2:n
y(k)=y(k-1)+h.*f(xx(k-1),y(k-1))+(h.^2./2).*df(xx(k-1),y(k-1));
end

>> f=@(t,x) -t*exp(-t*x);

>> f=@(t,x) t*exp(-t*x);

>> df=@(x,t) exp(-t.*x).*(1-t.*x-t.^2.*f(x,t));

>> xx=[2 2.1 2.2];

>> taylor2(f,df,xx,3)

ans = 3.0000 3.0004 3.0008


Ejemplo2:
Método de rungge-kutta de orden 4

Adaptación del método de Taylor de orden 4

ℎ^2 ℎ3 ℎ4
𝑥(𝑡 + ℎ) = 𝑥(𝑡) + ℎ𝑥 ′ (𝑡) + ∗ 𝑥 ′′ (𝑡) + ∗ 𝑥 ′′′ (𝑡) + 𝑥4(𝑡)
2! 3! 4!
Ejemplo 1: resolver la ecuación diferencial cuando rungge kutta de orden 4
𝑑𝑥
𝑑𝑦
= 𝑥 ∗ 𝑠𝑒𝑛(𝑡) x=x(t) tt=[0.1:0:1:0.5];

function x=ejem1(t);
f=@(t,x) x.*sin(t);
tt=0.1:0.1:0.5;
tx=rk4(f,tt,2);
x=interp1(tt,tx,t,'spline');
>> t=[0.1 0.2 0.3 0.4 0.5];

>> x=ejem1(t)

x=

2.0000 2.0301 2.0809 2.1535 2.2492

𝑑𝑥
Ejemplo2:Dada la ecuación diferencial 𝑑𝑡 𝑥 ∗ 𝑒 −𝑡∗√𝑥 y x(0)=3 escribir los comandos en matlab que
permitan trazar la grafica de g(t)=sen(t+x(t)) en el intervalo [0,2pi] donde x=x(t) es solución de la
ecuación diferencial dada.
function dx=ecua_1(t,x)
dx=x.*exp(-t.*sqrt(x));

function x=solucion(t)
tt=0:0.1:2*pi;
f=@(t,x) ecua_1(t,x);
tx=rk4(f,tt,3);
x=interp1(tt,tx,t,'spline')

>> t=0.1:0.01:0.2;

>> x=solucion(t);

x = 3.2877 3.3150 3.3419 3.3686 3.3950 3.4211 3.4469 3.4723 3.4975


3.5223 3.5468

>> plot(t,x), grid on


𝑝𝑖
Ejemplo3: escribir las ordenes matlab para calcularla integral ∫0 cos(𝑦) 𝑑𝑦 [o,pi] donde y es la
𝑑𝑦
solución de la ecuación diferencial. 𝑑𝑥 = 𝑥 + 𝑦 = 𝑓(𝑥, 𝑦); 𝑦(0) = 3

function dy=ecuc(x,y)
dy=x+y;

function y=solul(x)
tx=0:0.1:pi;
f=@(x,y) ecuc(x,y);
ty=rk4(f,tx,3);
y=interp1(tx,ty,x,'spline');

>> g=@(x) cos(solul(x));

>> quad(g,0,pi)

ans =

-0.1204

3
Ejemplo 4: escribir los comandos que permiten calcular la integral ∫−1(𝑡 − 1)2 ∗ 𝑥(𝑡)𝑑𝑡 donde
𝑑𝑥
x(t)es la solución de la ecuación diferencial 𝑑𝑡 = (𝑡 − 1)2 ∗ 𝑠𝑒𝑛(4 − 𝑥) = 𝑓(𝑡, 𝑥); 𝑥(−1) = 1
donde 1=x0.

function dx=ecua2(t,x)
dx=(t-1).^2.*sin(4-x);

function x=sol2(t)
tt=-1:0.1:3;
f=@(t,x) ecua2(t,x)
xx=rk4(f,tt,1);
x=interp1(tt,xx,t,'spline');

>> g=@(t) (t-1).^2.*sol2(t);

>> quad(g,-1,3)

ans =

13.0138
𝑑𝑥
Eemplo5: dada la ecuación diferencial = √𝑡 ∗ 𝑒 −𝑡𝑥 = 𝑓(𝑡, 𝑥) ; x(0)=pi/3 escribir los comandos
𝑑𝑡
en que permitan trazar la grafica de g(t)=cos(t-x(t)); para el intervalo de [0,2*pi]; donde x=x(t) es
solución de la ecuación diferencial dada.

function dx=ecua2(t,x)
dx=sqrt(t).*exp(-t*x);

function x=sol2(t)
tt=-1:0.1:2*pi;
f=@(t,x) ecua2(t,x)
tx=rk4(f,tt,pi/3);
x=interp1(tt,tx,t,'spline');
x=cos(t-x);
>> t=0:0.01:2*pi;

>> x=sol2(t);

>> plot(t,x), grid on

𝑝𝑖
Ejemplo6: escribir los comandos y arcrivos.m necesarios para calcular ∫−𝑝𝑖 𝑓(𝑡 − 2) ∗ cos(3𝑡) 𝑑𝑡
donde f(t) es el polinomio que interpola los puntos

t -2 -1.5 1.4 2.5 5.3


x 3 -1 2 0 5

function x=fun(t)
tt=[-2 -1.5 1.4 2.5 5.3];
xx=[3 -1 2 0 5];
x=interp1(tt,xx,t,'spline');

>> f=@(t)fun(t-2).*cos(3*t);

>> quad(f,-pi,pi)

ans =

-8.3696
Ejemplo 7: escribir los comandos matlab y archivos .m necesarios para trazar la grafica de la
𝑑𝑥 𝑑𝑥
función ℎ(𝑡) = 𝑑𝑡
,𝑡 ∈ [0,5] donde x=g(t)es la solución de la ecuación diferencial x=x(t) 𝑑𝑡
=𝑡∗
sin(𝑡𝑥 + 1) = 𝑓(𝑡, 𝑥) ;x(0)=3

Derivada central 𝑓 ′ (𝑥) = (𝑓(𝑡 + ℎ) − 𝑓(𝑡 − ℎ))/2ℎ


function dx=ecuacion(t,x)
dx=t.*sin(t*x+1);

function x=solusion(t)
tt=0:0.1:5;
f=@(t,x) ecuacion(t,x);
tx=rk4(f,tt,3);
x=interp1(tt,tx,t,'spline');

function dx=deriv(f,t)
h=0.00000001;
dx=(f(t+h)-f(t-h))./(2*h);

>> t=0:0.1:5;
>> f=@(t) solusion(t);
>> dx=deriv(f,t);
>> plot(t,dx),grid on

Vous aimerez peut-être aussi