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
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

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?

댓글을 달려면 로그인하십시오.

Chad Greene
Chad Greene 2016년 3월 5일

0 개 추천

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에 대해 자세히 알아보기

질문:

2016년 3월 5일

편집:

2016년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by