In: Advanced Math
Please make a little adjust for the following script then make it work for
Runge Kutta method
Consider the initial value problem
du/dt=t^5 u(0)=0
0<=t<=1
Q1. how we change the below script to make it to solve the above IVP and the answer should be near 4
here is the script
f =@(t ,y)(t^5); % define f by f(t,y)=y^5
for k =1:10
[ tlist , ylist ]= RKfour174 (f ,0 ,1 ,0, 2^k);
error1 (k)= max ( abs ( ylist - ( tlist )));
hlist (k) =1/2^k;
end
x= log ( hlist );
y= log ( error1 );
polyfit (x ,y ,1)
function [tlist,ylist] =RungMethod(f,t0,tfinal ,y0,N) what is the input for N, n is a stepsize
clear all
close all
%function for which Solution have to do
fun=@(t,y) t.^5;
%exact solution
y_ext=@(t) t.^6/6;
%initial guess
tinit=0; yinit=0;
tend=1;
for k=1:10
[t_rk,y_rk]=RK4(fun,tinit,yinit,tend,2^k);
y_exact=double(y_ext(t_rk));
error(k)=norm(y_exact-y_rk);
hlist(k)=1./2.^k;
fprintf('\tFor n=%d value of u(%2.2f) is
%f\n',2^k,t_rk(end),y_rk(end))
end
x=log(hlist);
y=log(error);
plot(x,y)
ylabel('log(error)')
xlabel('log(hlist)')
title('error vs. step size plot')
p=polyfit(x,y,1);
fprintf('\npolyfit values are p=%f and %f\n',p(1),p(2))
%%Matlab function for Runge Kutta Method
function [t_rk,y_rk]=RK4(f,tinit,yinit,tend,n)
%Euler method
% h amount of intervals
t=tinit; % initial
t
y=yinit; % initial
y
t_eval=tend; % at what
point we have to evaluate
h=(t_eval-t)/n; % Number of steps
t_rk(1)=t;
y_rk(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)));
dy=(1/6)*(k1+2*k2+2*k3+k4);
t=t+h;
y=y+dy;
t_rk(i+1)=t;
y_rk(i+1)=y;
end
end
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%