You are on page 1of 8

%(1)Backward Euler Method

clear
clc
format long
error=1000;
N=16434.087352829107;
%while (error>10^(-1))
N=N
a=0;
b=5;
h=(b-a)/N;
t(1)=a;
y(1)=0;
f=@(t,y) y-0.5*exp(0.5*t)*sin(5*t)+5*exp(0.5*t)*cos(5*t);
fex = @(t) exp(t/2).*sin(5*t);
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
tHat=t(i+1);
yHat=y(i)+h*f(t(i),y(i));
y(i+1)=y(i)+h*f(tHat,yHat);
end
yexact=fex(t);
figure(1)
plot(t,y,'r',t,yexact,'g')
error=(max(abs(y-yexact)))
%end
N = 1.64340873528291e+004
error = 0.09999999999997
%(1)Runge kutta 2 steps method
clear
clc
format long
error=1000
N=18454;
%while (error>10^(-5))
N=N
a=0;
b=5;
h=(b-a)/N;
t(1)=a;
y(1)=0;
f=@(t,y) y-0.5*exp(t/2).*sin(5*t)+5*exp(t/2).*cos(5*t);
fex= @(t) exp(t/2).*sin(5*t);
for i = 1:N
t(i) = a + (i-1)*h;
t(i+1)=a +(i)*h;
that = t(i) + 0.5*h;
yhat = y(i) + 0.5*h*f(t(i),y(i));
y(i+1) = y(i) + h * f(that, yhat);
end
yexact=fex(t);
figure(2)
plot(t,y,t,yexact)
error=(max(abs(y-yexact)))
%end
N =18454
error = 1.00000603919792e-005

%(1)Runge kutta 4 steps method


clear
clc
format long
error=1000
N=150;
while (error>10^(-5))
N=N+1
a=0;
b=5;
h=(b-a)/N;
t(1)=a;
y(1)=0;
f=@(t,y) y-0.5*exp(t/2).*sin(5*t)+5*exp(t/2).*cos(5*t);
fex= @(t) exp(t/2).*sin(5*t);
for i = 1:N
t(i) = a + (i-1)*h;
t(i+1) = a + (i)*h;
K1 = f(t(i),y(i));
K2 = f( t(i)+0.5*h , y(i)+0.5*K1*h );
K3 = f( t(i)+0.5*h , y(i)+0.5*K2*h );
K4 = f( t(i)+h , y(i)+K3*h );
y(i+1) = y(i) +(1/6)* h * (K1 + 2*K2 +2*K3 + K4);
end
yexact=fex(t);
figure(3)
plot(t,y,t,yexact)
error=(max(abs(y-yexact)))
end
N = 150
error = 1.01940694587910e-005
%(2)Backward Euler Method
clear
clc
format long
error=1000;
N=3200;
%while (error>10^(-3))
N=N
a=0;
b=1;
h=(b-a)/N;
t(1)=a;
y(1)=1;
f=@(t,y) t*y+y^(0.5);
fex = @(t) (1/4)*(exp((t.*t)/2).*(sqrt(pi).*erf(t/2)+2)).*(sqrt(pi).*erf(t/2)+2);
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
tHat=t(i+1);
yHat=y(i)+h*f(t(i),y(i));
y(i+1)=y(i)+h*f(tHat,yHat);
end
yexact=fex(t);
figure(1)
plot(t,y,'r',t,yexact,'g')
error=(max(abs(y-yexact)))
%end

N = 3200
error =0.00101985549883
%(2)Runge kutta 2 steps method
clear
clc
format long
error=1000;
N=361;
%while (error>10^(-5))
N=N
a=0;
b=1;
h=(b-a)/N;
t(1)=a;
y(1)=1;
f=@(t,y) t*y+y^(0.5);
fex = @(t) (1/4)*(exp((t.*t)/2).*(sqrt(pi).*erf(t/2)+2)).*(sqrt(pi).*erf(t/2)+2);
for i = 1:N
t(i) = a + (i-1)*h;
t(i+1)=a +(i)*h;
that = t(i) + 0.5*h;
yhat = y(i) + 0.5*h*f(t(i),y(i));
y(i+1) = y(i) + h * f(that, yhat);
end
yexact=fex(t);
figure(1)
plot(t,y,'r',t,yexact,'g')
error=(max(abs(y-yexact)))
figure(1)
plot(t,y,'r',t,yexact,'g')
%end
N = 361
error =1.00193615888422e-005
%(2)Runge kutta 4 steps method
clear
clc
format long
error=1000;
N=232;
%while (error>10^(-5))
N=N+1
a=0;
b=3;
h=(b-a)/N;
t(1)=a;
y(1)=1;
f=@(t,y) t*y+y^(0.5);
fex = @(t) (1/4)*(exp((t.*t)/2).*(sqrt(pi).*erf(t/2)+2)).*(sqrt(pi).*erf(t/2)+2);
for i = 1:N
t(i) = a + (i-1)*h;
t(i+1) = a + (i)*h;
K1 = f(t(i),y(i));
K2 = f( t(i)+0.5*h , y(i)+0.5*K1*h );
K3 = f( t(i)+0.5*h , y(i)+0.5*K2*h );
K4 = f( t(i)+h , y(i)+K3*h );
y(i+1) = y(i) +(1/6)* h * (K1 + 2*K2 +2*K3 + K4);
end

yexact=fex(t);
figure(1)
plot(t,y,'b')
hold on
plot(t,yexact,'r')
error=(max(abs(y-yexact)))
%end
N = 232
error = 1.00147074704182e-005
%(3)Backward Euler
clear
clc
format long
error=1000;
N=1638.0449950281;
%while (error>10^(0))
N=N
a=0;
b=1;
h=(b-a)/N;
y(:,1)=[1;1];
A=[1 2;4 3];
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
tHat=t(i+1);
yHat(:,i)=y(:,i)+h*A*y(:,i);
y(:,i+1)=y(:,i)+h*A*yHat(:,i);
end
figure(1)
plot(t,y(1,:));
hold on
plot(t,y(2,:));
hold on
fx = @(t) (2/3)*exp(5*t)-(1/3)*exp(-t);
fy = @(t) (4/3)*exp(5*t)+(1/3)*exp(-t);
xexact = fx(t);
yexact = fy(t);
plot(t,xexact)
plot(t,yexact)
error = (max(abs(y(1,:)-xexact)))
error2 = (max(abs(y(2,:)-yexact)));
%end
N = 1638.04499502809995
error =1.00000000000041
%(3)Runge kutta 2 steps method
clear
clc
format long
error=1000;
N=39.05;
%while (error>10^(0))
N=N
a=0;
b=1;
h=(b-a)/N;
y(:,1)=[1;1];

A=[1 2;4 3];


for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
tHat = t(i)+0.5*h;
yHat(:,i)=y(:,i)+0.5*h*A*y(:,i);
y(:,i+1)=y(:,i)+h*A*yHat(:,i);
end
figure(1)
plot(t,y(1,:));
hold on
plot(t,y(2,:));
hold on
fx = @(t) (2/3)*exp(5*t)-(1/3)*exp(-t);
fy = @(t) (4/3)*exp(5*t)+(1/3)*exp(-t);
xexact = fx(t);
yexact = fy(t);
plot(t,xexact)
plot(t,yexact)
error = (max(abs(y(1,:)-xexact)))
error2 = (max(abs(y(2,:)-yexact)));
%end
N = 39.05000000000000
error =

0.96604587134566

%(3)Runge kutta 4 steps method


clear
clc
format long
error=1000;
N=6;
%while (error>10^(0))
N=N
a=0;
b=1;
h=(b-a)/N;
y(:,1)=[1;1];
A=[1 2;4 3];
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
K1=A*y(:,i);
K2=A*(0.5*h*K1+y(:,i));
K3=A*(0.5*h*K2+y(:,i));
K4=A*(h*K3+y(:,i));
y(:,i+1) = y(:,i) +(1/6)* h * (K1 + 2*K2 +2*K3 + K4);
end
figure(2)
plot(t,y(1,:),'b');
hold on
plot(t,y(2,:));
hold on
fx = @(t) (2/3)*exp(5*t)-(1/3)*exp(-t);
fy = @(t) (4/3)*exp(5*t)+(1/3)*exp(-t);
xexact = fx(t);
yexact = fy(t);
plot(t,xexact,'y')
plot(t,yexact)
error = (max(abs(y(1,:)-xexact)))

error2 = (max(abs(y(2,:)-yexact)));
%end
N = 6
error =
0.75051265248366
%(4)Backward Euler
clear
clc
format long
error=1000;
N=1180;
%while (error>10^(-1))
N=N
a=0;
b=1;
h=(b-a)/N;
y(:,1) = [-2;1];
A= [0 1; -4 4];
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
tHat=t(i+1);
yHat(:,i)=y(:,i)+h*A*y(:,i);
y(:,i+1)=y(:,i)+h*A*yHat(:,i);
end
figure(1)
plot(t,y(1,:));
hold on
plot(t,y(2,:));
hold on
fx = @(t) -2*exp(2*t)+5*t.*exp(2*t);
fy = @(t) exp(2*t)+10*t.*exp(2*t);
xexact = fx(t);
yexact = fy(t);
plot(t,xexact)
plot(t,yexact)
error = (max(abs(y(1,:)-xexact)))
error2 = (max(abs(y(2,:)-yexact)));
%end
N = 1180
error =
0.10003098939807
%(4)Runge kutta 2 steps method
clear
clc
format long
error=1000;
N=31;
%while (error>10^(-1))
N=N
a=0;
b=1;
h=(b-a)/N;
y(:,1) = [-2;1];
A= [0 1; -4 4];
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
tHat = t(i)+0.5*h;

yHat(:,i)=y(:,i)+0.5*h*A*y(:,i);
y(:,i+1)=y(:,i)+h*A*yHat(:,i);
end
figure(1)
plot(t,y(1,:));
hold on
plot(t,y(2,:));
hold on
fx = @(t) -2*exp(2*t)+5*t.*exp(2*t);
fy = @(t) exp(2*t)+10*t.*exp(2*t);
xexact = fx(t);
yexact = fy(t);
plot(t,xexact)
plot(t,yexact)
error = (max(abs(y(1,:)-xexact)))
error2 = (max(abs(y(2,:)-yexact)));
%end
N = 31
error =
0.10127961664816
%(4)Runge kutta 4 steps method
clear
clc
format long
error=1000;
N=41;
%while (error>10^(-5))
N=N
a=0;
b=1;
h=(b-a)/N;
y(:,1) = [-2;1];
A= [0 1; -4 4];
for i=1:N
t(i)=a+(i-1)*h;
t(i+1)=a+i*h;
K1=A*y(:,i);
K2=A*(0.5*h*K1+y(:,i));
K3=A*(0.5*h*K2+y(:,i));
K4=A*(h*K3+y(:,i));
y(:,i+1) = y(:,i) +(1/6)* h * (K1 + 2*K2 +2*K3 + K4);
end
figure(2)
plot(t,y(1,:),'b');
hold on
plot(t,y(2,:));
hold on
fx = @(t) -2*exp(2*t)+5*t.*exp(2*t);
fy = @(t) exp(2*t)+10*t.*exp(2*t);
xexact = fx(t);
yexact = fy(t);
plot(t,xexact,'y')
plot(t,yexact)
error = (max(abs(y(1,:)-xexact)))
error2 = (max(abs(y(2,:)-yexact)));
%end
N = 41
error = 1.03099479709101e-005

Computer Project 2
Solving Differential Equations Using Euler Method

Esteban D. Perez

Kumnit Nong
MTH 291
Northern Virginia Community College
May 6, 2014

You might also like