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.
回答 (2 件)
Star Strider
2016 年 3 月 5 日
0 投票
‘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 件のコメント
Sam Menendez
2016 年 3 月 6 日
Walter Roberson
2016 年 3 月 6 日
It would be out of bounds if that dimension were empty.
Walter Roberson
2016 年 3 月 6 日
Your h is (tmax-tmin)/h which is a scalar . Why are you trying to index a scalar?
Chad Greene
2016 年 3 月 5 日
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.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!