index must be a positive integer or logical.

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)

‘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

even with number 1 it then says its out of bounds
It would be out of bounds if that dimension were empty.
Your h is (tmax-tmin)/h which is a scalar . Why are you trying to index a scalar?

Sign in to comment.

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.

Asked:

on 5 Mar 2016

Edited:

on 6 Mar 2016

Community Treasure Hunt

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

Start Hunting!