Currently trying to create a four order Runge Kutta script to help solve a forced linear oscillator equation x¨(t) + bx˙(t) + x(t) = cos(Ω0t) I keep getting the error Index exceeds matric dimensions. Error in the Function and also an error in the first order of runge kutta line. Any help will be appreciated thanks.
% Define Parameters
h=0.1;Omega=0.5;Tend=50;nsteps=Tend/h;m = 1.0;b = 0.0;k = 1.0;F = 0.0;T=1;x¨(t) + bx˙(t) + x(t) = cos(Ω0t)%Inital conditions
y(1)=1;y(2)=500;t(1)=1;%Define Function Handle
f= @(t,y) [y(2); cos(Omega*T)-b*y(2)-t(1)];%Function
for i=1:ceil(Tend/h) k1=f(t(i), y(i) ); k2=f(t(i)+0.5*h,y(i)+0.5*k1*h); k3=f(t(i)+0.5*h,y(i)+0.5*k2*h); k4=f(t(i) +h,y(i)+ k3*h); y(i+1)=y(i)+h/6*(k1+2*k2+2*k3+k4);end%Plot Results
plot(t,y)tlabel('t')ylabel('y')end
Best Answer