Question

Runge-Kutta method R-K method is given by the following algorithm. Yo = y(xo) = given. k1-f(xy) k4-f(xi +h,yi + k3) 6 For i =

Consider the same IVP given in problem 2 and answer the following a) Write a MATLAB script file to find y(2) using h = 0.1 an



dy dr =-1.2y + 7e-0.3x from x = 0 to x = 2.0 with the initial condition y-3 atx-0
Runge-Kutta method R-K method is given by the following algorithm. Yo = y(xo) = given. k1-f(xy) k4-f(xi +h,yi + k3) 6 For i = 0, 1, 2, , n, where h = (b-a)/n.
Consider the same IVP given in problem 2 and answer the following a) Write a MATLAB script file to find y(2) using h = 0.1 and call the file odeRK 19.m b) Generate the following table now using both ode Euler and odeRK19 only for h -0.01. yi (EM) yi (RK) Exact yi Error-EM (%) Error-RK(%) Xi 0.0 0.5 3.000000 4.080575 0.00 0.02 3.000000 3.000000 4.072295 0.00 2.0 c) Use the subplot command to produce plots for exact solution, EM and RK method for h 0.1 and h-0.01 in two different figures. (That means in one graph they have plots for the exact solution, EM and RK for h -0.1 and in another graph for h- 0.01) d) What can you say about the convergence rate of EK and RK method according to part (c)?
dy dr =-1.2y + 7e-0.3x from x = 0 to x = 2.0 with the initial condition y-3 atx-0
0 0
Add a comment Improve this question Transcribed image text
Answer #1


SMatlab code for Eulers forward and Rk4 method clear all close all %A11 h values hh 0.1,0.01 for it-1:2 Program for Euler %f k0sh* f (x-result2( ǐ ) , y-result2( i)) ; k1-h*f(x-result2(1) + (1/2) *h, y-re sult 2 (i) +(1/2) *ko) ; k2-h * f(x-result2(ǐerr2-double( (abs (y result2 (ii)-ext sl)/ext_s1) 100); x result1(ii),y_resultl(ii),y result2 (ii),ext_sl,errl,err2) end endEuler and Exact solution for h-0.100 4.5 -Exact solution Euler Solution 3.5 0.5 1.5 25 RK4 and Exact solution for h-0.100 Exa

%%Matlab code for Euler's forward and Rk4 method
clear all
close all

%---------------------%
%All h values
hh=[0.1,0.01];
for it=1:2
    %Program for Euler
    %function for Euler equation solution
    f=@(x,y) -1.2*y+7.*exp(-0.3*x);
    %Euler steps for h and x_end
    %Initial values
    x0=0;
    y0=3;
    %step size
    h=hh(it);
    %x end value
    xend=2;
    xn=x0:h:xend;
    % Euler steps
    y_result1(1)=y0;
    x_result1(1)=x0;
    %Loop for Euler Steps
    for i=1:length(xn)-1
        x_result1(i+1)= x_result1(i)+h;
        y_result1(i+1)=y_result1(i)+h*double(f(x_result1(i),y_result1(i)));
    end
    %printing the result for Euler
    fprintf('\tThe approximate solution using Euler method at x=%d for h=%0.2f is %f\n',xend,h,y_result1(end))
  
    %---------------------%
    %Program for RK4
    %function for RK4 equation solution
    f=@(x,y) -1.2*y+7.*exp(-0.3*x);
    %RK4 steps for h and x_end
    %Initial values
    x0=0;
    y0=3;
    %x end value
    xend=2;
    xn=x0:h:xend;
    % RK4 steps
    y_result2(1)=y0;
    x_result2(1)=x0;

        %Runge Kutta 4 iterations
        n=(xend-x0)/h;
        for i=1:n

            k0=h*f(x_result2(i),y_result2(i));
            k1=h*f(x_result2(i)+(1/2)*h,y_result2(i)+(1/2)*k0);
            k2=h*f(x_result2(i)+(1/2)*h,y_result2(i)+(1/2)*k1);
            k3=h*f(x_result2(i)+h,y_result2(i)+k2);
            x_result2(i+1)=x_result2(i)+h;
            y_result2(i+1)=double(y_result2(i)+(1/6)*(k0+2*k1+2*k2+k3));

        end
    %printing the result for RK4
    fprintf('\tThe approximate solution using RK4 method at x=%d for h=%0.2f is %f\n',xend,h,y_result2(end))

    %---------------------%
    %Finding exact solution using dsolve
    syms y(x)
    eqn=diff(y,x)==-1.2*y+7.*exp(-0.3*x);
    cond=y(0)==3;
    y_result3(x)=dsolve(eqn,cond);
    %printing exact solution
    fprintf('\tThe exact solution using dsolve at x=%d is %f\n',xend,y_result3(xend))
    fprintf('The expression for Exact solution\n')
    disp(y_result3(x))

    figure(it)
    subplot(2,1,1)
    hold on
    plot(x_result1,double(y_result3(x_result1)))
    plot(x_result1,y_result1)
    legend('Exact solution','Euler Solution')
  
    title(sprintf('Euler and Exact solution for h=%2.3f',h))
    xlabel('x')
    ylabel('y')
  
    subplot(2,1,2)
    hold on
    plot(x_result1,double(y_result3(x_result1)))
    plot(x_result2,y_result2)
    legend('Exact solution','RK4 Solution')

    title(sprintf('RK4 and Exact solution for h=%2.3f',h))
    xlabel('x')
    ylabel('y')
  
    if it==1
        %Printing the result for table
        fprintf('Table for h=0.1\n')
        fprintf('\n\tx,\ty(EM),\ty(RK),\tExact(Y),\tError-EM,\tError-RK,\n')
        for ii=1:length(y_result1)
            ext_sl=y_result3(x_result1(ii));
            err1=double((abs(y_result1(ii)-ext_sl)/ext_sl)*100);
            err2=double((abs(y_result2(ii)-ext_sl)/ext_sl)*100);
            fprintf('\t%2.2f,\t%2.5f,\t%2.5f,\t%2.5f,\t%2.2f,\t%2.5f,\t\n',...
            x_result1(ii),y_result1(ii),y_result2(ii),ext_sl,err1,err2)
        end
    end

end


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

Add a comment
Know the answer?
Add Answer to:
Runge-Kutta method R-K method is given by the following algorithm. o y(xo)- given k2-f(xi5.yi tk,...
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