Error: "Dimension argument must be a positive integer scalar within indexing range" using trapz
105 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jacqueline Rigatto
am 28 Feb. 2021
Kommentiert: Mathieu NOE
am 8 Mär. 2021
x_24= [0; 4.6958; 5.6732; 14.2002; 15.4490; 16.0151; 17.9994; 21.7987; 24.1082; 29.8818; 37.4162; 41.1113; 55.6853; 59.9431]
z_24=[ 6.1876; 7.1381; 7.2346; 4.4075; 4.4321; 4.2854; 3.9659; 3.4729; 3.1695; 2.2887; 1.2414; 0.8183; -0.0147; -0.2389]
NM_18=0.82;
MHWS_18=1.5-NM_18;
z_MHWS_24=z_24-MHWS_18;
MHWS_vector_18=repmat(MHWS_18,79,1);
figure(1)
plot (x_24,z_MHWS_24,'Yellow')
hold on
plot(MHWS_vector_18,'Blue')
hold off
x=x_24;
z=z_MHWS_24;
xi=5.67;
xf=14.20;
xRange = [xi,xf];
% find logical indices for range of interest
idl = x >= xRange(1) & x <= xRange(2);
% provide just values of interest to trapz
area_selected=trapz(x(idl),z(idl))
area_total=trapz(x,z)
The error that is happening is in the last two lines of the code above (from bottom to top).
0 Kommentare
Akzeptierte Antwort
Mathieu NOE
am 3 Mär. 2021
hello Jacqueline
with the given limits xi=5.67; xf=14.20; , idl contains only one valid x value , so trapz will throw an error, because it needs at least vector of 2 scalars (and you give only one value)
I guessed that you needed to include the 14.2002 value in your x vector , so simply make the second limit a bit higher : xf=14.21;
and then it's fine, you can do the trapz computation
results :
area_selected = 43.8377
area_total = 123.3377
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!