Extract eigenvalues and eigenvectors / Symbolic to numeric / Rayleigh-Ritz Method / Vibro-acoustics

4 views (last 30 days)
Hi all,
I am trying to solve a simple vibro-acoustics problem in a beam with the following code:
E=1;
A=1;
rho = 1;
I = 1;
L = 1;
n = 10;
syms i j x
di = x.^i;
dj = x.^j;
for i = 1:n
for j = 1:n
M(i,j) = int((rho*A*di.*dj),0,L);
K(i,j) = int((E*I*diff(di).*diff(dj)),0,L);
end
end
[Value, Vector] = eig(M,K);
But it seems that eig is not working properly. The error that shows up is:
"Error using sym/eig Too many input arguments."
I know that I must change M(i,j) and K(i,j) from sym to their numeric values to make eig work properly, but I do not know how to do so. Any thoughts?

Answers (2)

Walter Roberson
Walter Roberson on 16 Nov 2019
Edited: Walter Roberson on 18 Nov 2019
Symbolic eigenvalue does not support generalized eigenvalues.

Christine Tobler
Christine Tobler on 18 Nov 2019
The problem is making i and j into symbolic variables, which leads to the symbolic engine not knowing that these are real integer numbers. The following works much quicker and gives a result that you can apply "double" to without any problem:
E=1;
A=1;
rho = 1;
I = 1;
L = 1;
n = 10;
syms x
for i = 1:n
for j = 1:n
M(i,j) = int((rho*A*x.^i.*x.^j),0,L);
K(i,j) = int((E*I*diff(x.^i).*diff(x.^j)),0,L);
end
end
>> M
M =
[ 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12]
[ 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13]
[ 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14]
[ 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15]
[ 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16]
[ 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17]
[ 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18]
[ 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19]
[ 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20]
[ 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21]
>> K
K =
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[ 1, 4/3, 3/2, 8/5, 5/3, 12/7, 7/4, 16/9, 9/5, 20/11]
[ 1, 3/2, 9/5, 2, 15/7, 9/4, 7/3, 12/5, 27/11, 5/2]
[ 1, 8/5, 2, 16/7, 5/2, 8/3, 14/5, 32/11, 3, 40/13]
[ 1, 5/3, 15/7, 5/2, 25/9, 3, 35/11, 10/3, 45/13, 25/7]
[ 1, 12/7, 9/4, 8/3, 3, 36/11, 7/2, 48/13, 27/7, 4]
[ 1, 7/4, 7/3, 14/5, 35/11, 7/2, 49/13, 4, 21/5, 35/8]
[ 1, 16/9, 12/5, 32/11, 10/3, 48/13, 4, 64/15, 9/2, 80/17]
[ 1, 9/5, 27/11, 3, 45/13, 27/7, 21/5, 9/2, 81/17, 5]
[ 1, 20/11, 5/2, 40/13, 25/7, 4, 35/8, 80/17, 5, 100/19]
Now that the symbolic engine has been able to compute exact values for M and K, they can be translated to double and fed to eig for generalized doubles:
[U, D] = eig(double(K), double(M));
  1 Comment
Walter Roberson
Walter Roberson on 18 Nov 2019
The problem is making i and j into symbolic variables, which leads to the symbolic engine not knowing that these are real integer numbers.
Yes, that is a problem.
In the great majority of cases, if you have created a variable as symbolic, and you then assign a numeric value to the same variable name after you have used the variable in an expression, then you are likely to make mistakes in the code. It is tricky to keep track of whether the name refers to the symbolic variable or to the numeric variable. When you do
syms i
j = i^2
i = 3
then j does not become 3^2 = 9, just the same way that if you do
i = 2
j = i^2
i = 3
then j does not become 3^2 = 9. When you use a symbolic variable in an expression, then a copy of the value is taken, not a reference to the variable in the MATLAB workspace. For symbolic variables, the value is a reference to where the symbolic variable lives inside the MuPAD symbolic engine.
syms i
is the same as
i = sym('i');
and results in the MATLAB workspace having a variable named i which is pretty much a pointer inside of MuPAD, like
i = (reference to _sym_30184_8412 inside MuPAD)
when you then do
j = i^2
then i is looked up, found to be (reference to _sym_30184_8412 inside MuPAD) and MuPad is invoked to square that, returning a new symbolic reference such as (reference to _sym_2193_7762 inside MuPAD) and that reference gets stored inside the MATLAB variable. When you then proceed to
for i = 1 : n
then the MATLAB workspace variable i gets overwritten with numeric 1, but (_sym_30184_8412 inside MuPAD) and (reference to _sym_2193_7762 inside MuPAD) still exist: MATLAB does not tell MuPAD to change the MuPAD variable corresponding to i . When you then evaluate j in an expression, it is still (reference to _sym_2193_7762 inside MuPAD) which still refers to (_sym_30184_8412 inside MuPAD)^2 which is symbolic variable i squared, not MATLAB workspace numeric variable i squared.
Until you know the Symbolic Toolbox well, you should get in the habit of refusing to assign a numeric value to a variable that had a symbolic name before. Instead, you should use a different variable name, and use subs(), like
syms si sj x
di = x.^si;
dj = x.^sj;
for i = 1:n
for j = 1:n
M(i,j) = int(subs(rho*A*di.*dj, [si, sj], [i,j]),0,L);
K(i,j) = int(subs(E*I*diff(di).*diff(dj), [si, sj], [i,j]),0,L);
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!