필터 지우기
필터 지우기

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

조회 수: 7 (최근 30일)
Germer DA
Germer DA 2013년 10월 25일
편집: Germer DA 2013년 10월 25일
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!

채택된 답변

Walter Roberson
Walter Roberson 2013년 10월 25일
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
Germer DA
Germer DA 2013년 10월 25일
편집: Germer DA 2013년 10월 25일
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!
Walter Roberson
Walter Roberson 2013년 10월 25일
u3 = sym(zeros(50,1));

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by