How to solve algebraic equations in Matlab 2018a?

3 views (last 30 days)
Hello, I was trying to solve a algebraic equation, e.g.:
syms a b c d
[c,d]=solve(2*a-b,2*b-a+3,'a','b')
On Matlab 2017b, I can get the final results with the alert: Do not specify equations and variables as character vectors. Instead, create symbolic variables with syms.
But on Matlab 2018a, only error returns: 'a' is not a recognized parameter. For a list of valid name-value pair arguments, see the documentation for this function.
So are there some updates or changes between 2017b and 2018a?
And how can I solve algebraic equations correctly in Matlab 2018a?
Thanks!

Accepted Answer

John D'Errico
John D'Errico on 12 Jun 2018
Edited: John D'Errico on 12 Jun 2018
In R2018a, they finally stopped letting you just provide a string input. It has been something they have been threatening for several years now. The time is now up.
However, you can just do this:
syms x
xroots = solve(3*x^5+4*x^4+7*x^3+2*x^2+9*x+12)
xroots =
root(z^5 + (4*z^4)/3 + (7*z^3)/3 + (2*z^2)/3 + 3*z + 4, z, 1)
root(z^5 + (4*z^4)/3 + (7*z^3)/3 + (2*z^2)/3 + 3*z + 4, z, 2)
root(z^5 + (4*z^4)/3 + (7*z^3)/3 + (2*z^2)/3 + 3*z + 4, z, 3)
root(z^5 + (4*z^4)/3 + (7*z^3)/3 + (2*z^2)/3 + 3*z + 4, z, 4)
root(z^5 + (4*z^4)/3 + (7*z^3)/3 + (2*z^2)/3 + 3*z + 4, z, 5)
Then resolve that mess into numbers using vpa:
vpa(xroots)
ans =
-0.95832248349981782331007510989358
- 0.86122033780576856901169130559204 - 1.4377338954885689163709476213772i
- 0.86122033780576856901169130559204 + 1.4377338954885689163709476213772i
0.67371491288901081400006219387216 - 1.0159473094101229496455473520944i
0.67371491288901081400006219387216 + 1.0159473094101229496455473520944i
Or, you could have just used vpasolve directly.
vpasolve(3*x^5+4*x^4+7*x^3+2*x^2+9*x+12)
ans =
-0.95832248349981782331007510989358
- 0.86122033780576856901169130559204 + 1.4377338954885689163709476213772i
- 0.86122033780576856901169130559204 - 1.4377338954885689163709476213772i
0.67371491288901081400006219387216 - 1.0159473094101229496455473520944i
0.67371491288901081400006219387216 + 1.0159473094101229496455473520944i
Finally, if you absolutely insist on providing it as a string? Use str2sym.
vpasolve(str2sym('3*x^5+4*x^4+7*x^3+2*x^2+9*x+12'))
ans =
-0.95832248349981782331007510989358
- 0.86122033780576856901169130559204 + 1.4377338954885689163709476213772i
- 0.86122033780576856901169130559204 - 1.4377338954885689163709476213772i
0.67371491288901081400006219387216 - 1.0159473094101229496455473520944i
0.67371491288901081400006219387216 + 1.0159473094101229496455473520944i
  4 Comments
John D'Errico
John D'Errico on 12 Jun 2018
Hmm. Thinking about this, and trying to understand the thought processes that lead you in the wrong direction here ... I wonder if we tend to be guided to using the wrong approach there, especially because of the way MATLAB used to allow input to solve.
For example, if we wanted to solve the problem
2*a + b == 2
for a, in terms of b? In R2018 we should write this:
syms a b
a = solve(2*a+b == 2,a)
a =
1 - b/2
In fact, that would always have worked, and was the recommended solution. But in the past, we could also provide the equation in the form of string input.
a = solve('2*a+b == 2','a')
And there, it would have been logical to tell MATLAB to solve for a, as a string, as the name of the variable you wanted to solve for. I'll conjecture that we are still led in that direction. So you tried to write it like this:
syms a b
a = solve(2*a+b == 2,'a')
Subconsciously, you still wanted to tell solve that the "name" of the variable to solve for was a. So while we know that a is a variable, the name of that variable is thought of as 'a'? Is that what was happening, or was it just a residue of the old form that solve previously accepted, where you could provide string input?
Understanding what drives a person to make an error is useful if they need to improve the documentation, or if perhaps there is a fundamental interface problem. While I could never be able to convince TMW to change the interface, changing the docs is more easily done.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!