how to solve an integral using trapz
4 views (last 30 days)
Show older comments
william Smith
on 1 Apr 2019
Commented: Walter Roberson
on 2 Apr 2019
trying to solve the following equations using trapz:
Thanks!
w=integral abs(p* x_dot)
would this work:
w=trapz(abs(p(i)*x_dot(i))
0 Comments
Accepted Answer
Walter Roberson
on 1 Apr 2019
Not likely.
If i is a scalar, then abs(p(i)*x_dot(i)) would be a scalar, and trapz() of a scalar would be 0.
If i is a vector, and p and x_dot are vectors, then p(i) would be a vector with the same orientation that p has, and x_dot(i) would be a vector with the same orientation that x_dot has. Your * is algebraic matrix multiplication, so the inner dimensions would have to agree, which would not happen if p and x_dot have the same orientation. If p is a row vector and x_dot is a column vector then p(i) and x_dot(i) would be valid to use * between, giving you a scalar output, but then trapz() of a scalar is 0. So to get anything useful in this situation, p would have to be a column vector and x_dot would have to be a row vector, in which case the * would give a result which was length(i) by length(i) and it would be valid to trapz() that.
If i is a vector, and p and x_dot are non-scalar with 2 or more dimensions, then x(i) and x_dot(i) would be column vectors (linear indexing) and the * would fail with dimensions not matching.
If i is a matrix, then p(i) and x_dot(i) would be matrices with the same shape as i has. In order to be able to use * between those, i would have to be a square matrix, and the result of the * would be a square matrix the same size. It would be valid to trapz() that.
So it is possible for the code to work... it just isn't likely that you happened to have arranged the circumstances for that to occur.
What would probably make more sense is trapz(abs(p .* x_dot))
4 Comments
Walter Roberson
on 2 Apr 2019
n = min(length(p), length(s_dot));
w = trapz(abs(p(1:n) .* s_dot(1:n)));
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!