Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

The shooting method for bvp and intepolation to find the value of the initial slope

Status
Not open for further replies.

Tygra_1983

Student
Oct 8, 2021
121
Hi all

I have been learning the shooting method for boundary value problems. For second order differential equations, as you may well know, you need two initial conditions to get the procedure going; one known condition and one guess. I am using eulers method, so I am reducing the second order ODE to two first order ODE's. I am using a random equation to practice with that is in the form:

y'' = 4y' + 2y^3 + 2*x.

If z = y'
then
z' = y''

Which coverts to two first order ODE's

z' = 4z + 2y^3 + 2x
y' = z

My boundary conditions are y(0) = 0 and y(4) = 0. I am shooting the initial condition so that after 4 iterations my y value = 0.

So to break it down, I have made 4 guesses for z (the intial condition of the slope). These are -10, -2, 1 and 4. When I plug these into the code I get the corresponding values: -1.3405e+09, -1.0737e+07, 1348392 and 8.5814e+07. I figure these are enough points to get a good approximation using intepolation. Consequently to get my correct intial value of the slope so that at the end y(4) = 0, or close to zero.

Here is the result of my intepolation:

intepolation_jvuvxn.jpg


From the curve, the line crosses the x axis at -0.044486. This should be my value for the initial condition of the slope at z. However, when I perform the for loop again with this value I get a value of 428.3653. This is nowhere near zero.

I am doing all this in Octave. I don't expect anyone to go through the whole code, but its there so you can see what I'm doing.

I feel bad for posting such a long question, but I'm really stuck on why this method is not working. I hope someone can help please?

Code:
h=3
z = -10
x = 0
y = 0

for i = 1:4
       z(i+1) = z(i) + (2*y(i)^3 + 4*z(i) + 2*x(i))*h;
       y(i+1) = y(i) + z(i)*h
       x(i+1) = x(i) + h;
end
q1 = y(end)

z = -2
x = 0
y = 0

for i = 1:4
       z(i+1) = z(i) + (2*y(i)^3 + 4*z(i) + 2*x(i))*h;
       y(i+1) = y(i) + z(i)*h
       x(i+1) = x(i) + h;
end

q2 = y(end)


z = 1
y = 0
x = 0

for i = 1:4
       z(i+1) = z(i) + (2*y(i)^3 + 4*z(i) + 2*x(i))*h;
       y(i+1) = y(i) + z(i)*h
       x(i+1) = x(i) + h;
end

q3 = y(end)

y = 0
x = 0
z = 4

for i = 1:4
       z(i+1) = z(i) + (2*y(i)^3 + 4*z(i) + 2*x(i))*h;
       y(i+1) = y(i) + z(i)*h
       x(i+1) = x(i) + h;
end
q4 = y(end)




    p1 = [-10, q1]
    p2 = [-2, q2]
    p3 = [1, q3]
    p4 = [4,q4]
    P = [p1;p2;p3;p4]
    X = [1 (-10) (-10)^2 (-10)^3;
         1 (-2) (-2)^2 (-2)^3;
         1 1 1^2 1^3;
         1 (4) (4)^2 (4)^3 ]
     A = inv(X)*P(:,2)
     x = -30:0.1:20;

y = A(1) + A(2).*x + A(3).*x.^2 + A(4).*x.^3;
      scatter(P(:,1),P(:,2))
      hold on
      plot(x,y)
      grid on

      grid on
      y1 = interp1(y,x,0,'spline')
      z = y1
      y =0
      x = 0
for i = 1:4
       z(i+1) = z(i) + (2*y(i)^3 + 4*z(i) + 2*x(i))*h;
       y(i+1) = y(i) + z(i)*h
       x(i+1) = x(i) + h;
end
 
Replies continue below

Recommended for you

I must confess i don't understand what you are up to. I'd have thought an error of 400 when 3 of your numbers are in the 10 million+ range is not too bad. Have you tried evaluating your answer near x=-0.044486, it may be you are running into numerical problems. Try using Newton Raphaelson rather than interpolating a cubic spline.





Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376
 
GregLocock said:
I must confess i don't understand what you are up to.

Hi Greg,

I have the boundary conditions y(0) = 0 and y(4) = 0. y(0) = 0 in one initial condition that we know. The slope dz/dx is unknown, Hence, I am making 4 guesses for the initial conditions for dzdx which is -10, -2, 1 and 4. So these are substituted into the system of first order ODE's. In the code there a 4 for loops, each substituted with the initital guesses for dz/dx. From these you get the corresponding y values. Then I interpolate to find the value of dz/dx when y = 0. In the code the y1 = interp1(y,x,0,'spline') should give me this initial value that I can substitute back into the equation, giving me y = 0, or close to.

Does this help to understand me?

GregLocock said:
I'd have thought an error of 400 when 3 of your numbers are in the 10 million+ range is not too bad.

You have opened up my eyes to this. Yes, when the y values are that high, 428.3653 is indeed close to zero. Because of these enormous y values, and increment of 1 either side could give much larger results.

GregLocock said:
Try using Newton Raphaelson rather than interpolating a cubic spline.

I am wondering how to do this. I know the Newton-Rhapson Method is: xn+1 = xn - f(xn)/f'(xn).

How do I use my equations, which are two first order ODE's, in the Newton-Rhapson formula?


 
You get a value for the function you are trying to find the zero of. You get the gradient. You apply that NR equation to it for your next guess.

An alternative, to carry on with your initial approach, is to fit a spline over a much smaller range than you currently have. So instead of z=(-10,-2,1,4) use something more like z=(-2,-1,0,1), and maybe even tighter than that. You'll need to change X as well.

Using x to mean two different things, and X to mean a third, makes reading or discussing the script rather difficult

Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376
 
Result!!

I made my step size much smaller. The step size h was originally 3, and I have decreased it to 0.05. This is giving me a value much closer to zero.

Thanks for your help, Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor