For the next two problems you may want to program the RK2(3) method into a calcu
ID: 3168130 • Letter: F
Question
For the next two problems you may want to program the RK2(3) method into a calculator or computer. Unfortunately, unlike the first extra credit assignment, this method isn't well suited to a spreadsheet because you keep having to test the step-size and possibly change it (perhaps more than once) at each step, though some students have used a spreadsheet successfully anyway using the RK2(3) method for y, 2xy, y(O) want. Then compute the exact answer for the initial value problem and find the percent error in the RK2(3) method 7. Approximate x2) 1 and a tolerance of T = .001. You can use whatever initial step-size youExplanation / Answer
%%%% Matlab code %%%%%
clc;
clear all;
close all;
format short
%%% restoredefaultpath %%%
%%%%% RK order 3
f=@(x,y) 2*x*y;
h=0.1; %%%% initial guess
tol=0.001;
y_exct=exp(4);
temp=1;
disp('step size error ');
while (temp)
y=0;
t=0:h:2;
y(1)=1;
for n=1:length(t)-1
k1=feval(f,t(n),y(n));
k2=feval(f,t(n)+h/2,y(n)+k1*h/2);
k3=feval(f,t(n)+h,y(n)-k1*h+2*k2*h);
y(n+1)=y(n)+h*(k1+4*k2+k3)/6;
end
err=abs(y(length(t))-y_exct);
if (err < tol )
temp=0;
fprintf('%f %f ',h,err);
break;
else
fprintf('%f %f ',h,err);
h=h-0.003;
end
end
OUTPUT:
step size error
0.100000 0.166143
0.097000 11.598197
0.094000 5.477925
0.091000 16.121217
0.088000 12.234849
0.085000 8.980535
0.082000 6.588403
0.079000 5.238233
0.076000 5.035417
0.073000 5.996241
0.070000 8.045537
0.067000 11.027215
0.064000 3.414718
0.061000 9.464401
0.058000 5.776747
0.055000 4.204750
0.052000 4.991686
0.049000 8.015299
0.046000 4.591167
0.043000 4.588323
0.040000 0.012408
0.037000 0.444592
0.034000 5.753133
0.031000 3.377016
0.028000 2.555368
0.025000 0.003148
0.022000 4.179434
0.019000 1.081135
0.016000 0.000844