index must be a positive integer or logical.
Ältere Kommentare anzeigen
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.
Antworten (2)
Star Strider
am 5 Mär. 2016
0 Stimmen
‘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 Kommentare
Sam Menendez
am 6 Mär. 2016
Walter Roberson
am 6 Mär. 2016
It would be out of bounds if that dimension were empty.
Walter Roberson
am 6 Mär. 2016
Your h is (tmax-tmin)/h which is a scalar . Why are you trying to index a scalar?
Chad Greene
am 5 Mär. 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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!