Question

2. Use an RK4 shooting method with a step size of h - 0.01 to find the unique negative solution to the boundary value problem

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code shooting method using RK4 for 2nd order differential equation clear all close all Program for shooting method f-efor i= 1 : n k2-h * f (X ( ¡ ) + ( 1 /2 ) *h,yl(i)t(1/2)klg2(i)t(1/2)+11); x(i+1)=x in+1*h; yl (i+1)-double (yl(i)+(1/6) (k0Plotting of u(x) using shooting method 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 .1 0 03 04 05 0608 0.9 Published with MATLAB R2018SDE n. du() 2. てつさん

%%Matlab code shooting method using RK4 for 2nd order differential equation
clear all
close all


%Program for shooting method

f=@(x,y1,y2) y2;                        %function (i)
g=@(x,y1,y2) y1^2-(x./(1+x));           %function (ii)

%all guesses for y2(1) using shooting method
a_ini=[0:.005:10]; y1_end=1;

for ii=1:length(a_ini)
    x(1)=0;y1(1)=0;y2(1)=a_ini(ii); %initial conditions
    h=0.01;                 %step length
    x_in=x(1);              %Initial x
    x_max=1;          %Final x
    n=(x_max-x_in)/h; %number of steps

    %Runge Kutta 4 iterations
    for i=1:n

        k0=h*f(x(i),y1(i),y2(i));
        l0=h*g(x(i),y1(i),y2(i));
        k1=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        l1=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        k2=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        l2=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        k3=h*f(x(i)+h,y1(i)+k2,y2(i)+l2);
        l3=h*g(x(i)+h,y1(i)+k2,y2(i)+l2);
        x(i+1)=x_in+i*h;
        y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        y2(i+1)=double(y2(i)+(1/6)*(l0+2*l1+2*l2+l3));
      
    end
    yy1(ii)=y1(end);
end
p = interp1(yy1,a_ini,y1_end);

fprintf('\tThe value of U2(0) using shooting method is %f.\n',p);

%solution using value of y2(1)

    x(1)=0;y1(1)=0;y2(1)=p; %initial conditions
    h=0.01;            %step length
    x_in=x(1);         %Initial x
    x_max=1;           %Final x
    n=(x_max-x_in)/h; %number of steps

    %Runge Kutta 4 iterations
    for i=1:n

        k0=h*f(x(i),y1(i),y2(i));
        l0=h*g(x(i),y1(i),y2(i));
        k1=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        l1=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        k2=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        l2=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        k3=h*f(x(i)+h,y1(i)+k2,y2(i)+l2);
        l3=h*g(x(i)+h,y1(i)+k2,y2(i)+l2);
        x(i+1)=x_in+i*h;
        y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        y2(i+1)=double(y2(i)+(1/6)*(l0+2*l1+2*l2+l3));
    end
    %plotting the solution
    figure(2)

    plot(x,y1)
    title('Plotting of u(x) using shooting method')
    xlabel('x')
    ylabel('u(x)')
    ps=find(x==0.5);
    fprintf('\t The approximate value for U(%f) is %f.\n',x(ps),y1(ps))
  

    %%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
2. Use an RK4 shooting method with a step size of h - 0.01 to find the unique negative solution t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT