from
numerical method
by ECE IISc
euler, RK-4, Bs method
|
| ajadnumerical1.m |
% Numerical Experiment- program for all Euler, Midpoint, Adams-Bashforth Runge - Kutta method
clear all
clc
fprintf(' enter the value of h = ') % for diff value of h
h=input(' '); % enter the value of h
hold on
x=0;
t=0:(0.5):3; % time - step
fprintf(' euler for n=1\n midpoint for n=2\n adams Bashforth for n=3\n RK-4 for n=4\n')
fprintf(' enter the value of n = ')
n=input(' '); % enter the choice of methods
% program for Eulers method
if n==1
for i=1:6
k1=exp(cos(t(i)*x(i)))*h;
x(i+1)=x(i)+k1
format long
end
x_at_3s= x(7)
plot(t,x)
xlabel('t')
ylabel('x')
axis 'square'
% program for midpoint method
elseif n==2
for i=1:6
k1=exp(cos(t(i)*x(i)));
k2=x(i)+exp(cos(t(i)*x(i)))*(h/2);
k3=exp(cos((t(i)+(h/2))*k2));
x(i+1)=x(i)+h*k3
format long
end
x_at_3s= x(7)
plot(t,x)
xlabel('t')
ylabel('x')
axis 'square'
% program for adams Bashforth method
elseif n==3
for j=1:3
k1=exp(cos(t(j)*x(j)));
k2=exp(cos((t(j)+0.5*h)*(x(j)+0.5*h*k1)));
k3=exp(cos((t(j)+0.5*h)*(x(j)+0.5*h*k2)));
k4=exp(cos((t(j+1))*(x(j)+h*k3)));
x(j+1)=x(j)+(1/6)*h*(k1+2*k2+2*k3+k4);
end
for j=4:6
x(j+1)=x(j)+(h/24)*(55*exp(cos(t(j)*x(j)))-59*exp(cos(t(j-1)*x(j-1)))+37*exp(cos(t(j-2)*x(j-2)))-9*exp(cos(t(j-3)*x(j-3))))
format long
end
x_at_3s= x(7)
plot(t,x)
xlabel('t')
ylabel('x')
axis 'square'
% program for Runge-kutta method
elseif n==4
for i=1:6
k1=exp(cos(t(i)*x(i)));
k2=exp(cos((t(i)+0.5*h)*(x(i)+0.5*h*k1)));
k3=exp(cos((t(i)+0.5*h)*(x(i)+0.5*h*k2)));
k4=exp(cos((t(i+1))*(x(i)+h*k3)));
x(i+1)=x(i)+(1/6)*h*(k1+2*k2+2*k3+k4)
format long
end
x_at_3s= x(7)
plot(t,x)
xlabel('t')
ylabel('x')
axis 'square'
end
|
|
Contact us at files@mathworks.com