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

12 visualizaciones (últimos 30 días)
John O
John O el 22 de Mzo. de 2022
Comentada: Torsten el 22 de Mzo. de 2022
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.

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by