Does MATLAB have a way to solve for a variable in a nonlinear equation? (i.e. cos(x) + sin(y), solve for y in terms of x using symbols)
Show older comments
I have a very long, complicated equation in which is a variable for which I need solved. Basically, what I did was take the typical rotation matrix form and turn it in to a series of nonlinear equations.
xb = xa*(cosd(b)*cosd(c))+ya*((cosd(c)*sind(b)*sind(a))+(sind(c)*cosd(a)))+za*((sind(c)*sind(a)-(cosd(c)*sind(b)*cosd(a))));
yb = xa*(-sind(c)*cosd(b))+ya*(cosd(a)*cosd(c)-sind(a)*sind(b)*sind(c))+za*(sind(c)*sind(b)*cosd(a)+cosd(c)*sind(a));
zb = xa*(sind(b))+ya*(-cosd(b)*sind(a))+za*(cosd(a)*cosd(b));
I solved for two of the angles with respect to the other angle and the vector. What i need to do now is solve for the last angle in terms of just the vector.
Does anyone know if MATLAB can solve for a variable in terms of other variables?
Thank you!
1 Comment
Walter Roberson
on 7 Nov 2013
Using radians instead of degrees is usually cleaner.
Answers (2)
If you have the Optimization Toolbox, there is fsolve(), but it is a numerical solver, i.e., the output is always a number.
If you have the Symbolic Toolbox, you can try to get a closed form symbolic expression for the solution using solve(), but a symbolic solution doesn't always exist for a set of equations.
Additionally, as I told you in an earlier thread, the solutions to the equations you've shown are highly non-unique.
11 Comments
Caraline
on 7 Nov 2013
One closed-form solution to the problem is given by Rodrigues' formula
with k=cross(vector1, vector2).
It assumes the rotation you want is about an axis perpendicular to both vectors. It's not clear, though, if that's the particular solution you want. There are infinite other solutions.
Caraline
on 7 Nov 2013
Yes, suppose A is a rotation matrix in some reference coordinate system and the columns of S are a set of rotated axes expressed in the reference coordinates. Now suppose x is a position vector measured with respect to the rotated axes, then
(S^-1*A*S)*x
will perform rotation A on x, but give the result in the rotated coordinate system (as opposed to the reference system).
Walter Roberson
on 7 Nov 2013
I'm pretty sure MuPAD can handle solve() of trig functions. For example,
solve('cos(theta)=1/2','theta')
Trig functions are, though, difficult to resolve in more complex equations.
Walter Roberson
on 8 Nov 2013
I am surprised that solve('cos(theta)','theta') did not work. Unfortunately I do not have that toolbox so I cannot experiment.
When you got pi/2 did it literally say pi/2 with pi in words, or did it say 1.5707963267949 ? If it said pi/2 then that was symbolic and would be the correct solution to cos(theta)=0 .
Caraline
on 8 Nov 2013
Now I just need to solve for b.
I suspect you will not be able to solve for b. Again, because the solution is non-unique, there have to be some free variables left over. Think of trying to solve the single equation
x+y=1
for both x and y. I'm impressed that the solver took you as far as this, actually.
Walter Roberson
on 8 Nov 2013
Make sure that n, p, q, r, x, y, z, a, b, c, are all defined as symbols and not functions or arrays. You might want to check with size()
Walter Roberson
on 7 Nov 2013
0 votes
In general you can solve some kinds of equations using the Symbolic Toolbox if you have it (it is part of the Student Version license.)
I suggest you see previous discussions about rotation matrices, such as http://www.mathworks.co.uk/matlabcentral/answers/15411-recover-basic-rotations
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!