Precision and indices must be positive integers

조회 수: 1 (최근 30일)
Miraboreasu
Miraboreasu 2022년 12월 16일
편집: Miraboreasu 2022년 12월 16일
Hello, three questions for the following code
  1. If I don't use round() for timerise, timesta, timedecay, it will give a warn: Warning: Integer operands are required for colon operator when used as index. It is weird to use round, any better suggestions?
  2. My ouputpoint is integer from workspace, don't know why it has error: Array indices must be positive integers or logical values.(solved)
  3. The p value from workspace, looks wierd, some are accrated, some are not
% clc
clear
P0 = 1000.0;
timestep=2.0e-8;
t0 = [10.0e-6; 100.0e-6; 500.0e-6];
td = t0;
ts = 1.0e-7;
timeset=[t0(1), ts, td(1)];
time=sum(timeset);
t=0:timestep:time;
t=t(1:length(t));
timerise = t(1:round(t0(1)/timestep)+1);
timesta = t(round(t0(1)/timestep+2):(round(t0(1)/timestep+2)+round(ts/timestep-1)));
timedecay= t((round(t0(1)/timestep+2)+round(ts/timestep)):end);
ouputpoint = [1 round(t0(1)/timestep)+1, (round(t0(1)/timestep+2):(round(t0(1)/timestep+2)+ts/timestep-1)), (round(t0(1)/timestep+2)+ts/timestep), length(t)];
P=func(timerise,timesta,timedecay,t0(1),P0,ts,td(1),t(end));
P(ouputpoint);
function P=func(timer,timest,timed,t0,Ppeak,ts,td,t)
kr = Ppeak./t0;
pr = kr.*timer;
ps = Ppeak*ones(1,length(timest));
kd = (Ppeak-0)/(timest(end) - t);
b = -kd*t;
pd = kd*timed+b;
P=[pr ps pd];
end

답변 (3개)

Askic V
Askic V 2022년 12월 16일
편집: Askic V 2022년 12월 16일
The problem is in the line:
kd = (Ppeak-0)/(times(end) - t);
times is a built in Matlab function for element wise multiplication of arrays.
The keyword end can be used only with arrays to indicate last index of and the array
  댓글 수: 3
Askic V
Askic V 2022년 12월 16일
편집: Askic V 2022년 12월 16일
It is because your array ouputpoint contains the following:
ouputpoint = 0 501 502 503 504 505 506 507 1006
First, Matlab cannnot index array with index 0, in Matlab index starts with 1. Also last element is 1006, so are you sure your P array has 1006th element?
If you make this modification, you'll see it will work:
P(ouputpoint(2:end-1))
Miraboreasu
Miraboreasu 2022년 12월 16일
Thanks, question 2 is fixed, can you take a look at 1 and 3?

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


Torsten
Torsten 2022년 12월 16일
이동: Torsten 2022년 12월 16일
ouputpoint(1) = 0 (see above).
So you try to access P(0) which is not possible in MATLAB since array indices start at 1, not at 0.
And P(1006) does not exist since the array has only 507 elements.
  댓글 수: 1
Miraboreasu
Miraboreasu 2022년 12월 16일
Thanks, question 2 is fixed, can you take a look at 1 and 3?

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


John D'Errico
John D'Errico 2022년 12월 16일
Why did this line gather an error?
kd = (Ppeak-0)/(times(end) - t);
The end operator must be used within an array index expression.
Did you define an array called times? I don't see one, and neither does MATLAB. In that case, what does MATLAB do? It assumes you want to use the FUNCTION named times. No variable? It must be a function.
Yes, you have a variable named time. But should MATLAB know what you want to do? Computers tend to be rather literal things. They do as they are told to do. And would you really want your computer to start to re-write your code for you? Maybe you would, I don't know. But I'd not totally trust a computer program to get something complicated right.
As far as being an integer from your workspace, is this number an integer?
X = 1 + eps
X = 1.0000
It looks fairly integer-ish. And sometimes if something likes like a duck, people believe is a duck.
X == 1
ans = logical
0
But it is not.
  댓글 수: 1
Miraboreasu
Miraboreasu 2022년 12월 16일
Thanks, question 2 is fixed, can you take a look at 1 and 3?

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by