How to solve two equations with two unknowns that are vectors?
8 views (last 30 days)
Show older comments
I have to find 2 vectors x and y as follows:
syms x y;
E = [ws == sqrt(x^2+y^2), wd == atan2(x,y)];
S = vpasolve(E,x,y);
ws and wd are vectors. In order to obtain the correct first values for x and y, the first values of ws and wd need to be used.
The code above is giving me the error: More equations than variables is only supported for polynomial systems.
0 Comments
Answers (1)
John D'Errico
on 28 Feb 2022
Edited: John D'Errico
on 28 Feb 2022
Sometimes, we get lost in wanting to solve a problem one way, when really, the solution is trivial, if we but look at the problem clearly. Stepping back, and refocusing our mental view of the problem...
The first equation you write is the equation of a circle, centered at the origin, of radius ws.
The second equation defines a line, that also passes through the origin, at a specified angle as defeined by wd. So you are looking to find the intersection of the line with the ciircle. Yes? There will be two such intersection points of course.
The solution is simple. Well, actually, there are god knows how many ways we could probably solve it. We could convert to polar coordintes. Later on, we can convert back. Or, we can just do this:
Look at the second equation. Take the tangent of both sides. Effectively that tells us
tan(wd) = y/x
The line of interest has the equation
y = x*tan(wd)
So it is a line that passes through the origin, and has a slope of tan(wd).
Can we now substitute that into the other equation? First, square it. That would give us:
ws^2 = x^2 + x^2*(tan(wd))^2
Factor out the x^2, and solve.
x^2 = ws^2/(1 + tan(wd)^2)
So we would have x as:
x = +/- ws*sqrt(1/(1 + tan(wd)^2))
But we should also remember the trig identity, that
1 + tan^2 = sec^2
If you don't remember, here is a quick page with some trig identities:
But also, sec(u) = 1/cos(u). So we can write x trivially as:
x = +/- ws*cos(wd)
Therefore, y is as trivial.
y = x*tan(wd) = +/- ws*sin(wd)
Having done all of the work with pencil and paper, is there any need at all to use MATLAB? SIGH. What would Answers be if we did not use MATLAB?
We can verify that my solution is the analytically correct one.
syms x y ws wd
x = [ws*cos(wd),-ws*cos(wd)];
y = [ws*sin(wd),-ws*sin(wd)];
simplify(ws == sqrt(x.^2 + y.^2))
That looks to be a truism. How about the second equation? I have a funny feeling the symbolic toolbox does not really understand atan2, so I'll ust use atan.
wd == simplify(atan(y./x))
Again, those look to be equalities, if we did a little more simplification.
Alternatively, we can also use a numerical example to see how the solution works.
ws = rand(1,5)
wd = rand(1,5)
Now, compute x and y.
x = ws.*cos(wd) % I'll arbitrarily choose the "positive" root
y = ws.*sin(wd) % I'll arbitrarily choose the "positive" root
Now verify they satisfy the original equations.
ws - sqrt(x.^2 + y.^2)
wd - atan2(y,x)
In both cases the equations are perfectly satisfied, at least to within floating point trash in the least significant bits of the result. There was never any need to use a solve in any form.
Sadly, I never even needed to use MATLAB, only basic high school math, at least except to show how the solution works. Such is life.
0 Comments
See Also
Categories
Find more on Assumptions 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!