index must be a positive integer or logical.
Show older comments
clear all;
close all;
clc;
% solving the logistic growth model
% applying the forward Euler method
% definition of the right hand side
% using handles for a function
f=@(x) b*x*(100-x);
% defining the initial condition
k=0.45;
y=10;
n=50;
tmin=0;
tmax=25;
h=(tmax-tmin)/n;
t=[tmin zeros(0,n)];
y=[y zeros(0,n)];
for i=1:n
t(i+1)=t(i)+h;
y(i+1)=y(i)+h(y(i),k)
end
% visualization of results
T = table(t,y)
plot(t,y,'o')
[p,~,mu] = polyfit(T.t, T.y, 5);
finter = polyval(p,t,[],mu);
hold on
plot(t,finter)
hold off
I keep running this code and receiving
Attempted to access h(10,0.45); index must be a positive integer or logical.
Answers (2)
Star Strider
on 5 Mar 2016
0 votes
‘I keep running this code and receiving "Attempted to access h(10,0.45); index must be a positive integer or logical."’
The second index, 0.45 is not an integer.
3 Comments
Sam Menendez
on 6 Mar 2016
Walter Roberson
on 6 Mar 2016
It would be out of bounds if that dimension were empty.
Walter Roberson
on 6 Mar 2016
Your h is (tmax-tmin)/h which is a scalar . Why are you trying to index a scalar?
Chad Greene
on 5 Mar 2016
In the loop you try to evaluate
y(i+1)=y(i)+h(y(i),k)
but k equals 0.45, so that's the problem. Calling
h(10,0.45)
does not work because it's trying to access the 10th row of h and the 0.45th column of h.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!