Multiple Answers From 'solve'

I have an equation that I am passing to the 'solve' function. I know for a fact that it has two solutions. It only returns one solution. This is how I'm calling solve:
solve(S)
where S is a string containing the equation. It's pretty ugly but I can provide it if needed. What I need is a way to get that second solution.

5 个评论

Yes, we will need to see that string - formatted nicely, please.
Alright, I warned you (I'm trying to pass it through a for loop for plotting purposes, sorry for the mess!):
S=(['24.073^2=(-4.19*sin(',num2str(theta2),'*pi/180)-5.166*sin(theta4*pi/180)+23.3*sin(82.99*pi/180))^2+(-4.19*cos(',num2str(theta2),'*pi/180)-5.166*cos(theta4*pi/180)+23.3*cos(82.99*pi/180))^2']);
You will need to create a variable for theta2, I'm trying to solve for theta4.
My test values are for theta2=73.5, theta4 ought to be about 64. The only answer I am returned is -73.9 which is a correct answer but not the one I want. This is for a four-bar linkage problem, I strongly suspect that solve is finding the closed form solution when I would like the open form solution.
Are you trying to solve symbolically for theta4?
Yes, I'm feeding the solve line a series of theta2 values in a loop. I am returned a symbolic answer for theta4 which I turn into a double and plot.

请先登录,再进行评论。

 采纳的回答

syms theta2 theta4
S=(-4.19*sin(theta2*pi/180)-5.166*sin(theta4*pi/180)+23.3*sin(82.99*pi/180))^2+(-4.19*cos(theta2*pi/180)-5.166*cos(theta4*pi/180)+23.3*cos(82.99*pi/180))^2-24.073^2;
sols = solve(S,theta4);
sols = subs(sols,theta2,73.5);
sols =
[-115.9829194]
[ ]
[-73.90876566]
The first solution, added to 180, gives 64.0170806.

5 个评论

I'm getting some strange imaginary components when I run your code:
sols(1) =
-1.1598e+002 +2.5444e-014i
sols(2) =
-73.9088 + 0.0000i
But you're absolutely right, when I add the real part of sols(1) to 180 I get my desired 64. I see now that I drew that angle from the wrong end of the vector...nice catch!
Strange that you're getting an imaginary component and I'm not. What version of MATLAB are you using?
R2011a, it is a classroom license but I wouldn't think it would effect things(?)
Upon playing with it a bit and trying other test cases, I've found something interesting.
One of the two answers is right, plus or minus 180. Also, after this little bit of post-processing, it happens that the answer I want is always the only one with zero imaginary component.
So I just need to write some logic to figure out which one I want.
You've been extremely helpful, many thanks for the assistance!
The reason for the +/- 180 is shown in my answer.

请先登录,再进行评论。

更多回答(1 个)

Take your original equation,
24.073^2 = (-4.19*sin((1/180)*theta2*Pi) - 5.166*sin((1/180)*theta4*Pi) + 23.3*sin(.4610555556*Pi))^2 + (-4.19*cos((1/180)*theta2*Pi) - 5.166*cos((1/180)*theta4*Pi) + 23.3*cos(.4610555556*Pi))^2
and do trig substitutions on it.
24.073^2 = 587.1336560 + 43.29108000*cos((1/180)*theta2*Pi - (1/180)*theta4*Pi) - 195.2540000*cos((1/180)*theta2*Pi-.4610555556*Pi) - 240.7356000*cos((1/180)*theta4*Pi-.4610555556*Pi)
Substitute in any one value for theta2 and the equation becomes one involving constants, and cos() of theta4 converted from degrees to radians. It then becomes obvious that the solution must be periodic over 360 (degrees) and the positive-going and negative-going solutions must be 180 apart.
Thus, all you need from solve() is a single solution, which you can then normalize according to your preferred range, with the other solution in that preferred range 180 apart.

3 个评论

Using my test value of theta2=73.5, which should return about 64 for theta4, I was returned two very long expressions with lots of exponentials.
Perhaps I'm calling solve incorrectly for you equation?
Use vpa() or double() on the expression
Note: above I used Pi with capital-P . In MATLAB you would use pi with lower-case-p .
I get the same single solution I got before... that's some clever trig though. Andrew seems to have figured out how to make it give me both solutions.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by