Error in using Trapz

Please could you help me because when I use traps to get the area under the curve I had an error as follow:
Subscript indices must either be real positive integers or logicals.
Error in ipermute (line 22)
inverseorder(order) = 1:numel(order); % Inverse permutation order
Error in trapz (line 71)
if ~isempty(perm), z = ipermute(z,perm); end
Error in Untitled (line 136)
area=trapz(xx_star,hhh)
my code as follow:
sym xx_star;
for k=1:length(x2)
xx_star(k)=x2(k)
.
.
.
.
Nu(k)=1/yy1(1); %% it's constant
hhh(k)=(0.05*Nu(k))./(xx_star(k));
area=trapz(xx_star,hhh)
untitled.jpg

2 Commenti

Walter Roberson
Walter Roberson il 9 Feb 2019
could you provide your program and data for testing ?
Mohammad Qasem
Mohammad Qasem il 9 Feb 2019
Modificato: Mohammad Qasem il 9 Feb 2019
I sent it to you

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 10 Feb 2019
Your code has
hhh(k)=(0.05*Nu(k))./(xx_star(k));
area=trapz(xx_star,hhh)
On the first iteration, k = 1, your xx_star is a scalar, and your hhh is a scalar. So you are calling
trapz(SCALAR, SCALAR)
it happens that when you call trapz with a scalar second parameter, instead of treating the call as being
trapz(X, Y)
it treats it as being a request to
trapz(Y, DIM)
But your hhh value does not happen to look like a valid dimension, so an error happens.
It is likely that your call to trapz() should wait until after you have completely filled in xx_star and hhh.
Note: earlier in the file you have
while (yy2<0)
but your yy2 is a vector, not a scalar. In MATLAB when you have if or while with a condition, the condition is only considered true as long as all elements in the expression are non-zero (or true). So your while would stop running as soon as any value in yy2 become >= 0 -- and likewise, your loop would never end if your yy2 did happen to turn out to be all negative.... which happens to be the case. If the trapz() had not caused an error you would have an infinite loop.

2 Commenti

Mohammad Qasem
Mohammad Qasem il 10 Feb 2019
Thank you very much
What you recommend me to do ... to get the result of the area
I suspect you should be switching from
while (yy2<0)
for k = ...
end
end
to
for k = ...
...
yy2(k) = ...
if yy2(k) >= 0;
break
end
end
if k == 1
%not enough data to trapz so respond appropriately
else
area = trapz(xx_star, hhh);
end

Accedi per commentare.

Richiesto:

il 9 Feb 2019

Commentato:

il 10 Feb 2019

Community Treasure Hunt

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

Start Hunting!

Translated by