Error: "Dimension argument must be a positive integer scalar within indexing range" using trapz

105 次查看(过去 30 天)
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).

采纳的回答

Mathieu NOE
Mathieu NOE 2021-3-3
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

更多回答(0 个)

类别

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