I need help solving the equation that I am to create with a variable and every time I try to solve it seems to not work

3 views (last 30 days)
So here is the code that I have which wont work.I want h to be a variable and then the matrix K1 should be a matrix with the variable in it. Then to solve for h you should take the determinant of K1=0 which should yield two answers for h. I then would like to display those two answers. Can anyone help me out, it either says that I haven't defined h and then when I do matrix K1 becomes some weird value without h and the solve function which I read should work doesn't. Please let me know what im doing wrong. Thanks
h='h';
K1 = [(20-h),-10/sqrt(2);-10/sqrt(2),(30-h)];
lambda = solve( det(K1)==0 ,h );
disp(' Lambda 1 is'),disp(lambda(1)) ;
disp(' Lambda 2 is'),disp(lambda(2)) ;

Accepted Answer

John D'Errico
John D'Errico on 12 Nov 2014
Edited: John D'Errico on 12 Nov 2014
Why are you making h a character variable, that contains the string 'h'?
Seems like if you want it to be a symbolic variable, then why not make it symbolic?
help syms
If you want to understand what happened for you, consider what happens when you do this:
x = 'h';
30 - x
ans =
-74
Here, I've created a variable called x, to avoid the confusion between a variable h, and the ascii string 'h'.
Why is 30 - x = -74? Because when you use arithmetic operators on ascii strings, they get converted to their ascii numeric representation. The ascii code for 'h' is 104.
+'h'
ans =
104
So when you created that matrix K1, you created a numeric matrix, NOT a symbolic one.
h = 'h';
K1 = [(20-h),-10/sqrt(2);-10/sqrt(2),(30-h)]
K1 =
-84 -7.0711
-7.0711 -74
Learn to use sym or syms to create symbolic variables.
  3 Comments
John D'Errico
John D'Errico on 12 Nov 2014
Edited: John D'Errico on 12 Nov 2014
So if you don't have the function sym, it suggests you lack the symbolic toolbox, or it is not installed properly. If you don't have that TB, then you cannot do symbolic computations.
You can check this by the command:
ver
It will tell you if a license exists for the symbolic toolbox.
Caveat - you CAN do some computations. Here, using my sympoly toolbox, (found on the file exchange)
sympoly h
K1 = [(20-h),-10/sqrt(2);-10/sqrt(2),(30-h)]
K1 =
Sympoly array of size = [2 2]
|1 1| 20 - h
|2 1| -7.0711
|1 2| -7.0711
|2 2| 30 - h
format long g
roots(det(K1))
ans =
33.6602540378444
16.3397459621556
Not a symbolic solution, but still valid for this problem. By way of comparison, the symbolic TB yields...
syms h
K1 = [(20-h),-10/sqrt(2);-10/sqrt(2),(30-h)];
solve(det(K1))
ans =
25 - 5*3^(1/2)
5*3^(½) + 25
vpa(ans)
ans =
16.339745962155613532362768292471
33.660254037844386467637231707529

Sign in to comment.

More Answers (2)

Star Strider
Star Strider on 12 Nov 2014
Christopher, meet charpoly:
syms h
K1 = [(20-h),-10/sqrt(2);-10/sqrt(2),(30-h)];
lambda = solve(charpoly(K1,h))
produces:
lambda =
25/2 - (5*3^(1/2))/2
(5*3^(1/2))/2 + 25/2

William Rose
William Rose on 13 Nov 2014
You can just do what you originally posted, but with "syms h" added, as shown below. (You can use charpoly, as Star Strider said, but you don't have to):
>> syms h;
>> K1 = [(20-h),-10/sqrt(2);-10/sqrt(2),(30-h)];
>> solve(det(K1))
ans =
5*3^(1/2) + 25
25 - 5*3^(1/2)

Community Treasure Hunt

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

Start Hunting!