Question

The differential equation : dy/dx = 2x -3y , has the initial conditions that y = 2 , at x = 0 Obt...

The differential equation : dy/dx = 2x -3y , has the initial conditions that y = 2 , at x = 0

Obtain a numerical solution for the differential equation, correct to 6 decimal place , using ,

  1. The Euler-Cauchy method
  2. The Runge-Kutta method in the range x = 0 (0.2) 1.0
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for Euler, Improved Euler and RK4 method clear all close all %Function for which solution have to do f-e (x,y) 2*end fprintf(ntThe solution using Runge Kutta 4 for h-s.2f at x(8.If) is 钍\n,h,x4(end ) , y4 ( end ) ) Plotting solution usinSolution ploty vs. Euler Method RK4 Method 1.8 1.6 14 1.2 0.8 0.6 0.4 0.2 .1 0 03 04 05 0608 0.9 Published with MATLAB R2018a

%%Matlab code for Euler and RK4 method
clear all
close all
%Function for which solution have to do
f=@(x,y) 2*x-3*y;
fprintf('\n\tEuler Method for step size h=%.2f\n',0.2)

%Euler method
    h=0.2;           % amount of intervals
    x=0;             % initial x
    y=2;             % initial y
    x_eval=1;        % at what point we have to evaluate
    n=(x_eval-x)/h; % Number of steps
    x2(1)=x;
    y2(1)=y;
    fprintf(' At x(%.2f) solution of y is %f\n',x,y)
    for i=1:n
        %Eular Steps
        m=double(f(x,y));
        x=x+h;
        y=y+h*m;
        x2(i+1)=x;
        y2(i+1)=y;
        fprintf(' At x(%.2f) solution of y is %f\n',x,y)
    end
  
    fprintf('\n\tThe solution using Euler Method for h=%.2f at x(%.1f) is %f\n',h,x2(end),y2(end))

  
    %RK4 method
    fprintf('\n\tRK4 Method for step size h=%.2f\n',0.2)
    x=0;             % initial x
    y=2;             % initial y
    x_eval=1;        % at what point we have to evaluate
    n=(x_eval-x)/h; % Number of steps
    x4(1)=x;
    y4(1)=y;
    fprintf(' At x(%.2f) solution of y is %f\n',x,y)
    for i=1:n
    %RK4 Steps
       k1=h*double(f(x,y));
       k2=h*double(f((x+h/2),(y+k1/2)));
       k3=h*double(f((x+h/2),(y+k2/2)));
       k4=h*double(f((x+h),(y+k3)));
       dx=(1/6)*(k1+2*k2+2*k3+k4);
       x=x+h;
       y=y+dx;
       x4(i+1)=x;
       y4(i+1)=y;
       fprintf(' At x(%.2f) solution of y is %f\n',x,y)

    end
  
    fprintf('\n\tThe solution using Runge Kutta 4 for h=%.2f at x(%.1f) is %f\n',h,x4(end),y4(end))
  
%%Plotting solution using Euler method
figure(1)
hold on
plot(x2,y2,'Linewidth',2)
plot(x4,y4,'Linewidth',2)
xlabel('x')
ylabel('y(x)')
title('Solution plot y vs. x')
legend('Euler Method','RK4 Method','Location','northeast')
grid on

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

Add a comment
Know the answer?
Add Answer to:
The differential equation : dy/dx = 2x -3y , has the initial conditions that y = 2 , at x = 0 Obt...
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