Error with sym variable in a for loop

조회 수: 8 (최근 30일)
Pedro Pires
Pedro Pires 2016년 12월 27일
댓글: Walter Roberson 2016년 12월 28일
Hi. I need to find the value of a variable inside a for loop but I keep getting a "Undefined function or variable 'w'." error. Can anyone explain what I'm doing wrong? I tried changing the w(i)to the inside of the loop. I tried changing to just w and to but none of them worked. All the "final" arrays are [2,2,nel].
Thank you in advanced.
z=zeros(2,2,nel);
sym w(i);
for i=1:nel;
s(i)=solve(k_final(:,:,i)-w(i)^2*(m_final(:,:,i)+m2_final(:,:,i))==z(:,:,i),w(i))
end

답변 (2개)

Sean de Wolski
Sean de Wolski 2016년 12월 27일
syms w(i)
Plural "syms"
  댓글 수: 1
Pedro Pires
Pedro Pires 2016년 12월 28일
Thank you for the answer but the change for sym to syms creates 3 or 4 additional errors in the syms script. I also removed the (i) in the w(i) since it was redundant.

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


Walter Roberson
Walter Roberson 2016년 12월 27일
편집: Walter Roberson 2016년 12월 27일
If you need to be able to index w from 1 to nel then,
w = sym('w', [1 nel]);
However, your code would work just was well with
z=zeros(2,2,nel);
syms w
for i=1:nel;
s(i)=solve(k_final(:,:,i)-w^2*(m_final(:,:,i)+m2_final(:,:,i))==z(:,:,i),w)
end
You are solving for w(i) and so w(i) would not appear in the output, so there is no point in using distinct w(i) variables for it: you might as well use plain w
However, I would be surprised if there is a solution in general. Your equation could be rewritten as
w^2 = (k_final(:,:,i)-z(:,:,i))./(m_final(:,:,i)+m2_final(:,:,i))
which is not going to have a scalar solution. If you want a least-squared fit for w then you are going to need a different operator than solve().
  댓글 수: 2
Pedro Pires
Pedro Pires 2016년 12월 28일
편집: Pedro Pires 2016년 12월 28일
I tried using the syms before but it gave me 3 or 4 errors at the same time in the 'syms' script. I also used plain w previously but it didn't work so I tried what I wrote above. I already have some ideas to work around it so I'll try something different.
What is really intriguing is that it says "Undefined function or variable 'w'." no matter the position or use of sym/syms.
Thank you for the help.
Walter Roberson
Walter Roberson 2016년 12월 28일
nel = 13;
k_final = rand(2,2,nel);
m_final = rand(2,2,nel);
m2_final = rand(2,2,nel);
z=zeros(2,2,nel);
syms w
for i=1:nel;
s{i} = solve(k_final(:,:,i)-w^2*(m_final(:,:,i)+m2_final(:,:,i))==z(:,:,i),w);
end
This will give you a 1 x nel cell array with each item being [0×1 sym] which is the result() of the solve. With those equations is is not possible to find a single w value that solves all 4 of the equations simultaneously -- not unless k_final(:,:,i) ./ (m_final(:,:,i) + m2final(:,:,i)) happens to all be the same value (such as could happen if each layer of the arrays are all the same values as each other.)
I speculate that you want something closer to
nel = 13;
k_final = rand(2,2,nel);
m_final = rand(2,2,nel);
m2_final = rand(2,2,nel);
z=zeros(2,2,nel);
w = sym('w', [2 2]);
for i=1:nel;
s{i} = solve(k_final(:,:,i)-w^2*(m_final(:,:,i)+m2_final(:,:,i))==z(:,:,i),w);
end
This will give a cell array of struct. Each struct will have fields w1_1 w1_2 w2_1 w2_2 which are the values of the 2 x 2 array of w. Each of the fields will be a vector of four values -- there are four roots to the equations.
Note that this interpretation involves understanding w as a 2 x 2 matrix of symbols and understanding w^2 as a matrix power, not an element-by-element power, and involves understanding the w^2 * (m_final(:,:,i)+m2_final(:,:,i)) as an algebraic matrix multiplication. In such a case, the output is meaningful... and long.

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

Community Treasure Hunt

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

Start Hunting!

Translated by