SCILAB PROGRAMS
Name – Sachin
Roll no. – 2030193
Semester- IV
Group – G5
Physics honours
Dirac delta
//To analyze the limiting case of gaussian function as dirac-delta function
Clearall
clc
function y=f(x, sig) //defining the funcion
m=0
y=((2*%pi*sig**2)**-0.5)*(%e**((-(x-m)**2)/(2*sig**2))); //Gaussian distribution
endfunction
i=1
for sig=1:-0.1:0
j=1
for x=-10:0.05:10
A(i,j)=x
B(i,j)=f(x,sig)
j=j+1
end
i=i+1
end
for i=1:10
scf(1)
// Plotting of the graphs
subplot(2,5,i)
plot2d(A(i,:),B(i,:),rect=[-3,-0.5,3,5])
title("Dirac Delta vs x ")
xlabel("x","color","b","fontsize",3)
ylabel("y","color","b","fontsize",3)
xgrid(1,1)
end;
Output for the first program will be
Harmonic Oscillations with Initial Values
/* SHM */
clc;
function dz=f(t, z)
dz(1)=z(2);
dz(2)=[-2*z(1)]/2; // function
endfunction
z0=0; //initial values
y0=2;
y0p=-1;
z=0:0.1:10; //range
sol=ode([y0;y0p],z0,z,f) //ODE
g=[z' sol(1,:)']; //displaying the matrices and plotting
disp(g,"t & y=");
subplot(2,1,1);
plot2d(z,sol(1,:),5);
xlabel("time(t)");
ylabel ("Position(y)");
title ("Position v/s Time","fontsize",4);
xgrid(0.5);
subplot(2,1,2);
plot2d(sol(1,:),sol(2,:),2);
xlabel("Position (y)");
ylabel ("Velocity(v)");
title("Phase Plot","fontsize",4);
xgrid(0.5);
Output:
t & y are:-
0. 2.
0.1 1.8901749
0.2 1.7614638
0.3 1.6151527
0.4 1.4527035
0.5 1.2757395
0.6 1.0860286
0.7 0.8854665
0.8 0.6760572
0.9 0.4598929
1. 0.2391335
1.1 0.0159848
1.2 -0.2073237
1.3 -0.4285606
1.4 -0.6455155
1.5 -0.8560206
1.6 -1.0579727
1.7 -1.2493538
1.8 -1.4282518
1.9 -1.5928791
2.0 -1.741591
2.1 -1.8729014
2.2 -1.9854983
2.3 -2.0782569
2.4 -2.1502501
2.5 -2.2007588
2.6 -2.2292782
2.7 -2.2355234
2.8 -2.2194321
2.9 -2.1811649
3.0 -2.1211042
3.1 -2.0398502
3.2 -1.9382147
3.3 -1.8172131
3.4 -1.6780546
3.5 -1.5221295
3.6 -1.3509958
3.7 -1.1663634
3.8 -0.9700771
3.9 -0.7640981
4. -0.5504844
4.1 -0.3313705
4.2 -0.1089457
4.3 0.1145677
4.4 0.3369363
4.5 0.5559385
4.6 0.7693858
4.7 0.9751457
4.8 1.1711623
4.9 1.355477
5. 1.5262482… till t = 10
Damped Harmonic Oscillations
//Sachin 2030193
HO ( UNDER Damped harmonic oscillations)*/
clc;
//defining the function
function dz=f(t, z)
dz(1)=z(2);
dz(2)=[-(0.5*z(2))-z(1)];
endfunction
//initial conditions
z0=0;
y0=1;
y0p=-1;
z=0:01:20; //range
sol=ode([y0;y0p],z0,z,f) //inbuilt ode command
g=[z' sol(1,:)'];
disp("t & y=",g); //DISPLAYING THE VALUES OF TIME & DISPLACEMENT
subplot(2,1,1); //PLOTTING THE RESULTS
plot2d(z,sol(1,:),5);
xlabel("time(t)");
ylabel ("Position(y)");
title ("Position v/s Time","fontsize",4);
xgrid(0.5);
subplot(2,1,2);
plot2d(sol(1,:),sol(2,:),2);
xlabel("Position (y)");
ylabel ("Velocity(v)");
title("Phase Plot","fontsize",4);
xgrid(0.5);
Output:
"t & y="
0. 1.
1. -0.0556367
2. -0.6556447
3. -0.5450341
4. -0.0834678
5. 0.2568975
6. 0.2774056
7. 0.0890675
8. -0.0896295
9. -0.1331438
10. -0.0631716
11. 0.0249903
12. 0.060376
13. 0.0381404
14. -0.0029509
15. -0.0257383
16. -0.020931
17. -0.0028661
18. 0.0101652
19. 0.0107119
20. 0.0032905
● For Overdamped Oscillations
/*Sachin 2030193
(OVERDAMPED OSCILLATIONS)*/
clc;
function dz=f(t, z)
dz(1)=z(2);
dz(2)=[-(2.5*z(2))-z(1)];
endfunction
z0=0;
y0=1;
y0p=-1;
z=0:0.1:5;
sol=ode([y0;y0p],z0,z,f)
g=[z' sol(1,:)'];
disp(g,"t & y=");
subplot(2,1,1);
plot2d(z,sol(1,:),5);
xlabel("time(t)");
ylabel ("Position(y)");
title ("Position v/s Time","fontsize",4);
xgrid(0.5);
subplot(2,1,2);
plot2d(sol(1,:),sol(2,:),2);
xlabel("Position (y)");
ylabel ("Velocity(v)");
title("Phase Plot","fontsize",4);
xgrid(0.5);
Output:
T & y are
0. 1.
0.1 0.9070632
0.2 0.826665
0.3 0.7567425
0.4 0.6955968
0.5 0.641827
0.6 0.5942769
0.7 0.5519911
0.8 0.5141789
0.9 0.4801851
1. 0.4494655
1.1 0.4215676
1.2 0.3961137
1.3 0.3727884
1.4 0.3513269
1.5 0.3315067
1.6 0.31314
1.7 0.2960677
1.8 0.2801543
1.9 0.2652843
2. 0.2513582
2.1 0.2382904
2.2 0.2260065
2.3 0.2144418
2.4 0.2035394
2.5 0.1932492
2.6 0.1835267
2.7 0.1743324
2.8 0.1656306
2.9 0.1573894
3. 0.1495797
● For Critically Damped Oscillations
TAKING gamma = 2 */
clc;
function dz=f(t, z)
dz(1)=z(2);
dz(2)=[-(2*z(2))-z(1)];
endfunction
z0=0;
y0=1;
y0p=-1;
z=0:0.1:20;
sol=ode([y0;y0p],z0,z,f)
g=[z' sol(1,:)'];
disp(g,"t & y=");
subplot(2,1,1);
plot2d(z,sol(1,:),5);
xlabel("time(t)");
ylabel ("Position(y)");
title ("Position v/s Time","fontsize",4);
xgrid(0.5);
subplot(2,1,2);
plot2d(sol(1,:),sol(2,:),2);
xlabel("Position (y)");
ylabel ("Velocity(v)");
title("Phase Plot","fontsize",4);
xgrid(0.5);
Output:
T & y are
0. 1.
0.1 0.9070632
0.2 0.826665
0.3 0.7567425
0.4 0.6955968
0.5 0.641827
0.6 0.5942769
0.7 0.5519911
0.8 0.5141789
0.9 0.4801851
1. 0.4494655
1.1 0.4215676
1.2 0.3961137
1.3 0.3727884
1.4 0.3513269
1.5 0.3315067
1.6 0.31314
1.7 0.2960677
1.8 0.2801543
1.9 0.2652843
2. 0.2513582
2.1 0.2382904
2.2 0.2260065
2.3 0.2144418
2.4 0.2035394
2.5 0.1932492
2.6 0.1835267
2.7 0.1743324
2.8 0.1656306
2.9 0.1573894
3. 0.1495797
3.1 0.1421751
3.2 0.1351515
3.3 0.1284867
3.4 0.1221603
3.5 0.1161533
3.6 0.1104481
3.7 0.1050285
3.8 0.0998792
3.9 0.094986
4. 0.0903353
4.1 0.0859148
4.2 0.0817126
4.3 0.0777175
4.4 0.073919
4.5 0.0703073
4.6 0.0668729
4.7 0.063607
4.8 0.0605012
4.9 0.0575475
5. 0.0547385
clc
clf
//Defining Differential Equation
function dy=f(t, z, F0, m, k, w)
dy(1)=z(2)
dy(2)=(F0/m)*cos(w*t)-(k/m)*z(1)
endfunction
//initial conditions
m=2
k=2
F0=2
w=1.2
t0=0
t=0:0.1:100
y0=2
yp=-1
//ODE command
sol=ode([y0;yp],t0,t,f)
//plotting
subplot(121)
plot(t,sol(1,:))
xgrid
xtitle("t v/s x graph","Time","Distance from mean position")
subplot(122)
plot(sol(1,:),sol(2,:))
xgrid
xtitle("v v/s x graph","Distance from mean position","Velocity")
Output:
●
●
● Forced Harmonic Oscillations
my"(t) + ky(t) = F(t)
Taking ω =1.2,
we have 2y"+2y=2cos(1.2t )
y(0)=2 ; y'(0)=-1
Scilab Code: -
/*
SHM (forced harmonic oscillations ) */
clc;
function dz=f(t, z)
dz(1)=z(2);
dz(2)=[(2*cos(1.2*t))-(2*z(1))]/2;
endfunction
z0=0;
y0=2;
y0p=-1;
z=0:0.1:50;
sol=ode([y0;y0p],z0,z,f)
g=[z' sol(1,:)'];
disp(g,"t & y=");
subplot(2,1,1);
plot2d(z,sol(1,:),5);
xlabel("time(t)");
ylabel ("Position(y)");
title ("Position v/s Time","fontsize",4);
xgrid(0.5);
subplot(2,1,2);
plot2d(sol(1,:),sol(2,:),2);
xlabel("Position (y)");
ylabel ("Velocity(v)");
title("Phase Plot","fontsize",4);
xgrid(0.5);
Output:
"t & y="
0. 2.
0.1 1.8951646
0.2 1.7813014
0.3 1.6593337
0.4 1.5301264
0.5 1.3944825
0.6 1.2531421
0.7 1.1067834
0.8 0.9560272
0.9 0.8014422
1. 0.6435532
1.1 0.4828501
1.2 0.3197993
1.3 0.1548548
1.4 -0.0115293
1.5 -0.1788858
1.6 -0.3467223
1.7 -0.5145091
1.8 -0.6816676
1.9 -0.8475608
2. -1.0114845
2.1 -1.1726608
2.2 -1.3302332
2.3 -1.4832648
2.4 -1.6307379
2.5 -1.7715573
2.6 -1.9045558
2.7 -2.0285019
2.8 -2.1421113
2.9 -2.24406
3. -2.3330005…… till the defined range
Forced oscillations using RK2 method
Scilab Code: -
/*Runge Kutta 2 for Second Order Differential Equation
z''=-z*/
clc
clf
clear
function dy=f(x, z, y)
dy=cos(1.2*x)-z
endfunction
function dz=g(x, z, y)
dz=y
endfunction
x0=0 //Initial Time
xf=100 //Final Time
z0=2 //Displacement from mean position at initial time
y0=5 //Velocity at initial time
h=%pi/20
x(1)=x0
z(1)=z0
y(1)=y0
n=(xf-x0)/h
for i=1:n
x(i+1)=x(i)+h
k1=h*f(x(i),z(i),y(i))
l1=h*g(x(i),z(i),y(i))
k2=h*f(x(i)+h,z(i)+l1,y(i)+k1)
l2=h*g(x(i)+h,z(i)+l1,y(i)+k1)
z(i+1)=z(i)+0.5*(l1+l2)
y(i+1)=y(i)+0.5*(k1+k2)
end
subplot(121)
plot(x,z)
xgrid
xtitle("displacement v/s time graph","time","displacement")
subplot(122)
plot(z,y)
xgrid
xtitle("v v/s x graph","Distance from mean position","Velocity")
Output:
● Forced Damped Harmonic Oscillations
/*Forced Damped Oscillation
*/
clc
clf
//Defining Function
function dy=f(t, z, F0, m, k, w, gam)
dy(1)=z(2)
dy(2)=(F0/m)*cos(w*t)-(k/m)*z(1)-(gam/m)*z(2)
endfunction
m=2
k=2
F0=2
w=1.2
t0=0
t=0:0.1:100
gam=0.1
y0=2
yp=-1
sol=ode([y0;yp],t0,t,f)
subplot(121)
plot(t,sol(1,:))
xgrid
xtitle("t v/s x graph","Time","Distance from mean position")
subplot(122)
plot(sol(1,:),sol(2,:))
xgrid
xtitle("v v/s x graph","Distance from mean position","Velocity")
Output:
Shooting Method
Using Inbuilt command
//Sachin 2030193 shooting method by inbuilt ...//
function p=f(x, y)
p(1)=y(2)
p(2)= -y(1)
endfunction
clc
clf
xi=input("Enter the initial value of x :")
xf=input("Enter the final value of x :")
yi=input("Enter the initial value of y :")
yf=input("Enter the final value of y :")
z=2
e=0.01
while abs(z-yf)>e
yi(2)=input("Guess the value of dy/dx at initial value :")
h=0.01;
x=xi:h:xf // range of x
y0=[yi;yf]
//using inbuilt function
y=ode(y0,xi,x,f);
plot(x,y(1,:),'b')
plot(xf,yf,'r.')
end
Output:
The correct guess would be 0.825
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 ,
z1 = y(x) z2 =
y'(x)
So equation becomes--
z1' = z2
z2' = (3z1-5z2)/2
with z1(0) = 1 & z2(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:-