0% found this document useful (0 votes)
155 views4 pages

Shooting Method Scilab

This document summarizes solving a second order differential equation using the shooting method and RK2 integration. It defines the differential equation, boundary conditions, and integration parameters. It then uses the shooting method to iteratively guess the initial slope that satisfies the boundary conditions, integrating each guess using RK2. The correct initial slope is found and the solution is plotted.
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)
155 views4 pages

Shooting Method Scilab

This document summarizes solving a second order differential equation using the shooting method and RK2 integration. It defines the differential equation, boundary conditions, and integration parameters. It then uses the shooting method to iteratively guess the initial slope that satisfies the boundary conditions, integrating each guess using RK2. The correct initial slope is found and the solution is plotted.
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/ 4

//Yatendra verma -164: 29-03-2023

//FOURIER COEFFICIENTS OF A GIVEN PERIODIC FUNCTION:


TRIANGULAR WAVE
clear
clc
clf
A=2
T=A
n=25
x=linspace(-4,4,300)
l1=0
u1=T/2
l2=u1
u2=T
function a=f1(x)
a=x
endfunction
function a=f2(x)
a=A-x
endfunction
a0=(1/T)*(integrate("f1(x)","x",l1,u1)+integrate("f2(x)","x",l2,u2))
for i=1:n

a(i)=(2/T)*(integrate("f1(x)*cos(2*i*%pi*x/T)","x",l1,u1)+integrate("f2(x
)*cos(2*%pi*i*x/T)","x",l2,u2))

b(i)=(2/T)*(integrate("f1(x)*sin(2*%pi*i*x/T)","x",l1,u1)+integrate("f2(x)
*sin(2*%pi*i*x/T)","x",l2,u2))
end
B=0
for i=1:n
a0=a0+a(i)*cos(2*%pi*i*x/T)
B=B+b(i)*sin(2*%pi*i*x/T)
end
y=a0+B
plot2d(x,y)
a=gca()
a.x_location="origin"
xtitle("Plot of fourier series of triangular wave,n="+string(n),"Time
T","Amplitude")
//Solve ODE(2nd order ) with dirichlet boundary using shooting and RK2 method
//Shivam chahar-106
clc
clear;clf;
funcprot(1);//we have to check multiple functions is redifined or not when its
value 1 it will show a warning message if a function is redifined
xi=0;xf=%pi/2;h=0.1;x=xi:h:xf;n=length(x);yi=1;yf=1
function [g]=f(x, y, z)
g=-y;
endfunction
function [y, z]=RK2(xi, zi)
y(1)=yi;z(1)=zi;
for i=1:n-1
s1=z(i);p1=f(x(i),y(i),z(i));
s2=z(i)+h*p1;p2=f(x(i)+h,y(i)+s1*h,z(i)+p1*h);
y(i+1)=y(i)+(h/2)*(s1+s2);
z(i+1)=z(i)+(h/2)*(p1+p2);
end
endfunction
//shooting method
for i=1:2
zt(i)=input("Enter guess "+string(i)+" for initial slope(dy/dx):");
[y,z]=RK2(yi,zt(i));
yn(i)=y(n,1);
end
zc=zt(1)+(zt(2)-zt(1))*(yf-yn(1))/(yn(2)-yn(1));
[y,n]=RK2(yi,zc);
disp("x y z");disp([x' y z]);
plot(x',y,'d-','linewidth',4);
xlabel("x-axis",'fontsize',3,'color',2,'fontname',3);ylabel("y-
axis",'fontsize',3,'color',2,'fontname',3);
title('Shooting method', 'fontsize',5, 'color', 2, 'edgecol', 4, 'fontname ',5)
a=gca
a.box="on"
a.thickness=1
a.font_size=3
a.font_style=3
a.children.children.thickness=3
xstring(.3,1.1,"$\frac {d^2y} {dx^2} +y=0: y (0) =1,y (pi/2) =1$")
d=get ("hdl")
d.font_size=4
OUTPUT:

Enter guess 1 for initial slope(dy/dx):4

Enter guess 2 for initial slope(dy/dx):2

"x y z"

0. 1. 2.

0.1 1.0883739 1.89

0.2 1.1658391 1.76105

0.3 1.2316186 1.6144422

0.4 1.2850529 1.4516461

0.5 1.3256058 1.274293

0.6 1.3528705 1.0841608

0.7 1.3665734 0.8831551

0.8 1.3665767 0.6732907

0.9 1.3528801 0.4566714

1. 1.3256206 0.2354685

1.1 1.285071 0.0118995

1.2 1.2316375 -0.2117944

1.3 1.1658556 -0.4333706

1.4 1.0883843 -0.6506079

1.5 1. -0.8613282

You might also like