Question 2 [8 marks] The following initial value problem of ODE has an exact sol
ID: 3283283 • Letter: Q
Question
Question 2 [8 marks] The following initial value problem of ODE has an exact solution. To understand how accurate different numeric methods can be used to approximate the exact solutions, 1) use the Euler's method, improved Euler's method and the fourth-order Runge-Kutta method respectively to find the numeric solutions to the given initial value problem of ODE. 2) Determine the absolute error of each numeric solution relative to the corresponding exact solution. Calculate the solutions with a step size of 0.1 and the nodes xi=0.1, X2-0.2 x3-0.3, x4 = 0.4 and xs = 0.5 . Keep 4 decimal digits in your calculations. dy=1-4x + 2y dx y(0)-1Explanation / Answer
MATLAB CODE IS ATTCHED
clc;
clear all;
%% EXACT SOLUTION
x=[0:0.1:0.5];
disp(' Exact Solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5')
y_exact(1:length(x))=0.5+2*x(1:length(x))+0.5*exp(2*x(1:length(x)))
%% EXPLICIT EULER METHOD
x=[0:0.1:0.5];
y(1)=1; % initial value condition
for i=1:length(x)
der_y(i)=1-4*x(i)+2*y(i);
y(i+1)=y(i)+0.1*der_y(i);
end;
disp(' Solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5 in Euler Method ')
y(1:length(x)) % the solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5
disp(' Absolute Error at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5 in Euler Method ')
abs(y(1:length(x))-y_exact(1:length(x)))
%% IMPROVED EULER METHOD
for i=1:length(x)
x(7)=0.6;
der1_y(i)=1-4*x(i)+2*y(i);
y(i+1)=y(i)+0.1*der1_y(i);
der2_y(i)=1-4*x(i+1)+2*y(i+1);
y(i+1)=y(i)+0.1*0.5*(der1_y(i)+der2_y(i));
end;
x=[0:0.1:0.5];
disp(' Solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5 in Improved Euler Method ')
y(1:length(x)) % the solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5
disp(' Absolute Error at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5 in Improved Euler Method ')
abs(y(1:length(x))-y_exact(1:length(x)))
%% 4TH ORDER RK METHOD
x=[0:0.1:0.5];
y(1)=1; % initial value condition
for i=1:length(x)
der_y(i)=1-4*x(i)+2*y(i);
k1=0.1*der_y(i);
k2=0.1*(1-4*(x(i)+0.1/2)+2*(y(i)+k1/2));
k3=0.1*(1-4*(x(i)+0.1/2)+2*(y(i)+k2/2));
k4=0.1*(1-4*(x(i)+0.1)+2*(y(i)+k3));
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4);
end;
disp(' Solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5 in RK-4 Method ')
y(1:length(x)) % the solutions at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5
disp(' Absolute Error at x=0,x=0.1,x=0.2,x=0.3,x=0.4,x=0.5 in RK-4 Method ')
abs(y(1:length(x))-y_exact(1:length(x)))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.