Symbolic solver help again

1 Ansicht (letzte 30 Tage)
Robert
Robert am 4 Dez. 2016
Bearbeitet: Robert am 4 Dez. 2016
So i have had multiple issues with the solver getting different values because matlab has allowed so many intricacies with this tool.
I have two examples here. One works and proves my method. The other does not and i have no clue why given they are almost the same
The first is as follows.
syms s
syms k wn real
equation = (s^3+3*s^2+((10+1.26)/(5))*s+k)
S1 = -wn + wn*sqrt(3)*i
p = subs(equation,s,S1)
[wn k] = solve(real(p) == 0,imag(p) == 0,[wn k])
double(wn)
double(k)
this works just fine and i get the result i should.
Next is the exact same thing with just a change of numbers.
syms s
syms k wn real
equation = 1+((s+1.4))/((s+5))*(10*(s+10))/(s*(s+1)*(s+10)+10*k*s)
pretty(equation)
S1 = -wn + wn*sqrt(3)*i
p = subs(equation,s,S1)
[wn k] = solve(real(p) == 0,imag(p) == 0,[wn k])
The second example will not give me the correct answer. Can someone tell me why? The only other constraint i can think to tell it is that K>0 and I've already told it that Wn and K are real which is one thing i ran into before hand and that is how i got example 1 to work.
Can anyone tell me whats going on here? Thanks
P.S example 2 K should 9.08 Wn is unimportant
  1 Kommentar
Robert
Robert am 4 Dez. 2016
Also just for kicks i tried rearranging example equation 2 into polynomial format and it still won't solve it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Dez. 2016
You moved into a system that required a polynomial of order 5 or above. The symbolic solutions are in terms of an auxiliary variable, x, which are the fifth roots of the polynomial. You did not allow it to return the conditions under which the system is true, so it just discards that information and returns in terms of the auxiliary variable without telling you what the auxiliary variable means.
If you change to
Sol = solve(real(p) == 0,imag(p) == 0, [wn k], 'returnconditions', true)
then you will get Sol.wn, Sol.k, and also Sol.parameters and Sol.conditions . Sol.parameters will be the names of the auxiliary variables used in the solution -- x in this case. Sol.conditions is the expression that needs to be true for the wn and k to be valid solutions.
You then proceed further by,
X = solve(Sol.conditions, Sol.parameters);
all_k = subs(Sol.k, Sol.parameters, X);
pos_k = all_k(vpa(all_k) > 0);
and you can double() that if you want.
  3 Kommentare
Walter Roberson
Walter Roberson am 4 Dez. 2016
Sol = solve(real(p) == 0,imag(p) == 0, [wn k], 'returnconditions', true);
X = solve(Sol.conditions, Sol.parameters);
all_k = subs(Sol.k, Sol.parameters, X);
mask = logical( vpa(all_k) > 0 );
pos_k = all_k(mask);
corresponding_wn = subs(Sol.wn, Sol.parameters, X(mask))
k = double(pos_k);
wn = double(corresponding_win);
Walter Roberson
Walter Roberson am 4 Dez. 2016
Any time a variable gets isolated as roots of an expression (set of values that make the expression equal zero) such as roots of a polynomial, then chances are that another variable will end up using the just-isolated variable multiple times. Any one root has to be carried through consistently in the multiple occurrences to produce a solution. If there is a single closed form solution then you might be able to afford to substitute that closed form; if there are a pair of closed form solutions you might be able to afford creating a vector of the two and substituting through. After that it gets too expensive unless the solutions turn out to be simple extensions of numeric constants (e.g., perhaps the algebraic numbers extended with simple trig and sqrt). Once complex enough, it becomes easier to drop in a variable to represent the roots and manipulate from there. When that happens, MATLAB starts to want to use ReturnConditions to return the variable. You can use the maxdegree option change the polynomial degree that it will explicitly expand. At first glance it appears to me that your first set of equation might not have needed more than degree 3 and with the syms real constraint might have been able to narrow down to a single value. Your second set needs degree 5 and even if you had put on enough constraints to select only one root, with the lack of closed form solutions it would have preferred to leave it as a variable.
I do not know the exact boundaries it uses in deciding whether to bring the root() or RootOf() inline and when it turns it into a variable. The symbolic engine is not documented nearly as well as I would prefer.
Anyhow I find that the flow I show can be used fairly generally, but I have seen cases where the second solve() to get the values of the variables has required a returnconditions of its own and third solve()

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by