Filter löschen
Filter löschen

How can I use a symbolic variable in a for-loop?

7 Ansichten (letzte 30 Tage)
Germer DA
Germer DA am 25 Okt. 2013
Bearbeitet: Germer DA am 25 Okt. 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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Okt. 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 Kommentare
Germer DA
Germer DA am 25 Okt. 2013
Bearbeitet: Germer DA am 25 Okt. 2013
Hello Walter, thanks for your quick reply!
First , I was wrong the error I get is:
| _The following error occurred converting from sym to double: Error using mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead._ |
Second : Previously, I defined the time = 0:.1:90 and that worked.
What I try to do here is to define u as a function of time ( u=f(time) ), so I can get my acceleration.
But I am not sure, how to do that!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by