.Use the improved Euler (IE)\'s method and Runge-Kutta (RK) method to solve the
ID: 2074744 • Letter: #
Question
.Use the improved Euler (IE)'s method and Runge-Kutta (RK) method to solve the following equation and plot the numerical solutions. DE = x + y, y(0) = 1 Use the step size 1 as the interval of x = [0,5]. Compare the performances of the two methods. Use the MATLAB code, introduced on the handout during . lecture. - You can also see the pages 2 and 3 in this cover sheet. . Submit your matlab codes (including main code and user- defined function files) Submit your plots in a pdf file. The plots should show the solutions using IE and RK methods and the exact solution. yexact (z) = 2 exp(x)-x-1Explanation / Answer
The matlab code includes four files. Additional to the methods specified in the question, it also implements normal euler method for comparision.
main.m
%Main function to caluculate solution of a differential equation using
clc
close all
clear all
F = @(y,u) u+y; % Anonymous function to calculate output of the differential equation
U = @(x) x;% Anonymous function to calculate Input of the differential equation
x0 = 0; % Start time
xfinal = 5; % Stop time
h =1; % step size
y0 = 1; % Initil value of the output
[xe,ye] = EulerResult_x(F,U,x0,xfinal,h,y0); % Calculate output using eulers method
[xie,yie] = ImpEulerResult_x(F,U,x0,xfinal,h,y0); % Calculate output using eulers method
[xr,yr] = Runge_kutta_x (F,U,x0,xfinal,h,y0); % Calculate output using eulers method
exact_sol = 2*exp(xie)-xie-1;
plot(xie,exact_sol);
hold on;
plot(xe,ye)
plot(xie,yie)
plot(xr,yr)
legend('Exact solution','Eulers method','Improved Eulers method','Runge kutta method')
xlabel('x');ylabel('y');
EulerResult_x.m
% Function to calculate output in next input step using Euler method.
% Outputs
% x = input vector with sampling input steps
% y = solution of the differential equation
% Inputs
% F = f(y,x) Anonymous function to calculate output of the differential equation
% U = u(x). Anonymous function to calculate Input of the differential equation
% x0 = Initial input
% xfinal = final input
% h = step size
% y0 = Initil value of the output
function [x,y] = EulerResult_x (F,U,x0,xfinal,h,y0)
x=x0:h:xfinal; % input vector
y(1)= y0; % Initial y output
for i=1:length(x)-1
u(i) = U(x(i)); % Calclation of input value
y(i+1) = y(i)+h*F(y(i),u(i)); % Calculation of output value
end
ImpEulerResult_x.m
% Function to calculate output in next input step using Improved Euler method.
% Outputs
% x = input vector with sampling input steps
% y = solution of the differential equation
% Inputs
% F = f(y,x) Anonymous function to calculate output of the differential equation
% U = u(x). Anonymous function to calculate Input of the differential equation
% x0 = Initial input
% xfinal = final input
% h = step size
% y0 = Initil value of the output
function [x,y] = ImpEulerResult (F,U,x0,xfinal,h,y0)
x=x0:h:xfinal; % input vector
y(1)= y0; % Initial y output
for i=1:length(x)-1
u(i) = U(x(i)); % Calclation of input value
u(i+1) = U(x(i+1));
y(i+1) = y(i)+(F(y(i),u(i))+F(y(i)+h*F(y(i),u(i)),u(i+1)))*h/2; % Calculation of output value
end
Runge_kutta.m
% It calculates ODE using Runge-Kutta 4th order method
% t = time vector with sampling time steps
% y = solution of the differential equation
% Inputs
% F = f(y,t) Anonymous function to calculate output of the differential equation
% U = u(t). Anonymous function to calculate Input of the differential equation
% x0 = Initial input
% xfinal = final input
% h = step size
% y0 = Initil value of the output
function [x,y] = Runge_kutta_x (F,U,x0,xfinal,h,y0)
x = x0:h:xfinal; % Calculates upto y(3)
y = zeros(1,length(x));
y(1) = y0; % initial condition
for i=1:(length(x)-1)
s_1 = F(x(i),y(i));
s_2 = F(x(i)+0.5*h,y(i)+0.5*h*s_1);
s_3 = F((x(i)+0.5*h),(y(i)+0.5*h*s_2));
s_4 = F((x(i)+h),(y(i)+s_3*h));
y(i+1) = y(i) + (1/6)*(s_1+2*s_2+2*s_3+s_4)*h; % main equation
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.