How do i use trapz to integrate each element in a nx1 matrix?

조회 수: 12 (최근 30일)
John O
John O 2022년 3월 22일
댓글: Torsten 2022년 3월 22일
Hello, I'm trying to integrate a nx1 matrix where each element in the matrix is a different equation. So the results should still be an nx1 matrix of the same size as original.
clc
clear
close all
% Parameters
load('XXX.mat')
load('AXX.mat')
v_ac = 10;
v_dc = 40;
% Build model
syms u1(t) u2(t) u3(t) phi1(x) phi2(x) phi3(x)
syms W_s(x)
u = [u1(t) u2(t) u3(t)]
phi = [phi1(x) phi2(x) phi3(x)]
N = 3
mult = phi.*u
W_a(x,t) = W_s(x) + sum(mult(1:N))
phia_1 = ephi1(v_dc+1)
phia_2 = ephi2(v_dc+1)
phia_3 = ephi3(v_dc+1)
phia = [phia_1 phia_2 phia_3]
w_sa = w_ss(v_dc+1)
w_a = subs(W_a,[phi1 phi2 phi3 W_s],[phia_1 phia_2 phia_3 w_sa])
w_a = simplify(w_a)
eqn1 = []
eqn2 = []
XXX = 0:0.05:1;
eqn_mps(x,t) = (diff(w_a(x,t),x,1)^2)+(2*diff(w_a(x,t),x,1)*diff(w_0(x),x,1))
mps = trapz(XXX,eqn_mps(XXX,t))
for i = 1:N
eqn_1(x,t) = phia(i)*diff(w_a,t,2);
eqn1 = [eqn1;eqn_1];
end
eqn1
eqn = trapz(XXX,eqn1(XXX,t))
This is my error
Error using trapz
Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.

답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 3월 22일
trapz is for numerical integration. It does not work on symbolic equations. For symbolic integration use the "int" function from the Symbolic Math Toolbox.
  댓글 수: 3
John O
John O 2022년 3월 22일
It also works if I use trapz individually instead of on the matrix.
Torsten
Torsten 2022년 3월 22일
Yes, because the matrix eqn_mps is symbolic.
You must substitute numerical values for all the symbolic variables contained in eqn_mps.
If the result of this substitution is a matrix with the number of rows equal to the number of rows of XXX, trapz will work.
Since it is not possible to decipher from your code which symbolic variables or arrays eqn_mps depends on, it's up to you to make the correct substitutions.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by