Question about vpasolve to solve a nonlinear equation with mutliple variables

5 views (last 30 days)
Hello everyone,
Recently I'm solving a nonlinear equation with mutliple variables. For example:
x=sym('a%d%d',[2,1]);
a=vpasolve(x.a11+tan(x.a12)==0,x,'random',true)
I know "vpasolve" can solve questions about multiple equations and variables. But I only have one equation, and the number of variables depends on the specific problem. So I want to find a more flexible codes. Brief, how can I solve a nonlinear equation with changeable number of variables?
Best,
Xiao
  1 Comment
Walter Roberson
Walter Roberson on 26 Sep 2018
eqn = x(1)+tan(x(2))==0
a = vpasolve(eqn, x,'random',true);
If eqn does not reference some of the variables in x, then those variables will end up with numeric values that could be just about anything.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 26 Sep 2018
Edited: John D'Errico on 26 Sep 2018
You have one equation, in more than one unknown. There is no solution for x. Or, said differently, there may generally be infinitely many solutions.
Had you asked to solve for one variable, as a function of the others, that you could do, well potentially so. It would depend on the actual equation. But VPASOLVE would not generally be the tool to solve the problem. VPASOLVE is a numerical tool, that works in high precision. You would just then use solve anyway.
For example:
syms x y
EQN = x+y == 0;
vpasolve(EQN,x)
ans =
-1.0*y
So for a trivial polynomial equation of low order, VPASOLVE did not have a hiccup. But, even for an almost as trivial one that is not trivially polynomial, we see:
EQN = sin(x+y) == 0;
vpasolve(EQN,x)
Error using mupadengine/feval (line 187)
Symbolic parameters not supported in nonpolynomial equations.
Error in sym/vpasolve (line 172)
sol = eng.feval('symobj::vpasolve',eqns,vars,X0);
solve(EQN,x)
ans =
-y
So you can use solve, at least in that case. Of course, not all problems have a solution. In your example I can do this:
a = solve(x(1)+tan(x(2))==0,x(1))
a =
-tan(a21)
  6 Comments
Walter Roberson
Walter Roberson on 27 Sep 2018
If you simply do not mention any variable names to solve for, then it will solve for all of the mentioned variables. With the single output form of vpasolve() the result will be a struct with one field for each variable.
If you have a complete list of all of the variables that might potentially be present, then you can mention the complete list as the second parameter of vpasolve(). With the single output form of vpasolve() the result will be a struct with one field for each of those variables. Even the "unused" ones will have numeric values (that could be just about anything.) This is not as efficient as not mentioning any of them, or using symvar() to determine the list of variables and mentioning only those, but mentioning the complete list can make some interfaces easier since it can reduce the amount of post-processing you need to do.
Walter Roberson
Walter Roberson on 27 Sep 2018
Instead of that loop you have, the better approach would probably be
fn = fieldnames(b);
for K = 1 : length(fn)
thisfield = fn{K};
fprintf('%s = ', thisfield);
disp( b.(thisfield) )
end

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!