I need to set up a Runge Kutta solver for water leaving a bucket through a certain diameter nozzle as function of time, it says array indices must be positive integers/log val

3 views (last 30 days)
Christiaan Janeke on 12 Nov 2021
Edited: Alan Stevens on 13 Nov 2021
rnoz=1.049/2
rtank=120/2
Dnoz=1.049
Dtank=120
g=386.088
t=10
h=60
y=(0:200)
N:50
y(1)=240
F_y =-(((rnoz^2)/(rtank^2))*sqrt((2*g)/(1-(Dnoz/Dtank)^4)))*sqrt(y(1))*h+y(1)
for i=0:60:200
k_1 = F_y(y(i))
k_2 = F_y(y(i)+0.5*h*k_1)
k_3 = F_y(y(i)+0.5*h*k_2)
k_4 = F_y(y(i)+k_3*h)
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h
end

Alan Stevens on 12 Nov 2021
Indices in Matlab start at 1, not 0, so you need
for i=1:60:200
not
for i=0:60:200
Alan Stevens on 13 Nov 2021
You probably want to define your function F_y as
F_y = @(y) -(((rnoz^2)/(rtank^2))*sqrt((2*g)/(1-(Dnoz/Dtank)^4)))*sqrt(y)*h+y;
Also note that if you increment i by 60 each iteration, then your k values still won't make sense (they will be calculated incorrectly). Just let
i = 1: ceil(200/h);
or something like that. (I suspect h = 60 is far too large a step size!).

R2021a

Community Treasure Hunt

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

Start Hunting!