Question

MATLAB HELP 3. Consider the equation y′ = y2 − 3x, where y(0) = 1.   USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS PROVIDED IN THE PICTURES

a. Use a Euler approximation with a step size of 0.25 to approximate y(2).

b. Use a Runge-Kutta approximation with a step size of 0.25 to approximate y(2).

c. Graph both approximation functions in the same window as a slope field for the differential equation.

d. Find a formula for the actual solution (not by hand). Which is most helpful in understanding the solution: the explicit formula, or the numerical approximation? Explain.

e. Use the explicit formula to compute the exact value of y(2). How close are the Euler and Runge-Kutta approximations?

Please help me form the MATLAB code needed for this question. USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS PROVIDED

3. Consider the equation y =y®-3x, where y(0) = 1. a. Use an Euler approximation with a step size of 0.25 to approximate y(2Euler approximation function [x,y] = EulerApprox(dydx , xrange,yo,h) xxrange (1):h:xrange (2) x transpose(x): y-zeros (size (Euler approximation function [x,y] = EulerApprox(dydx , xrange,yo,h) xxrange (1):h:xrange (2) x transpose(x): y-zeros (size (

3. Consider the equation y' =y®-3x, where y(0) = 1. a. Use an Euler approximation with a step size of 0.25 to approximate y(2) b. Use a Runge-Kutta approximation with a step size of 0.25 to approximate y(2). c. Graph both approximation functions in the same window as a slope field for the differential equation. d. Find a formula for the actual solution (not by hand). Which is most helpful in understanding the solution: the explicit formula, or the numerical approximation? Explain. e. Use the explicit formula to compute the exact value of y(2). How close are the Euler and Runge-Kutta approximations?
Euler approximation function [x,y] = EulerApprox(dydx , xrange,yo,h) xxrange (1):h:xrange (2) x transpose(x): y-zeros (size ( while(i
1 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE TTHERE TO HELP YOU..ALL THE BEST..

AS FOR GIVEN DATA.

%%Matlab code for Euler's forward and Rk4 method
clear all
close all
%Answering question a.
%---------------------%
%Program for Euler
%function for Euler equation solution
syms x y
f(x,y)=y^2-3*x;
%Euler steps for h and x_end
%Initial values
x0=0;
y0=1;
%step size
h=0.25;
%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))

%Answering question b.
%---------------------%
%Program for RK4
%function for RK4 equation solution
syms x y
f(x,y)=y^2-3*x;
%RK4 steps for h and x_end
%Initial values
x0=0;
y0=1;
%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))

%Answering question c.
%---------------------%
hold on
%plotting the solutions for Euler and RK4
plot(x_result1,y_result1,'b')
plot(x_result2,y_result2,'g')
title('Solution plot for differential equation')
xlabel('x')
ylabel('y')
%slope field calculation
funn=@(x,y) y.^2-3.*x;
xx=0:.2:2; yy=-3:.2:3;
[X,Y]=meshgrid(xx,yy);
slopes=funn(X,Y);
dy=slopes./sqrt(1+slopes.^2);
dx=ones(size(dy))./sqrt(1+slopes.^2);
h=quiver(X,Y,dx,dy,.5,'color','r');
set(h,'maxheadsize',0.1)
legend('Euler solution','RK4 solution','Slope field','location','best')

%Answering question d.
%---------------------%
%Finding exact solution using dsolve
syms y(x)
eqn=diff(y,x)==y^2-3*x;
cond=y(0)==1;
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))
fprintf('Numerical solution are better to understand than exact solution.\n')
%Answering question e.
%---------------------%
err1=double((abs(y_result3(xend)-y_result1(end))/y_result3(xend))*100);
err2=double((abs(y_result3(xend)-y_result2(end))/y_result3(xend))*100);
fprintf('For step size h=%2.2f.\n',h)
fprintf('\tThe percentage relative error for solving differential eqn using Euler method is %f\n',abs(err1))
fprintf('\tThe percentage relative error for solving differential eqn using RK4 method is %f\n',abs(err2))

I HOPE YOU UNDERSTAND..

PLS RATE THUMBS UP..ITS HELPS ME ALOT..

THANK YOU...!!

Add a comment
Know the answer?
Add Answer to:
MATLAB HELP 3. Consider the equation y′ = y2 − 3x, where y(0) = 1.   USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS...
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