Hey everyone I need to construct a matlab program that implements the fourth ord
ID: 2971014 • Letter: H
Question
Hey everyone I need to construct a matlab program that implements the fourth order Runge-kutta
it should be able to implement the fourth order runge-kutta for ivps of the form
y^1=f(y,t)
y(t0)=y0
It should have a data function at the end of the .m file that allows users to input values for
f(y,t),t0,y0,N and h, h being step size and N the number of steps to reach endpoint
the program should output the approximations of y(ti)(i.e.{w0,w1...wn})
should also include a graphical approximation to the solution.
the program should be coded with
y^1=2/t*y+t^2e^t
y(1)=0
h=.1
N=10
*dont think its a long code, but Im matlab challenged
Explanation / Answer
clc; % Clears the screen
clear all;
h=0.1; %step size
N=10; %no of points
l=N*h;
t(1)=0.1;
for i=1:1:N-1 %calculating t-axis
t(i+1)=(i+1)*h;
end;
y = zeros(1,length(t));
y(1) = 0; % initial condition
F_ty = @(t,y) (((2/t)*y)+(t^2)*(exp(t))); % change the function as you desire
for i=1:(length(t)) % calculation loop
k_1 = F_ty(t(i),y(i));
k_2 = F_ty(t(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_ty((t(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_ty((t(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; % main equation
fprintf('t(%d) = %f -> y(%d) = %f ',i,t(i),i,y(i));%displaying the values
end