Solving systems of equations symbolically

48 vues (au cours des 30 derniers jours)
Robert
Robert le 1 Mai 2016
Commenté : Walter Roberson le 8 Déc 2016
Ive been trying to get matlab to solve a system symbolically for 2 days I find it hard to believe that this thing can't do what I'm asking.
I understand how the solve works. For example
syms x y
[x y] = solve('3*x-y=2' , 'x+y =1') ;
When i run this it works but i get a lie of warnings and i have no idea why? All the you tube videos I've seen seem to work perfectly with this
Warning: Support of strings that are not valid variable names or define a number will be removed in a future release. To
create symbolic expressions, first create symbolic variables and then use operations on them.
> In sym>convertExpression (line 1536)
In sym>convertChar (line 1441)
In sym>tomupad (line 1198)
In sym (line 177)
In solve>getEqns (line 405)
In solve (line 225)
In symbolic_test (line 7)
Warning: Support of strings that are not valid variable names or define a number will be removed in a future release. To
create symbolic expressions, first create symbolic variables and then use operations on them.
> In sym>convertExpression (line 1536)
In sym>convertChar (line 1441)
In sym>tomupad (line 1198)
In sym (line 177)
In solve>getEqns (line 405)
In solve (line 225)
In symbolic_test (line 7)
Warning: Do not specify equations and variables as character strings. Instead, create symbolic variables with syms.
> In solve>getEqns (line 445)
In solve (line 225)
In symbolic_test (line 7)
Regardless of the warnings what I'm really after is this.
I am trying to make the syntax easier to deal with. If i have say 5 + equations i really don't want to type them all in to the sole function. Its pitiful that matlab expects that
What i want to do is have a vector of the equations. Why is this so hard?
For Example
equations = [ '3*x-y=2' , 'x+y =1'];
answers = [x y];
answers = solve(equations);
How do i do this?
There is no mention of this in the help solve file Any help much appreciated

Réponses (3)

Walter Roberson
Walter Roberson le 1 Mai 2016
Mathworks has definite plans to remove support for passing strings to solve() and to sym(), except for strings that define a pure number and strings that name a pure variable. The YouTube videos you are looking at are for older versions where this was not yet warned about.
For example,
syms x y
equations = [ sym('3')*x-y==sym('2') , x+y ==sym('1')];
answers = [x y];
sol = solve(equations, answers);
sol.x, sol.y
In turn, it is recommended that if the numeric value can be represented within double, that you do not quote it:
syms x y
equations = [ sym(3)*x-y==sym(2) , x+y ==sym(1)];
answers = [x y];
sol = solve(equations, answers);
sol.x, sol.y
For integer constants that are exactly representable as doubles, you can leave out the sym() without changing the meaning:
syms x y
equations = [ 3*x-y==2 , x+y ==1];
answers = [x y];
sol = solve(equations, answers);
sol.x, sol.y
The meaning subtly changes for constants that are not integers. For example, sym(3.14)*sym(sqrt(2)) is slightly different than 3.14*sqrt(2) and is different yet than sym(3.14)*sqrt(sym(2)) . Each place you have floating point constants at the same level in a term, they will be combined using floating point arithmetic, but sym() around any numeric constant requests that the constant be approximated as a rational, and rationals have exact calculations applied to them rather than the round-off of IEEE 754 double.
Remember that your expression
[ '3*x-y=2' , 'x+y =1']
is the [] list-building operator applied to two row vectors of char, and the "," operator inside [] is horzcat, so you are asking for horzcat( '3*x-y=2' , 'x+y =1' ) which is '3*x-y=2x+y =1'. This would not happen for { '3*x-y=2' , 'x+y =1' } as horzcat() of entries within the cell array operator does not combine adjacent entries into the same cell.
When you pass a string to solve() or to sym(), then the string is interpreted as a MuPAD expression. MuPAD uses '=' as the equation operator. But the ability to construct equations this way is being removed, so you need to start converting to use symbolic expressions and MATLAB syntax. MATLAB syntax uses "==". It is a form of comparison: the equation is not true unless the left hand side compares equal to the right hand side. And solve() allows you to use inequalities, like 3*x-y<2

John D'Errico
John D'Errico le 1 Mai 2016
Modifié(e) : John D'Errico le 1 Mai 2016
You say that you understand solve. But no, apparently you don't understand how solve works.
syms x y
[x y] = solve(3*x-y==2 , x+y == 1)
x =
3/4
y =
1 / 4
If you did understand solve, then you would not have gotten those messages. :)
If your goal is to be able to do this...
syms x y
E ={3*x-y==2 , x+y == 1};
[x,y] = solve(E{:})
x =
3/4
y =
1/4
  4 commentaires
John D'Errico
John D'Errico le 2 Mai 2016
Modifié(e) : John D'Errico le 2 Mai 2016
Curly braces create a cell array. The == sign is how the symbolic toolbox works to define equalities.
As for the string input form, clearly TMW is phasing this input form out.
Karan Gill
Karan Gill le 8 Déc 2016
I'm curious why you didn't check the doc after getting those warnings? https://www.mathworks.com/help/symbolic/solve.html

Connectez-vous pour commenter.


positiveenergy
positiveenergy le 8 Déc 2016
我也遇到了同样的问题,非常感谢回答的哥么,太给力了
  1 commentaire
Walter Roberson
Walter Roberson le 8 Déc 2016
Approximate translation:
I also encountered the same problem, thank you very much brother answered it, too much to force the

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by