///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0.
%%Matlab code for Euler, Improved Euler and RK4 method
clear all
close all
%Function for which solution have to do
f=@(t,y) (t.^3-t.^2-7.*t-5).*exp(t);
%Euler method
n=40;
% number of intervals
t=0;
% initial t
y=0;
% initial y
t_eval=4; % at what point
we have to evaluate
h=(t_eval-t)/n; % step size
t2(1)=t;
y2(1)=y;
for i=1:n
%Eular Steps
m=double(f(t,y));
t=t+h;
y=y+h*m;
t2(i+1)=t;
y2(i+1)=y;
end
fprintf('\n\tThe solution using Euler Method for
h=%.2f at x(%.1f) is %f\n',h,t2(end),y2(end))
%Midpoint method
t=0;
% initial t
y=0;
% initial x
t3(1)=t;
y3(1)=y;
for i=1:n
%midpoint steps
m1=double(f(t,y));
m2=double(f((t+h),(y+h*m1)));
y=y+double(h*((m1+m2)/2));
t=t+h;
y3(i+1)=y;
t3(i+1)=t;
end
fprintf('\n\tThe solution using improved Euler
Method for h=%.2f at x(%.1f) is %f\n',h,t3(end),y3(end))
%RK4 method
t=0;
% initial t
y=0;
% initial x
t4(1)=t;
y4(1)=y;
for i=1:n
%RK4 Steps
k1=h*double(f(t,y));
k2=h*double(f((t+h/2),(y+k1/2)));
k3=h*double(f((t+h/2),(y+k2/2)));
k4=h*double(f((t+h),(y+k3)));
dx=(1/6)*(k1+2*k2+2*k3+k4);
t=t+h;
y=y+dx;
t4(i+1)=t;
y4(i+1)=y;
end
fprintf('\n\tThe solution using Runge Kutta 4
for h=%.2f at x(%.1f) is %f\n',h,t4(end),y4(end))
%%Exact solution
syms y(t)
eqn = diff(y,t) == (t.^3-t.^2-7.*t-5).*exp(t);
cond = y(0) == 0;
ySol(t) = dsolve(eqn,cond);
y_ext=ySol(t4);
fprintf('Exact solution is y(t)=')
disp(ySol)
%%Plotting solution using Euler method
figure(1)
plot(t2,y2,'r',t3,y3,'b',t4,y4,'g',t4,y_ext,'m')
fprintf('\n\n\tError norm using Euler method is
%f.\n',norm(y_ext-y2))
fprintf('\tError norm using Modified Euler method is
%f.\n',norm(y_ext-y3))
fprintf('\tError norm using Runge Kutta method is
%f.\n',norm(y_ext-y4))
xlabel('t')
ylabel('y(t)')
title('Solution plot y(t) vs. t')
legend('Euler Method','Midpoint','RK4 Method','Exact
solution','Location','northeast')
grid on
err1=abs(y_ext-y2);
err2=abs(y_ext-y3);
err3=abs(y_ext-y4);
figure(2)
subplot(3,1,1)
plot(t2,err1)
title('Error plot for Euler method')
xlabel('t')
ylabel('error')
subplot(3,1,2)
plot(t2,err2)
title('Error plot for Midpoint method')
xlabel('t')
ylabel('error')
subplot(3,1,3)
plot(t2,err3)
title('Error plot for RK4 method')
xlabel('t')
ylabel('error')
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%
///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0. 3. Consider the...
(Matlab) Use Matlab's built-in Runge-Kutta function ode45 to solve the problem 1010y -xz +28x - y 3 on the interval t є [0, 100 with initial condition (z(0), y(0),z(0)) = (1,1,25), and plot the trajectory of the solution ((t), (t)) forte [0, 100 (Matlab) Use Matlab's built-in Runge-Kutta function ode45 to solve the problem 1010y -xz +28x - y 3 on the interval t є [0, 100 with initial condition (z(0), y(0),z(0)) = (1,1,25), and plot the trajectory of the...
Problem Thre: 125 points) Consider the following initial value problem: dy-2y+ t The y(0) -1 ea dt ical solution of the differential equation is: y(O)(2-2t+3e-2+1)y fr exoc the differential equation numerically over the interval 0 s i s 2.0 and a step size h At 0.5.A Apply the following Runge-Kutta methods for each of the step. (show your calculations) i. [0.0 0.5: Euler method ii. [0.5 1.0]: Heun method. ii. [1.0 1.5): Midpoint method. iv. [1.5 2.0): 4h RK method...
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...
Solve the ordinary differential equation below over the interval 0 sts 2s using two different methods: the Euler method and the second-order Runge-Kutta method (midpoint version). Begin by writing the state space representation of the equation. Use a time step of 1 s, and place a box around the values of x and x at t- 2 s obtained using each method. Show your work. 20d's +5dr +20x = 0 dt d x(0) = 1, x'(0) = 1 Solve the...
1 with 5. Consider the differential equation y, f(x,y) with initial condition y(zo) = yo. Show that, zi = zo +h, the solution at x1 can be obtained with an er ror O(h3) by the formula In other words, this formula describes a Runge-Kutta method of order 2. with 5. Consider the differential equation y, f(x,y) with initial condition y(zo) = yo. Show that, zi = zo +h, the solution at x1 can be obtained with an er ror O(h3)...
1 st s2, y(1)1 The exact solution is given by yo) - = . 1+Int Write a MATLAB code to approximate the solution of the IVP using Midpoint (RK2) and Modified Euler methods when h [0.5 0.1 0.0s 0.01 0.005 0.001]. A) Find the vector w mid and w mod that approximates the solution of the IVP for different values of h. B) Plot the step-size h versus the relative error of both in the same figure using the LOGLOG...
Complete using MatLab 1. Consider the following initial value problem 3t2-y, y(0) = 1 Using Euler's Method and the second order Runge-Kutta method, for t E [0, 1] with a step size of h 0.05, approximate the solution to the initial value problem. Plot the true solution and the approximate solutions on the same figure. Be sure to label your axis and include an a. appropriate legend b. Verify that the analytic solution to the differential equation is given by...
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 , The Euler-Cauchy method The Runge-Kutta method in the range x = 0 (0.2) 1.0
Numerical Methods Consider the following IVP dy=0.01(70-y)(50-y), with y(0)-0 (a) [10 marks Use the Runge-Kutta method of order four to obtain an approximate solution to the ODE at the points t-0.5 and t1 with a step sizeh 0.5. b) [8 marks Find the exact solution analytically. (c) 7 marks] Use MATLAB to plot the graph of the true and approximate solutions in one figure over the interval [.201. Display graphically the true errors after each steps of calculations. Consider the...
(1) Solve the differential equation y 2xy, y(1)= 1 analytically. Plot the solution curve for the interval x 1 to 2 (see both MS word and Excel templates). 3 pts (2) On the same graph, plot the solution curve for the differential equation using Euler's method. 5pts (3) On the same graph, plot the solution curve for the differential equation using improved Euler's method. 5pts (4) On the same graph, plot the solution curve for the differential equation using Runge-Kutta...