I am solving a differential equation (dy/dt = (-1/2*y*sin^2(t)) where y(0)=1) using different methods for my homework. For the Runge-Kutta method I keep getting the error: "Subscript indices must either be real positive integers or logicals."
I know that the y(0) = 1 initial condition is the problem, and that there just can't be a 0 in the parentheses, but I have know idea how to correct this? How can I put in the initial condition and make the program run. Below is the code that I am using if you need it.
clear;clc;h = input('What would you like the time step to be? >> ');x = 0:h:10*pi;y = zeros(1,length(x));y(0) = 1;F_xy = @(t,y) ((-1/2)*y*(sin(t)*sin(t)));for i = 1:(length(x)-1) k_1 = F_xy(x(i),y(i)); k_2 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_1)); k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2)); k_4 = F_xy((x(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;end
Best Answer