Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Solving nonlinear equations with unknown angles

Subject: Solving nonlinear equations with unknown angles

From: Ben

Date: 4 Mar, 2009 04:41:01

Message: 1 of 4

Hi,
I am new to MATLAB. Can anyone give me hints on how to solve 2 nonlinear equations. The angles are unknown. Basically, i have this equation:

r2*cos(theta_2) - r1*cos(theta_1) = r4*cos(theta_4) - r3*cos(theta_3);
r2*sin(theta_2) - r2*sin(theta_1) = r4*sin(theta_4) - r3*sin(theta_3);

r1, r2, r3, r4, theta_1 and theta_2 are given.
I'm trying to solve for the unkown angles theta_3 and theta_4. Do I have to define a new function to solve this or can i use functions like fsolve?

Any help will be appreciated.

Thanks.

Subject: Solving nonlinear equations with unknown angles

From: Roger Stafford

Date: 4 Mar, 2009 08:14:01

Message: 2 of 4

"Ben " <benlawr2000@yahoo.com> wrote in message <gol0ot$jsu$1@fred.mathworks.com>...
> .......
> r2*cos(theta_2) - r1*cos(theta_1) = r4*cos(theta_4) - r3*cos(theta_3);
> r2*sin(theta_2) - r2*sin(theta_1) = r4*sin(theta_4) - r3*sin(theta_3);
>
> r1, r2, r3, r4, theta_1 and theta_2 are given.
> I'm trying to solve for the unkown angles theta_3 and theta_4.
> ......

  It is possible to solve your equations analytically, which would allow you to avoid using the optimization toolbox functions. It is closely related to the problem of finding the intersections between two circles of radii r3 and r4.

  I'll just sketch an approach you can take. Define x4 = r4*cos(theta_4), y4 = r4*sin(theta_4), x3 = r3*cos(theta_3), and y3 = r3*sin(theta_3). Then you have four equations in four unknowns:

 x4-x3 = p (= left hand side of your 1st equation)
 y4-y3 = q (= left hand side of your 2nd equation)
 x3^2+y3^2 = r3^2
 x4^2+y4^2 = r4^2

Using the first two equations to substitute for x4 and y4 gives

 (x3+p)^2+(y3+q)^2 = r4^2

and subtracting the other equation in x3 and y3 gives

 2*p*x3+p^2+2*q*y3+q^2 = r4^2-r3^2

or

 p*x3+q*y3 = (r4^2-r3^2-p^2-q^2)/2

Solving for, say, y3 in terms of x3 in this and substituting in

 x3^2+y3^2 = r3^2

gives a quadratic equation in the single unknown x3 (or y3) which in general will have either two solutions, one, or none. Working backwards in the above equations will then give the corresponding y3, x4 and y4 values for each of the x3 solutions.

  You can then use matlab's 'atan2' function to solve for theta_3 and theta_4 in terms of these solutions:

 theta_3 = atan2(y3,x3);
 theta_4 = atan2(y4,x4);

Therefore, you will obtain two pairs of solutions for these angles, or one, or none, depending on the values of p, q, r3 and r4.

  Note: You should also try using 'solve' in the Symbolic Toolbox for this problem. It may be smart enough to use this same technique and you would thereby avoid having to use all this messy algebra.

Roger Stafford

Subject: Solving nonlinear equations with unknown angles

From: Ben

Date: 4 Mar, 2009 14:25:02

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gold89$ssi$1@fred.mathworks.com>...
> "Ben " <benlawr2000@yahoo.com> wrote in message <gol0ot$jsu$1@fred.mathworks.com>...
> > .......
> > r2*cos(theta_2) - r1*cos(theta_1) = r4*cos(theta_4) - r3*cos(theta_3);
> > r2*sin(theta_2) - r2*sin(theta_1) = r4*sin(theta_4) - r3*sin(theta_3);
> >
> > r1, r2, r3, r4, theta_1 and theta_2 are given.
> > I'm trying to solve for the unkown angles theta_3 and theta_4.
> > ......
>
> It is possible to solve your equations analytically, which would allow you to avoid using the optimization toolbox functions. It is closely related to the problem of finding the intersections between two circles of radii r3 and r4.
>
> I'll just sketch an approach you can take. Define x4 = r4*cos(theta_4), y4 = r4*sin(theta_4), x3 = r3*cos(theta_3), and y3 = r3*sin(theta_3). Then you have four equations in four unknowns:
>
> x4-x3 = p (= left hand side of your 1st equation)
> y4-y3 = q (= left hand side of your 2nd equation)
> x3^2+y3^2 = r3^2
> x4^2+y4^2 = r4^2
>
> Using the first two equations to substitute for x4 and y4 gives
>
> (x3+p)^2+(y3+q)^2 = r4^2
>
> and subtracting the other equation in x3 and y3 gives
>
> 2*p*x3+p^2+2*q*y3+q^2 = r4^2-r3^2
>
> or
>
> p*x3+q*y3 = (r4^2-r3^2-p^2-q^2)/2
>
> Solving for, say, y3 in terms of x3 in this and substituting in
>
> x3^2+y3^2 = r3^2
>
> gives a quadratic equation in the single unknown x3 (or y3) which in general will have either two solutions, one, or none. Working backwards in the above equations will then give the corresponding y3, x4 and y4 values for each of the x3 solutions.
>
> You can then use matlab's 'atan2' function to solve for theta_3 and theta_4 in terms of these solutions:
>
> theta_3 = atan2(y3,x3);
> theta_4 = atan2(y4,x4);
>
> Therefore, you will obtain two pairs of solutions for these angles, or one, or none, depending on the values of p, q, r3 and r4.
>
> Note: You should also try using 'solve' in the Symbolic Toolbox for this problem. It may be smart enough to use this same technique and you would thereby avoid having to use all this messy algebra.
>
> Roger Stafford

Hi. Thanks for your reply.

I tried using this function,
syms x4 y4 x3 y3
f(1) = v2-v1-x4-x3;
f(2) = w2-w1-y4-y3;
[A,B] = solve(f(1), f(2));

where i defined
v1 = r1*cos(theta_1);
w1 = r1*sin(theta_1);
v2 = r2*cos(theta_2);
w2 = r2*sin(theta_2);
x4 = r4*cos(theta_4);
y4 = r4*sin(theta_4);
x3 = r3*cos(theta_3);
y3 = r3*sin(theta_3);

When i run the script it says explicit solution not found. Is my script right? I also tried to do what you have outlined but MATLAB does not allow me to just throw equations in the editor. Lets say I declared
p = r2*cos(theta_2) - r1*cos(theta_1)

and then say p = x4-x3;

It doesn't allow me to equate p twice. This may sound like a stupid question but, im really new to MATLAB and our school ask us to work on this program without giving us any tutorial. So i have to learn this basically on my own....

Thanks....

Subject: Solving nonlinear equations with unknown angles

From: Roger Stafford

Date: 4 Mar, 2009 20:00:20

Message: 4 of 4

"Ben " <benlawr2000@yahoo.com> wrote in message <gom2vu$76j$1@fred.mathworks.com>...
> ......
> [A,B] = solve(f(1), f(2));
> ......

  When you write "[A,B] = solve(f(1),f(2));" the 'solve' function has no way of knowing which variables are to be regarded as known and which unknown. You need to use the form which specifies these explicitly with var1, var2, etc.:

 "g = solve(eq1,eq2,...,eqn,var1,var2,...,varn)"

Check on the documentation for 'solve' about this.

  In any case, I think it may be asking too much of 'solve' to directly solve for theta_3 and theta_4 as unknowns. It may become confused by the fact that there are infinitely many possible angle solutions if the angles are allowed to go beyond the range from -pi to +pi or from 0 to 2*pi.

  When I tried solving the four equations:

 x4-x3 = p
 y4-y3 = q
 x3^2+y3^2 = r3^2
 x4^2+y4^2 = r4^2

with just x3, y3, x4, and y4 as unknowns on my ancient 15-year-old version of Symbolic Toolbox, it did manage to grind out a solution involving p, q, r3, and r4, and, as expected, it was in terms of the roots of a quadratic equation. If mine can do it, yours surely can. That should save on your paper supplies.

  It is easy to then follow up with the use of 'atan2' in the manner I mentioned earlier to find theta_3 and theta_4. Incidentally, in that formula with 'atan2' the assumption made there is that both r3 and r4 are positive quantities. The resulting angles will lie somewhere in the interval from -pi to +pi radians.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us