Professional Documents
Culture Documents
Matlab Code Abdur Rahim Khan 146 (D) 18pwciv5057
Matlab Code Abdur Rahim Khan 146 (D) 18pwciv5057
1
Question#2
PART(A)
% RK4 Method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over
[a,b]
clear all, close all, clc
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
0 0.5000 0.5000 0
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc
f=@(t,y) (sin(2*t)-(2*t*y))*(t.^-2) %% define the function
a=1; b=2; %% define the domain
h=0.8; %% step size
n=(b-a)/h; %% number of grid points
t=a:h:b; %% grid points in [a,b]
w(1)=0.5;
for j=1:n
k1=h*feval(f,t(j),w(j));
k2=h*feval(f,t(j)+h/2,w(j)+k1/2);
k3=h*feval(f,t(j)+h/2,w(j)+k2/2);
k4=h*feval(f,t(j)+h,w(j)+k3);
w(j+1)=w(j)+(1/6)*(k1+2*k2+2*k3+k4); end
y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given y=y(t); %% compute the exact soltion
over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
f=
@(t,y)(sin(2*t)-(2*t*y))*(t.^-2)
ti, wi, yi, actual-error
E=
Part: C
% RK4 method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over
[a,b]
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer
ti, wi, yi, actual-error
E=
Part: D
% RK4 method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over
[a,b]
clear all, close all, clc
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o')
grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
% Taylor's method of order 2 for initial value problems % solve y'=f(t.y), 0n [0,2] with y(0)=0.5;
clear all, close all, clc, format short
f=@(t,y) (0.2*t*exp(1).^(3*t))-(0.04*exp(1).^(3*t))+(0.04*exp(1).^(-2*t)); %% define f(t,y)
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error
')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o')
grid on legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
0 0.5000 0.5000 0
% Taylor's method of order 2 for initial value problems % solve y'=f(t.y), 0n [0,2] with y(0)=0.5;
clear all, close all, clc, format short
f=@(t,y) t+(1/(1-t)); %% define f(t,y)
d1f= @(t,y) (1+(t-y).^2) ; %% define the derivative if f(t,y) a=1; b=2; %%
define the domain
h=0.5; %% define the step size
n=(b-a)/h; %% define the number of points
t=a:h:b; %% define the grid points
w(1)=0.5; %% define the initial conditions
for j=1:n
w(j+1)=w(j)+h*feval(f,t(j),w(j))+(h.^2/factorial(2))*feval(d1f,t(j),w(j)) ;
end
y=@(
t)
(t+1).
^2-
0.5*e
xp(t);
%%
define
exact
soluti
on if
given
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error
')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o')
grid on legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
Part: C format
long
% Taylor's method of order 2 for initial value problems % solve y'=f(t.y), 0n [0,2] with y(0)=0.5;
clear all, close all, clc, format short
f=@(t,y) t*log(t)+(2*t); %% define f(t,y) d1f= @(t,y) y +
t*y.^1/2 ; %% define the derivative if f(t,y) a=2;
b=3; %% define the domain
h=0.25; %% define the step size
n=(b-a)/h; %% define the number of points
t=a:h:b; %% define the grid points
w(1)=0.5; %% define the initial conditions
for j=1:n
w(j+1)=w(j)+h*feval(f,t(j),w(j))+(h.^2/factorial(2))*feval(d1f,t(j),w(j)) ;
end y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error
')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o')
grid on legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
EXERCISE#5.3
Question#1
Part A:
% Euler's method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b] clear all, close
all, clc f=@(t,y) t*exp(1).^(3*t)-(2*y); %% define f(t,y)
y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given
a=0; b=1; %% define the domain of variable t
h=0.5; %% define the stepsize
n=(b-a)/h; %% define number of grid points over [a,b] t=a:h:b; %% define
the grid points in [a,b]
w(1)=0.5; %% Define the intial cobdition
for j=1:n
w(j+1)=w(j)+h*feval(f,t(j),w(j)); % Euler numerical scheme end
err_estimate=(h*M/2*L)*(exp(L*(t-a))-1);
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error,
error estimate ')
E=[t; w; y; abs(y-w); err_estimate]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error, error estimate
E=
0 0.5000 0.5000 0 0
err_estimate=(h*M/2*L)*(exp(L*(t-a))-1);
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error,
error estimate ')
E=[t; w; y; abs(y-w); err_estimate]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error, error estimate
E=
err_estimate=(h*M/2*L)*(exp(L*(t-a))-1);
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error,
error estimate ')
E=[t; w; y; abs(y-w); err_estimate]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error, error estimate
E=
1.0000 0.5000 2.6409 2.1409 0
Part D:
% Euler's method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b] clear all, close
all, clc f=@(t,y) cos(2*t)+sin(3*t); %% define f(t,y)
y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given
err_estimate=(h*M/2*L)*(exp(L*(t-a))-1);
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error,
error estimate ')
E=[t; w; y; abs(y-w); err_estimate]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error, error estimate
E=
0 0.5000 0.5000 0 0
EXERCISE#5.4
Question#1
(By RK2 Method)
Part A:
% Runge-Kutta's method of order 2
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
0 0.5000 0.5000 0
Part B;
% Runge-Kutta's method of order 2
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc f=@(t,y) t+(1/(1-t)); %% define function f(t,y)
a=1; b=2; %% define the domain
h=0.5; %% define step-size
n=(b-a)/h; %% define number of points t=a:h:b;
w(1)=0.5;
for j=1:n
k=h*feval(f,t(j),w(j));
w(j+1)=w(j)+ h* feval(f,t(j)+h/2,w(j)+k/2); end
y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given y=y(t); %% compute the exact soltion
over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
ti, wi, yi, actual-error
E=
Part C:
% Runge-Kutta's method of order 2
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc f=@(t,y) (t*log(t)+(2*t)); %% define function f(t,y)
a=2; b=3; %% define the domain
h=0.25; %% define step-size
n=(b-a)/h; %% define number of points
t=a:h:b;
w(1)=0.5;
for j=1:n
k=h*feval(f,t(j),w(j));
w(j+1)=w(j)+ h* feval(f,t(j)+h/2,w(j)+k/2); end
y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given y=y(t); %% compute the exact soltion
over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
E=
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc
f=@(t,y) (0.2*t*exp(1).^(3*t))-(0.04*exp(1).^(3*t))+(0.04*exp(1).^(-2*t)) %% define the function
a=0; b=1; %% define the domain
h=0.5; %% step size
n=(b-a)/h; %% number of grid points
t=a:h:b; %% grid points in [a,b]
w(1)=0.5;
for j=1:n
k1=h*feval(f,t(j),w(j)); k2=h*feval(f,t(j)+h/2,w(j)+k1/2);
k3=h*feval(f,t(j)+h/2,w(j)+k2/2); k4=h*feval(f,t(j)+h,w(j)+k3);
w(j+1)=w(j)+(1/6)*(k1+2*k2+2*k3+k4); end y=@(t) (t+1).^2-0.5*exp(t); %%
define exact solution if given
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
actual-error
E=
0 0.5000 0.5000 0
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc
f=@(t,y) t+(1/(1-t)) %% define the function
a=1; b=2; %% define the domain
h=0.5; %% step size
n=(b-a)/h; %% number of grid points
t=a:h:b; %% grid points in [a,b]
w(1)=0.5;
for j=1:n
k1=h*feval(f,t(j),w(j)); k2=h*feval(f,t(j)+h/2,w(j)+k1/2); k3=h*feval(f,t(j)+h/2,w(j)+k2/2);
k4=h*feval(f,t(j)+h,w(j)+k3); w(j+1)=w(j)+(1/6)*(k1+2*k2+2*k3+k4); end y=@(t) (t+1).^2-
0.5*exp(t); %% define exact solution if given
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b] fprintf(' ti, wi, yi, actual-error
')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
f=
@(t,y)t+(1/(1-t))
ti, wi, yi, actual-error
E=
Part C:
% RK4 method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc
f=@(t,y) y + t*y.^1/2 %% define the function
a=2; b=3; %% define the domain
h=0.25; %% step size
n=(b-a)/h; %% number of grid points
t=a:h:b; %% grid points in [a,b]
w(1)=0.5;
for j=1:n
k1=h*feval(f,t(j),w(j));
k2=h*feval(f,t(j)+h/2,w(j)+k1/2); k3=h*feval(f,t(j)+h/2,w(j)+k2/2); k4=h*feval(f,t(j)+h,w(j)+k3);
w(j+1)=w(j)+(1/6)*(k1+2*k2+2*k3+k4); end y=@(t) (t+1).^2-0.5*exp(t); %% define exact
solution if given
y=y(t); %% compute the exact soltion over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
f=
@(t,y)y+t*y.^1/2
E=
Part D:
% RK4 method
% to approximate the solution of initial value problem y'= f(t,y) with y(a)=y0, over [a,b]
clear all, close all, clc
f=@(t,y) cos(2*t) + sin (3*t) %% define the function
a=0; b=1; %% define the domain
h=0.25; %% step size
n=(b-a)/h; %% number of grid points
t=a:h:b; %% grid points in [a,b]
w(1)=0.5;
for j=1:n
k1=h*feval(f,t(j),w(j));
k2=h*feval(f,t(j)+h/2,w(j)+k1/2);
k3=h*feval(f,t(j)+h/2,w(j)+k2/2);
k4=h*feval(f,t(j)+h,w(j)+k3);
w(j+1)=w(j)+(1/6)*(k1+2*k2+2*k3+k4); end
y=@(t) (t+1).^2-0.5*exp(t); %% define exact solution if given y=y(t); %% compute the exact soltion
over the grid points t_i in [a,b]
fprintf(' ti, wi, yi, actual-error ')
E=[t; w; y; abs(y-w)]'
plot(t,y,'--',t,w,'-o') grid on
legend('Exact solution','Numerical solution','Location','NorthEastOutside');
Answer:
f=
@(t,y)cos(2*t)+sin(3*t)
E=
0 0.5000 0.5000 0