Initial condition of y(0) = 1

7 views (last 30 days)
Joseph Mayer
Joseph Mayer on 29 Mar 2016
Answered: Torsten on 30 Mar 2016
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

Accepted Answer

Torsten
Torsten on 30 Mar 2016
Just put it
y(1) = 1.0;
The '0' does not refer to the spatial x-value, but is an array index. Keep in mind that x(1)=0, and y(1) is the solution value at x(1).
Best wishes
Torsten.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!