MATLAB question, please answer with vaild code. Please answer all parts of the q
ID: 3349669 • Letter: M
Question
MATLAB question, please answer with vaild code. Please answer all parts of the question. Thank you very much.
Consider the IVP y 3y 3t, y(0) 1 (solution y(t) e* - t- 1/3). 4p3tt- 1/3 a) Approximate y(1) using Euler's Method with h 1, 0.1, and 0.01. Compute the relative b) Approximate y(1) using Heun's Method with h = 1,0,1, and 0.01. Compute the relative c) Approximate y(1) using Runga-Kutta's 4-step Method with h = 1, 0.1, and 0.01. Com- error for each approximation. error for each approximation pute the relative error for each approximation.Explanation / Answer
we will use 3 different function files to implement 3 methods our files will solveEuler.m, solveHeuns.m, solveRungaKutta.m.
below is the content of every file.
solveEuler.m
---------------------------------------------------
function y = solveEuler( x,y,h,xf,fun )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
while x<xf
y = y+h*fun(x,y);
x = x+h;
end
end
------------------------------------------
solveHeuns.m
------------------------------------------
function y = solveHeuns(x,y,h,xf,fun)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
while x<xf
y = y + h*(fun(x,y)+fun(x+h,y+h*fun(x,y)))/2;
x=x+h;
end
end
-------------------------------------------
solveRungaKutta.m
-------------------------------------------
function y = solveRungaKutta4(x,y,h,xf,fun)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
while x<xf
k1=h*fun(x,y);
k2=h*fun(x+h/2,y+k1/2);
k3=h*fun(x+h/2,y+k2/2);
k4=h*fun(x+h,y+k3);
k=(k1+2*k2+2*k3+k4)/6;
y=y+k;
x=x+h;
end
end
-----------------------------------------------
and we will make one main file to calculate y(1) utilizing all of the above methods. below is content of main.m file. run this file.
-----------------------------------------------
h = [1 0.1 0.01];
f1 = 4*exp(3)/3-1-1/3;
disp('Euler''s method');
for i=1:3
hb=h(i);
val = solveEuler(0,1,h(i),1,@(x,y) 3*y+3*x);
error = abs(val-f1)*100/f1;
hb
val
error
end
disp('Heun''s method');
for i=1:3
hb=h(i);
val = solveHeuns(0,1,h(i),1,@(x,y) 3*y+3*x);
error = abs(val-f1)*100/f1;
hb
val
error
end
disp('Runga Kutta 4 step method');
for i=1:3
hb=h(i);
val = solveRungaKutta4(0,1,h(i),1,@(x,y) 3*y+3*x);
error = abs(val-f1)*100/f1;
hb
val
error
end
------------------------------------------
Note : 1) use smart indent to structure the code.
2) make sure that path to function files is added in matlab.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.