vpasolve gives answers outside the range that I specify

3 views (last 30 days)
My code is the following:
syms E C D V P S A
equation1 = (E - C + 200*D + 3)/V - 5*P - (500*S)/(100*A + 1) - 1/5 == 0
Y = vpasolve( equation1, [E C D V P S A], [0 100;0 100;0 100;0 100;0 100;0 100;0 100] )
Y =
struct with fields:
E: -3.0
C: 0
D: 0
V: 0
P: 0
S: 0
A: 0
But! I have defined the search range to be positive, so why do I keep getting this answer? I cannot do anything to change the outcome.
Also, I am really questioning the usefulness of vpasolve since it can't even give an answer to something like:
syms x y
S = vpasolve([x^2+y^2 == 1],[x y],[0.5 1;0 1])
S.x
S.y
, where I am interested in a solution where x is between 0.5 and 1, which clearly exists! (but not according to Matlab)
  2 Comments
Paul
Paul on 15 Jun 2022
Though not stated on the doc page AFAICT, maybe vpasolve needs to have at least the same number of equations as unknowns when specifying init_param. There may be some other restrictions on the equations as well.
This works fine
syms x y
S = vpasolve([x + y == 1; x^2 == y],[x y],[0 1;0 1])
S = struct with fields:
x: 0.61803398874989484820458683436564 y: 0.38196601125010515179541316563436
But this doesn't?
syms x y
S = vpasolve(x + y == 1,[x y],[0 1;0 1])
S = struct with fields:
x: [0×1 sym] y: [0×1 sym]
Star Strider
Star Strider on 15 Jun 2022
I certainly agree that solving one equation in seven unknowns is unlikely to be successful for more than one of them, however the question was about getting a positive result.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Jun 2022
One option is to specify conditions on the variables, for example on ‘E’ (and any others you may want) —
syms E C D V P S A
assume(E>=0)
equation1 = (E - C + 200*D + 3)/V - 5*P - (500*S)/(100*A + 1) - 1/5 == 0
equation1 = 
Y = solve( equation1, [E C D V P S A] )
Y = struct with fields:
E: pi - 14/5 C: pi D: 0 V: 1 P: 0 S: 0 A: 0
Y = vpa(struct2cell(Y),)
Y = 
I am not certain how much sense this makes in the context of the problem, however it seems to produce the desired result.
.
  3 Comments
Peter
Peter on 14 Jun 2022
Also, my variable space is kind of big actually so it might actually be better to use the optimization toolbox on second thought!
Star Strider
Star Strider on 14 Jun 2022
I usually prefer solve and then using vpa on the results, to vpasolve.
I am not certain what you may want to do with the Optimization Toolbox functions, since in general the Symbolic Math Toolbox may be better for problems such as these.
However an easy way to use your equation with them is:
syms E C D V P S A
equation1 = (E - C + 200*D + 3)/V - 5*P - (500*S)/(100*A + 1) - 1/5
equation1 = 
eqn1fcn = matlabFunction(equation1, 'Vars',{[E C D V P S A]})
eqn1fcn = function_handle with value:
@(in1)in1(:,5).*-5.0-(in1(:,6).*5.0e+2)./(in1(:,7).*1.0e+2+1.0)+(-in1(:,2)+in1(:,3).*2.0e+2+in1(:,1)+3.0)./in1(:,4)-1.0./5.0
This presents all the variables as elements of the ‘in1’ vector, with:
in1(:,1) = E
...
in1(:,7) = A
in the order of their position in the 'Vars' vector.
.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!