Error using trapz (line 47) Dimension argument must be a positive integer scalar within indexing range.

9 views (last 30 days)
function Length = LengthOfQuadraticSplines(A,X)
Length = 0.0;
for i=1:length(X)-1
syms x;
drivative = diff(A(i,1)*x^2+A(i,2)*x+A(i,3));
Tx = linspace(X(1),X(2),3);
Ty = sqrt(1+(drivative^2));
Length = Length + trapz(Tx,Ty);
end
this is my code for this equation and it keep give me an error in trapz function

Answers (1)

Walter Roberson
Walter Roberson on 26 May 2022
Edited: Walter Roberson on 26 May 2022
syms x;
Symbolic
drivative = diff(A(i,1)*x^2+A(i,2)*x+A(i,3));
Symbolic because x is symbolic.
Ty = sqrt(1+(drivative^2));
Symbolic because drivative is symbolic.
Length = Length + trapz(Tx,Ty);
Tx is a numeric vector of length 3, but Ty is symbolic. There is no symbolic trapz() function.
When x is numeric and equally spaced, then trapz(x,y) mathematically works out as
(sum(y)-(y(end)-y(1))/2) * (x(2)-x(1))
Your x vector is length 3, so
(y(1)/2 + y(2) + y(3)/2) * (x(2)-x(1))
But... your Ty is scalar, not a vector of length 3, do you have a problem.
Perhaps you want to evaluate the expression Ty at the three x locations. If you did that and used double() on the result, you would be able to do a plain trapz()

Community Treasure Hunt

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

Start Hunting!