how to solve an integral using trapz

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

采纳的回答

Walter Roberson
Walter Roberson 2019-4-1
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 个评论
william Smith
william Smith 2019-4-1
Thank you!
I tried it and got:
Matrix dimensions must agree.
Walter Roberson
Walter Roberson 2019-4-2
n = min(length(p), length(s_dot));
w = trapz(abs(p(1:n) .* s_dot(1:n)));

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by