How can I use a symbolic variable in a for-loop?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Germer DA
le 25 Oct 2013
Modifié(e) : Germer DA
le 25 Oct 2013
Hello, I try to simulate the wave acceleration of irregular waves, which is defined as: a = ut/dt So I created a symbolic variable for the derivation (diff), my problem is to involve the symbolic variable in the for loop. I get the following error:
Error using mupadmex
Error in MuPAD command: Dimensions do not match. [(Dom::Matrix(Dom::ExpressionField()))::_mult2]
Error in sym/privBinaryOp (line 1694)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in sym/mtimes (line 184)
X = privBinaryOp(A, B, 'symobj::mtimes');
%% calculate irregular waves
syms time;
t = 0:dt:180; %modeling with dt = .1 s
u3 = zeros(50,length(t));
phase = 2*pi*rand(50);
for j = 1:50
u3(j,:) = u2(j)*sin(omega(j).*time + phase(j));
end
u4=sum(u3);
a4 = diff(u4,time);
-----------------------------------
u2 and omega are 1x50 matrices
---------------------------------------
Thanks in advance!
Réponse acceptée
Walter Roberson
le 25 Oct 2013
You have
u3(j,:) = u2(j)*sin(omega(j).*time + phase(j))
where u3 has been assigned zeros(), so it can only store numeric values and not symbolic values.
Your omega(j) is scalar, your time is symbolic scalar, your phase is scalar, your u2(j) is scalar, and you assign the whole result to a row vector. Are you sure that is what you want to do?
You sum() the u3 array. sum() without a dimension number sums along the first dimension, so it sums by columns giving a row result. But your columns are all the same, so all the row entries will be the same.
2 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!