Error in using Trapz
Mostra commenti meno recenti
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)

2 Commenti
Walter Roberson
il 9 Feb 2019
could you provide your program and data for testing ?
Mohammad Qasem
il 9 Feb 2019
Modificato: Mohammad Qasem
il 9 Feb 2019
Risposte (1)
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
il 10 Feb 2019
Walter Roberson
il 10 Feb 2019
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
Categorie
Scopri di più su Numerical Integration and Differentiation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!