0% found this document useful (0 votes)
19 views17 pages

Shooting Method-Merged

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)
19 views17 pages

Shooting Method-Merged

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/ 17

//Solve Boundary value Problems using Shooting Method

//solve y''(x)+y(x)=0 with y(0)=1 and y(pi/2)=1 by shooting method

clear
clc

xi=input("initial boundary value,xi = ");


xf=input("final boundary value xf = ");

n=input("value of number of grid points,n = ");


h=(xf-xi)/n;

yi=input("yi = ");
yf=input("yf = ") ;

x=linspace(xi,xf,n+1);

y(1)=yi;x(1)=xi;
function p=f(x, y, g)
p=-y;
endfunction

// first guess
g(1)=input("initial guess 1 ")
for i=1:n
x(i+1)=x(i)+h
y(i+1)=y(i)+g(i)*h
g(i+1)=g(i)+f(x(i),y(i),g(i))*h
end
y1=y(n+1)
g1=g(1)

// second guess
g(1)=input("initial guess 2 = ")
for i=1:n
y(i+1)=y(i)+g(i)*h
g(i+1)=g(i)+f(x(i),y(i),g(i))*h
end
y2=y(n+1)
g2=g(1)

g(1)=g1+((g2-g1)/(y2-y1))*(yf-y1) // interpolation
for i=1:n
y(i+1)=y(i)+g(i)*h
g(i+1)=g(i)+f(x(i),y(i),g(i))*h
end

plot (x,y)
xgrid(5)
xtitle('Solution of ODE using SHOOTING METHOD','x values', 'y values')
a=gca()
a.x_location="origin"
a.y_location="left"
//Solve Boundary value Problems using Shooting Method
//solve y''(x)-y(x)=0 with y(0)=1 and y'(1)=0 by shooting method

clear
clc

xi=input("initial boundary value,xi = ");


xf=input("final boundary value xf = ");

n=input("value of number of grid points,n = ");


h=(xf-xi)/n;

yi=input("yi = ");
gf=input("gf = ") ;

x=linspace(xi,xf,n+1);

y(1)=yi;x(1)=xi;
function p=f(x, y, g)
p=y;
endfunction

// first guess
g(1)=input("initial guess 1 ")
for i=1:n
x(i+1)=x(i)+h
y(i+1)=y(i)+g(i)*h
g(i+1)=g(i)+f(x(i),y(i),g(i))*h
end
y1=y(n+1)
g1=g(1)
g1o=g(n+1)

// second guess
g(1)=input("initial guess 2 = ")
for i=1:n
y(i+1)=y(i)+g(i)*h
g(i+1)=g(i)+f(x(i),y(i),g(i))*h
end
y2=y(n+1)
g2=g(1)
g2o=g(n+1)

g(1)=g1+((g2-g1)/(g2o-g1o))*(gf-g1o) // interpolation
for i=1:n
y(i+1)=y(i)+g(i)*h
g(i+1)=g(i)+f(x(i),y(i),g(i))*h
end

plot (x,y)
xgrid(5)
xtitle('Solution of ODE using SHOOTING METHOD','x values', 'y values')
a=gca()
a.x_location="origin"
a.y_location="left"

You might also like