Performing integral trapz-error invalid permutation index

3 次查看(过去 30 天)
I have the following code
Vg = linspace(0, 5, 100);
for t = 1:length(Vg);
Fun(t) =((Vg(t)^2)/(exp(Vg(t)/(k*Ts))-1));
IntNs(t) = trapz(Vg,Fun(t));
end
Every time I attempt to run this I receive the error message "Order contains an invalid permutation index", Im not quite sure what is going awry as I have run an almost identical code in other scripts. Could anyone guide me to the problem and how to correct it.
Thanks
-Jarett

回答(2 个)

Roger Stafford
Roger Stafford 2015-2-24
Apparently you wish to find the integral of the function 'Fun' with respect to 'Vg'. If so, the line
IntNs(t) = trapz(Vg,Fun(t));
should be located after you have exited the for-loop:
Vg = linspace(0, 5, 100);
for t = 1:length(Vg);
Fun(t) =((Vg(t)^2)/(exp(Vg(t)/(k*Ts))-1));
end
IntNs = trapz(Vg,Fun);
As you have it, you are trying to find the integral of the scalar Fun(t) with respect to 'Vg' which 'trapz' doesn't like. See the documentation at:
http://www.mathworks.com/help/matlab/ref/trapz.html
Note where it says, "Q = trapz(X,Y) integrates Y with spacing increment X. By default, trapz operates on the first dimension of Y whose size does not equal 1. length(X) must be equal to the size of this dimension." That is obviously not true in your use of 'trapz', since Fun(t) is a scalar.

Lazaros Christoforidis
Hey
Im kinda late, but I think I will help others
c='number ';
basically try IntNs=cumtrapz(Vg,Fun)+c;
where c: u choose it

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by