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

6 views (last 30 days)
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.

Answers (1)

Benjamin Thompson
Benjamin Thompson on 22 Mar 2022
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 Comments
John O
John O on 22 Mar 2022
It also works if I use trapz individually instead of on the matrix.
Torsten
Torsten on 22 Mar 2022
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.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!