Index Exceeds number of array elements (1)

function ydot = fossil_fules(t,y)
p=y(1);os=y(2);od=y(3);as=y(4);ad=y(5);
r= [0.0,0.0,1.0,4.0,5.0,8.0,10.0,10.5,10.0,8.0,3.5,2.0,0.0,0.0];
yr= [1000,1850,1950,1980,2000,2050,2080,2100,2120,2150,2225,2300,2500,5000];
f = pchip(yr,r);
d = 8.65;
m1 =4.95*10^2;
m2 =4.95*10^-2;
vs =0.12;
vd =1.23;
w =10^-3;
k1 =2.19*10^-4;
k2 =6.12*10^-5;
k3 =0.997148;
k4 =6.79*10^-2;
hs = (os-(os^2 - k3*as*(2*os-as))^0.5) / k3;
cs = (as-hs)/2;
ps = k4*((hs^2)/cs);
ydot = [(ps-p/d)+f(t)/m1 ; 1/vs*((od-os)*w-k1-(ps-p/d)*m2) ; 1/vd *(k1-(od-os)*w) ; 1/vs *((ad-as)*w-k2) ; 1/vd *(k2-(ad-as)*w)];
end
Error is in line 3 "p=y(1);os=y(2);od=y(3);as=y(4);ad=y(5);"

 Accepted Answer

I suspect your initial conditions vector in your ode solver call only has one element. It should be a 5-element vector.
You would have to post that so we could see it.

4 Comments

tspan = [1000,5000];
yzero = [1.00;2.01;2.23;2.20;2.26];
[t,y] = ode45(@fossil_fules,tspan,yzero)
This is the ode solver im using
I get a different error:
Index exceeds the number of array elements (1).
Error in fossil_fules (line 19)
ydot = [(ps-p/d)+f(t)/m1 ; 1/vs*((od-os)*w-k1-(ps-p/d)*m2) ; 1/vd *(k1-(od-os)*w) ; 1/vs
*((ad-as)*w-k2) ; 1/vd *(k2-(ad-as)*w)];
The probleml seems to be that ‘f’ is (1x1) and ‘t’ is 1000.
I suspect that you’re using pchip incorrectly.
If I create an anonymous function with your pchip call:
f = @(tq) pchip(yr,r,tq);
your code runs without error.
It works! thank you so much
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!