Issues with Symbolic Solve
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm using R2012a and have run across the following issue when using the function solve.
syms a b c
temp = solve(a*b+c)
I get temp = -a*b, which I as a human know is the solution for c, but my code couldn't tell this without trying to substitute in -a*b in for a, then b, and finally c. Why don't I get a vector of results [z1, z2, -z1*z2]? So I try the following...
temp = solve(a*b+c,a,b,c)
I get a warning "1 equations in 3 variables..., " and the result I get is empty. So I try the following...
temp = solve(a*b+c,a,a,b,c)
I get a warning "1 equations in 4 variables...," and the result I get is almost what I would expect
temp.a = -z2/z1, z
temp.b = z1
temp.c = z2
I have no idea why I'm getting two solutions for a. When I try the following so that I have an equal number of equations and variables I don't get a warning, but I do get an empty result.
syms z
temp = solve(a*b+c,z,2*z,a,b,c)
I've also tried adding " == 0" to the equations to no avail.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Jul. 2012
S = solve(eqn) solves the equation eqn for the default variable determined by symvar.
and
When performing differentiation, integration, substitution or solving equations, MATLAB uses the variable returned by symvar(s,1) as a default variable. For a symbolic expression or matrix, symvar(s,1) returns the variable closest to x.
and
When sorting the symbolic variables by their proximity to x, symvar uses this algorithm:
The variables are sorted by the first letter in their names. The ordering is x y w z v u ... a X Y W Z V U ... A.
So when you pass in only the expression, solve() will solve for the default variable, which will be the variable closest to "x", and in your case that would be "c". Thus,
solve(a*b+c)
is the same as
solve(a*b+c, c)
which is why you get -a*b as the answer.
When you pass in multiple names, solve() will solve for all of those names simultaneously ("simultaneous equations"), trying to isolate each of the names passed in onto the left hand side and expressed entirely in terms of the remaining constants and variables. It does not solve for each of the variables in turn. But solving simultaneous equations for N variables can only work if you have at least N equations, which is why you get the warning.
If you want to solve for each variable in turn, call solve() multiple times, specifying the variable each time. Automate it if you want to. For example,
expr = a*b+c;
vars = symvars(expr);
clear temp
for v = vars
try
temp.(char(v)) = solve(expr, v);
catch
temp.(char(v)) = 'error';
end
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!