Error with sym variable in a for loop

3 views (last 30 days)
Pedro Pires
Pedro Pires on 27 Dec 2016
Commented: Walter Roberson on 28 Dec 2016
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

Answers (2)

Sean de Wolski
Sean de Wolski on 27 Dec 2016
syms w(i)
Plural "syms"
  1 Comment
Pedro Pires
Pedro Pires on 28 Dec 2016
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.

Sign in to comment.


Walter Roberson
Walter Roberson on 27 Dec 2016
Edited: Walter Roberson on 27 Dec 2016
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 Comments
Pedro Pires
Pedro Pires on 28 Dec 2016
Edited: Pedro Pires on 28 Dec 2016
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 on 28 Dec 2016
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!