how to solve an integral using trapz

조회 수: 1 (최근 30일)
william Smith
william Smith 2019년 4월 1일
댓글: Walter Roberson 2019년 4월 2일
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