# How to solve two equations with two unknowns that are vectors?

12 views (last 30 days)
Lu Da Silva on 28 Feb 2022
Edited: John D'Errico on 28 Feb 2022
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.

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))
ans =
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))
ans =
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)
ws = 1×5
0.0204 0.9098 0.1916 0.3678 0.6903
wd = rand(1,5)
wd = 1×5
0.4044 0.4637 0.7594 0.6111 0.5011
Now, compute x and y.
x = ws.*cos(wd) % I'll arbitrarily choose the "positive" root
x = 1×5
0.0188 0.8137 0.1390 0.3013 0.6054
y = ws.*sin(wd) % I'll arbitrarily choose the "positive" root
y = 1×5
0.0080 0.4069 0.1319 0.2110 0.3316
Now verify they satisfy the original equations.
ws - sqrt(x.^2 + y.^2)
ans = 1×5
1.0e+-16 * 0 0 0.2776 0 0
wd - atan2(y,x)
ans = 1×5
1.0e+-16 * 0 0.5551 0 0 0
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.