[go: up one dir, main page]

0% found this document useful (0 votes)
63 views14 pages

Scilab 1

The document presents a Scilab program for solving a second-order ordinary differential equation (ODE) using various numerical methods including Runge-Kutta (both 2nd and 4th order) and Euler's method, along with an in-built Scilab function. It provides the exact solution of the ODE and compares the numerical solutions with the exact results, displaying outputs and errors for each method. The document includes code snippets and plots to visualize the results of the computations.

Uploaded by

Sachin Gurjar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views14 pages

Scilab 1

The document presents a Scilab program for solving a second-order ordinary differential equation (ODE) using various numerical methods including Runge-Kutta (both 2nd and 4th order) and Euler's method, along with an in-built Scilab function. It provides the exact solution of the ODE and compares the numerical solutions with the exact results, displaying outputs and errors for each method. The document includes code snippets and plots to visualize the results of the computations.

Uploaded by

Sachin Gurjar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SCILAB PROGRAM

Name- Sanskar Chauhan


Roll. No. -1930183
4th Sem ,2nd Year
University Roll. No. -19036567092

Second Order ODE


With Initial ​Vaules

The Differential equation used here is:

​2y"+5y'-3y=0
with intial values ​y(0)=1 & y'(0)=4

To solve this equation , we substitute


z=y'
So equation becomes--
2z'+5z-3y=0
⇒ ​z'=(3y-5z)/2 with z(0)=1 & z'(0)=4

Then we solve it like first ODE.The exact solution of


above Differential equation is--
y= 2e​½x ​- e​-3x
Using Runga Kutta-4 Method

Code:

clc;
function dy=f(x,y,z)
dy=[(3*y)-(5*z)]/2;
endfunction
x0=0;
y0=1;
z0=4;
h=0.1;
x=x0;y=y0;z=z0;
for i=1:5
x(i+1)=x(i)+h
z(i+1)=z(i)+h
k1=h*z(i)
l1=h*f(x(i),y(i),z(i))
k2=h*(z(i)+l1/2)
l2=h*f(x(i)+h/2,y(i)+k1/2,z(i)+l1/2)
k3=h*(z(i)+l2/2)
l3=h*f(x(i)+h/2,y(i)+k2/2,z(i)+l2/2)
k4=h*(z(i)+l3)
l4=h*f(x(i)+h,y(i)+k3,z(i)+l3)
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4)
z(i+1)=z(i)+(1/6)*(l1+2*l2+2*l3+l4)
end
g=[x,y,z]
disp(g,"g=");
plot(x,y,'r.-');
yexact=2*exp((1/2)*x)-exp(-3*x);
error=yexact-y;
t=[x y yexact error]
disp(t,"t=");
plot(x,yexact,'b--');
xtitle('Using Runga Kutta-4 Method');
legend('y','yexact');
xlabel("x");
ylabel("y/yexact");

Output:

g=

x. y. y'

0. 1. 4.
0.1 1.3617047 3.2737836
0.2 1.6615016 2.7516915
0.3 1.9170671 2.3816384
0.4 2.1415799 2.1250794
0.5 2.3448916 1.953503
t=
x. y. yexact. Error

0. 1. 1. 0.
0.1 1.3617047 1.361724 0.0000193
0.2 1.6615016 1.6615302 0.0000286
0.3 1.9170671 1.9170988 0.0000318
0.4 2.1415799 2.1416113 0.0000314
0.5 2.3448916 2.3449207 0.0000291

Plot:-

Using Runga Kutta-2 Method

Code:

clc;
function dy=f(x,y,z)
dy=[(3*y)-(5*z)]/2;
endfunction
x0=0;
y0=1;
z0=4;
h=0.1;
x=x0;y=y0;z=z0;
for i=1:5
x(i+1)=x(i)+h
k1=h*z(i)
l1=h*f(x(i),y(i),z(i))
k2=h*(z(i)+l1)
l2=h*f(x(i)+h,y(i)+k1,z(i)+l1)
y(i+1)=y(i)+(1/2)*(k1++k2)
z(i+1)=z(i)+(1/2)*(l1+l2)
end
g=[x,y,z];
disp(g,"g=");
plot(x,y,'b.-');
yexact=2*exp((1/2)*x)-exp(-3*x);
error=yexact-y;
t=[x y yexact error];
disp(t,"t=");
plot(x,yexact,'r--');
legend('y','yexact');
xtitle ("Using Runga Kutta-2 Method");
xlabel("x");
ylabel("y/yexact");

Output:

g=

x. y. y'

0. 1. 4.
0.1 1.3575 3.28625
0.2 1.6552281 2.7702016
0.3 1.910035 2.4022452
0.4 2.1345567 2.145463
0.5 2.3382939 1.9723945

t=

x. y. yexact. Error

0. 1. 1. 0.
0.1 1.3575 1.361724 0.0042240
0.2 1.6552281 1.6615302 0.0063021
0.3 1.910035 1.9170988 0.0070639
0.4 2.1345567 2.1416113 0.0070546
0.5 2.3382939 2.3449207 0.0066268

​Plot:-
​Using Euler's Method

Code:
clc;
function dy=f(x,y,z)
dy=[(3*y)-(5*z)]/2;
endfunction
x0=0;
y0=1;
z0=4;
h=0.1;
x=x0;y=y0;z=z0;
for i=1:5
x(i+1)=x(i)+h
y(i+1)=y(i)+h*z(i)
z(i+1)=z(i)+h*f(x(i),y(i),z(i))
end
g=[x,y,z];
disp(g,"g=");
plot(x,y,'b.-');
yexact=2*exp((1/2)*x)-exp(-3*x);
error=yexact-y;
t=[x y yexact error];
disp(t,"t=");
plot(x,yexact,'r--');
xtitle('Using Eulers Method');
legend('y','yexact');
xlabel("x");
ylabel("y/yexact");

Output:

g=
x. y. y'
0. 1. 4.
0.1 1.4 3.15
0.2 1.715 2.5725
0.3 1.97225 2.186625
0.4 2.1909125 1.9358062
0.5 2.3844931 1.7804916

t=
x. y. yexact. Error
0. 1. 1. 0.
0.1 1.4 1.361724 - 0.0382760
0.2 1.715 1.6615302 - 0.0534698
0.3 1.97225 1.9170988 - 0.0551512
0.4 2.1909125 2.1416113 - 0.0493012
0.5 2.3844931 2.3449207 - 0.0395725
Plot:-
Using In-Built Scilab Function

The Differential equation used here is:

​2y"+5y'-3y = 0

with intial values y(0) = 1 & y'(0) = 4


We substitute ,
z​1 =​ y(x)
z​2 =
​ y'(x)

So equation becomes--
​z​1​' = z​2
z​2​' = (3z​1​-5z​2​)/2
with ​z​1​(0) = 1 & z​2​(0) = 4

.The exact solution of above Differential equation is--

y = 2e​½x​ - e​-3x
Code:

clc;
function dz=f(x,z)
dz(1)=z(2);
dz(2)=[(3*z(1))-(5*z(2))]/2;
endfunction
z0=0;
y0=1;
y0p=4;
z=0:0.1:0.5;
sol=ode([y0;y0p],z0,z,f)
g=[z' sol(1,:)'];
disp(g,"x. & y=");
plot2d(z,sol(1,:),5);
yexact=2*exp((1/2)*z)-exp(-3*z)
error=yexact-sol(1,:);
h=[z' sol(1,:)' yexact' error'];
disp(h,"x,y,yexact,error=");
plot2d(z,yexact,-1);
legend('y','yexact');
xlabel("x");
ylabel ("y/yexact");
Output:

x. & y=
x. y
0. 1.
0.1 1.3617239
0.2 1.6615301
0.3 1.9170987
0.4 2.1416112
0.5 2.3449206

​x, y, yexact, error =

x. y. yexact. error
0. 1. 1. 0.
0.1 1.3617239 1.361724 4.419D-08
0.2 1.6615301 1.6615302 0.0000001
0.3 1.9170987 1.9170988 7.976D-08
0.4 2.1416112 2.1416113 7.080D-08
0.5 2.3449206 2.3449207 3.969D-08
Plot:-

You might also like