You are on page 1of 3

SOLUCIN PARA EDO

f=@(t,y)2./t.*y+t.^2*exp(t);
h=0.05;
a=1; b=2; y0=0;
t=a:h:b;
S=[y0];
N=(b-a)/h;
for i=1:N
y=y0+h*f(t(i),y0);
S=[S;y];
y0=y;
end

t=t
EULER

%f=@(t,y)2./t*y+t.^2*exp(t);
%h=0.05;
%a=1; b=2; y0=0;
function [t,S]=euler1(f,a,b,y0,h)
t=a:h:b;
S=[y0];
N=(b-a)/h;
for i=1:N
y=y0+h*f(t(i),y0);
S=[S;y];
y0=y;
end
t=t';

aplica Euler 1

f=@(t,y)2./t*y+t.^2*exp(t);
h=0.05;
a=1; b=2; y0=0;
[t,S]=euler1(f,a,b,y0,h);
plot(t,S);
grid

Aplica Euler 2
f=@(t,y)2./t*y+t.^2*exp(t);
h=0.005;
a=1; b=2; y0=0;
[t,S]=euler1 (f,a,b,y0,h);
[t1,y]=ode45(f,[a b],y0);
plot(t,S,'b-.',t1,y,'b')
hold on
grid

close all
f=@(t,y)2./t*y+t.^2*exp(t);
h=0.005;
a=1; b=2; y0=0;
[t,S]=euler1(f,a,b,y0,h);
[t1,y]=ode45(f,[a b],y0);
[t1,S2]=ok2(f,a,b,y0,h);
plot(t,S,'b-.',t1,y,'r:',t2,S2,'k:')
legend('euler','ode45','rk2')
grid

rk2
function [t,S]=rk2(f,a,b,y0,h)
t=a:h:b;
S=[y0];
N=(b-a)/h;
for i=1:N
k1=h*f(t(i),y0);
k2=h*f(t(i)+h,y0+k1);
y=y0+0.5*(k1+k2);
S=[S;y];
y0=y;
end
t=t';

rk4
function [t,S]=rk4(f,a,b,y0,h)
t=a:h:b;
S=[y0];
N=(b-a)/h;
for i=1:N
k1=h*f(t(i),y0);
k2=h*f(t(i)+h/2,y0+k1/2);
k3=h*f(t(i)+h/2,y0+k2/2);
k4=h*f(t(i)+h,y0+k3);
y=y0+(1/6)*(k1+2*k2+2*k3+k4);
S=[S;y];
y0=y;
end
t=t';

aplica eluler para todos


f=@(t,y)2./t*y+t.^2*exp(t);
h=0.05;
a=1; b=2; y0=0;
[t,S]=euler1(f,a,b,y0,h);
[t1,y]=ode45(f,[a b],y0);
[t2,S2]=rk2(f,a,b,y0,h);
[t3,S3]=rk4(f,a,b,y0,h);

plot(t,S,'b-.',t1,y,'r:',t2,S2,'k:',t3,S3,'g:')
legend('euler','ode45','rk2','rk4')
grid
TAYLOR
Algoritmo Taylor
function [t,S]=taylor2(f,df,a,b,y0,h)
t=a:h:b;
S=[y0];
N=(b-a)/h;
for i=1:N
yp=f(t(i),y0);
y2p=df(t(i),y0);
y=y0+h*yp+h^2*y2p/2;
S=[S;y];
y0=y;
end
t=t';

APLICA TAYLOR
close all
f=@(t,y)2./t*y+t.^2*exp(t);
df=@(t,y)(-2./t.^2.*y)+(2*t+t.^2)*exp(t)+(2./t).*(2./t*y+t.^2*exp(t));
h=0.1;
a=1; b=2; y0=0;
[t,S]=taylor2(f,df,a,b,y0,h);
[t2,S2]=rk2(f,a,b,y0,h);
[S S2]

You might also like