Name: Aditya Kumar
Roll no.: 3914
Group: A+B
Scilab Code 11
Aim:
Solve the first order ODE dy/dx+y=0 in the range of 0<x<5 and plot it.
Hence, make a comparison with the analytical solution.
[Modified Euler’s Method]
clc
clear
clf()
//Solve the first order ODE dy/dx+y=0 in the range of 0<x<5 and plot [Link],
make a comparison with the analytical solution.
function func=f(x, y)
func=-y
endfunction
x0=0
y(1)=1
h=0.1
X=x0:h:5
x=X'
n=length(x)
disp(['x' 'y' 'analytical'])
for i=1:n-1
x(i+1)=x(i)+h
y(i+1)=y(i)+h*(f(x(i),y(i)))
y(i+1)=y(i)+(h/2)*(f(x(i),y(i))+f(x(i+1),y(i+1)))
disp([x(i+1) y(i+1) exp(-x(i+1))])
end
plot(x,y,'r')
//analytical solution
plot(x,exp(-x),'--g')
legend(['Solution using Modified Eulers method','Analytical Solution'])
xtitle('Solution of differential equation dy/dx+y=0','x-axis','y-axis')
OUTPUT:
"x" "y" "analytical"
0.1 0.905 0.9048374
0.2 0.819025 0.8187308
0.3 0.7412176 0.7408182
0.4 0.670802 0.67032
0.5 0.6070758 0.6065307
0.6 0.5494036 0.5488116
0.7 0.4972102 0.4965853
0.8 0.4499753 0.449329
0.9 0.4072276 0.4065697
1. 0.368541 0.3678794
1.1 0.3335296 0.3328711
1.2 0.3018443 0.3011942
1.3 0.2731691 0.272531
Aim:
Solve the first order ODE dy/dx+y=0 in the range of 0<x<5 and plot it.
Hence, make a comparison with the analytical solution.
[Runge-Kutta Method]
clc
clear
clf()
//Solve the first order ODE dy/dx-y+x=0 in the range of 0<x<5 and plot [Link],
make a comparison with the analytical solution.
function func=f(x, y)
func=-y
endfunction
x0=0
y(1)=1
x_max=5
h=0.1
X=x0:h:x_max
x=X'
n=length(x)
disp('x y')
disp([x0 y(1)])
for i=1:n-1
k1=h*f(x(i),y(i))
k2=h*f(x(i)+h,y(i)+k1)
y(i+1)=y(i)+((1/2)*(k1+k2))
disp([x(i+1) y(i+1)])
end
plot(x,y,'r')
//analytical solution
plot(x,exp(-x),'--g')
legend(['Solution using Runge-Kutta method','Analytical Solution'])
xtitle('Solution of differential equation dy/dx+y=0','x-axis','y-axis')
OUTPUT:
"x y"
0. 1.
0.1 0.905
0.2 0.819025
0.3 0.7412176
0.4 0.670802
0.5 0.6070758
0.6 0.5494036
0.7 0.4972102
0.8 0.4499753
0.9 0.4072276
1. 0.368541
1.1 0.3335296
1.2 0.3018443
Aim:
Solve the first order ODE dy/dx+y=0 in the range of 0<x<5 and plot it.
Hence, make a comparison with the analytical solution.
[Runge-Kutta 4th order Method]
clc
clear
clf()
//Solve the first order ODE dy/dx+y=0 in the range of 0<x<5 and plot [Link],
make a comparison with the analytical solution.
function func=f(x, y)
func=-y
endfunction
x0=0
y(1)=1
x_max=5
h=0.1
X=x0:h:x_max
x=X'
n=length(x)
disp('x y')
disp([x0 y(1)])
for i=1:n-1
k1=h*f(x(i),y(i))
k2=h*f(x(i)+(h/2),y(i)+(k1/2))
k3=h*f(x(i)+(h/2),y(i)+(k2/2))
k4=h*f(x(i)+h,y(i)+k3)
y(i+1)=y(i)+((1/6)*(k1+(2*k2)+(2*k3)+k4))
disp([x(i+1) y(i+1)])
end
plot(x,y,'r')
//analytical solution
plot(x,exp(-x),'--g')
legend(['Solution using Runge-Kutta 4th order method','Analytical Solution'])
xtitle('Solution of differential equation dy/dx+y=0','x-axis','y-axis')
OUTPUT:
"x y"
0. 1.
0.1 0.9048375
0.2 0.8187309
0.3 0.7408184
0.4 0.6703203
0.5 0.6065309
0.6 0.5488119
0.7 0.4965856
0.8 0.4493293
0.9 0.40657
1. 0.3678798
1.1 0.3328714
1.2 0.3011945
Q. Solution of dy/dx-y+x=0 using Runge-Kutta 4th order and
display y(0.1) and y(0.2)
clc
clear
clf()
//Solve the first order ODE dy/dx+y=0 in the range of 0<x<5 and plot [Link],
make a comparison with the analytical solution.
function func=f(x, y)
func=y-x
endfunction
x0=0
y(1)=2
x_max=5
h=0.1
X=x0:h:x_max
x=X'
n=length(x)
disp('x y')
for i=1:n-1
k1=h*f(x(i),y(i))
k2=h*f(x(i)+(h/2),y(i)+(k1/2))
k3=h*f(x(i)+(h/2),y(i)+(k2/2))
k4=h*f(x(i)+h,y(i)+k3)
y(i+1)=y(i)+((1/6)*(k1+(2*k2)+(2*k3)+k4))
if x(i+1)==0.1 || x(i+1)==0.2
disp([x(i+1) y(i+1)])
end
end
plot(x,y,'r')
//analytical solution
plot(x,exp(x)+x+1,'--g')
legend(['Solution using Runge-Kutta 4th order method','Analytical Solution'])
xtitle('Solution of differential equation dy/dx-y+x=0','x-axis','y-axis')
OUTPUT:
"x y"
0.1 2.2051708
0.2 2.4214026