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 2 Equations

Subject: Solving 2 Equations

From: Michael Boyd

Date: 28 Feb, 2012 11:41:10

Message: 1 of 10

Hi guys,

I am trying to find the value of variable, 'crank', that satisfies 2 equations.

Here is the script.

clc
clear all

a=13.5;
l=48;
crank=0:pi/900:2*pi;
phase=30;

positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;

I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.

I am unsure how you would do this.

Mike

Subject: Solving 2 Equations

From: Torsten

Date: 28 Feb, 2012 11:51:38

Message: 2 of 10

On 28 Feb., 12:41, "Michael Boyd" <mikeb...@btinternet.com> wrote:
> Hi guys,
>
> I am trying to find the value of variable, 'crank', that satisfies 2 equations.
>
> Here is the script.
>
> clc
> clear all
>
> a=13.5;
> l=48;
> crank=0:pi/900:2*pi;
> phase=30;
>
> positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
>
> I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
>
> I am unsure how you would do this.
>
> Mike


Try fmincon for the problem

min: crank
a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 >= 59.75
a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 >= 58.75
0<=crank<=2*pi

Best wishes
Torsten.

Subject: Solving 2 Equations

From: Michael Boyd

Date: 28 Feb, 2012 12:10:12

Message: 3 of 10

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <032717fd-78f8-40ac-8954-44b3d9e7f7df@b18g2000vbz.googlegroups.com>...
> On 28 Feb., 12:41, "Michael Boyd" <mikeb...@btinternet.com> wrote:
> > Hi guys,
> >
> > I am trying to find the value of variable, 'crank', that satisfies 2 equations.
> >
> > Here is the script.
> >
> > clc
> > clear all
> >
> > a=13.5;
> > l=48;
> > crank=0:pi/900:2*pi;
> > phase=30;
> >
> > positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> > positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
> >
> > I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
> >
> > I am unsure how you would do this.
> >
> > Mike
>
>
> Try fmincon for the problem
>
> min: crank
> a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 >= 59.75
> a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 >= 58.75
> 0<=crank<=2*pi
>
> Best wishes
> Torsten.


So do I have to set up a function and solve for that?

Subject: Solving 2 Equations

From: Torsten

Date: 28 Feb, 2012 12:37:36

Message: 4 of 10

On 28 Feb., 13:10, "Michael Boyd" <mikeb...@btinternet.com> wrote:
> Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <032717fd-78f8-40ac-8954-44b3d9e7f...@b18g2000vbz.googlegroups.com>...
> > On 28 Feb., 12:41, "Michael Boyd" <mikeb...@btinternet.com> wrote:
> > > Hi guys,
>
> > > I am trying to find the value of variable, 'crank', that satisfies 2 equations.
>
> > > Here is the script.
>
> > > clc
> > > clear all
>
> > > a=13.5;
> > > l=48;
> > > crank=0:pi/900:2*pi;
> > > phase=30;
>
> > > positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> > > positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
>
> > > I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
>
> > > I am unsure how you would do this.
>
> > > Mike
>
> > Try fmincon for the problem
>
> > min: crank
> > a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 >= 59.75
> > a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 >= 58.75
> > 0<=crank<=2*pi
>
> > Best wishes
> > Torsten.
>
> So do I have to set up a function and solve for that?- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

I'd suggest that you do so.
Only if this is _one isolated_ problem, you could also run a loop with
crank
increasing from 0 to 2*pi in small increments, evaluate the two
expressions
above and see when the two conditions are met for the first time.

Best wishes
Torsten.

Subject: Solving 2 Equations

From: Michael Boyd

Date: 28 Feb, 2012 13:05:14

Message: 5 of 10

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <aae63a13-c145-4d85-914c-7807467a149e@gr6g2000vbb.googlegroups.com>...
> On 28 Feb., 13:10, "Michael Boyd" <mikeb...@btinternet.com> wrote:
> > Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <032717fd-78f8-40ac-8954-44b3d9e7f...@b18g2000vbz.googlegroups.com>...
> > > On 28 Feb., 12:41, "Michael Boyd" <mikeb...@btinternet.com> wrote:
> > > > Hi guys,
> >
> > > > I am trying to find the value of variable, 'crank', that satisfies 2 equations.
> >
> > > > Here is the script.
> >
> > > > clc
> > > > clear all
> >
> > > > a=13.5;
> > > > l=48;
> > > > crank=0:pi/900:2*pi;
> > > > phase=30;
> >
> > > > positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> > > > positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
> >
> > > > I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
> >
> > > > I am unsure how you would do this.
> >
> > > > Mike
> >
> > > Try fmincon for the problem
> >
> > > min: crank
> > > a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 >= 59.75
> > > a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 >= 58.75
> > > 0<=crank<=2*pi
> >
> > > Best wishes
> > > Torsten.
> >
> > So do I have to set up a function and solve for that?- Zitierten Text ausblenden -
> >
> > - Zitierten Text anzeigen -
>
> I'd suggest that you do so.
> Only if this is _one isolated_ problem, you could also run a loop with
> crank
> increasing from 0 to 2*pi in small increments, evaluate the two
> expressions
> above and see when the two conditions are met for the first time.
>
> Best wishes
> Torsten.

 I have written the function now, so what would be the syntax?

x=fmincon(function,initial value, ?, ?)

Subject: Solving 2 Equations

From: John D'Errico

Date: 28 Feb, 2012 13:25:14

Message: 6 of 10

"Michael Boyd" wrote in message <jiiegm$gs0$1@newscl01ah.mathworks.com>...
> Hi guys,
>
> I am trying to find the value of variable, 'crank', that satisfies 2 equations.
>
> Here is the script.
>
> clc
> clear all
>
> a=13.5;
> l=48;
> crank=0:pi/900:2*pi;
> phase=30;
>
> positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
>
> I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
>
> I am unsure how you would do this.

Because you are thinking of it in the wrong way. You are
NOT solving two equations. You wish to minimize crank,
subject to a set of nonlinear constraints on it.

And, yes, you will want to set up a function for at least
the nonlinear constraints. The objective function for
fmincon can be done as an anonymous function.

If you prefer not to do it that way (or don't have the
optimization toolbox), you could also use fminbnd.

a=13.5;
l=48;
crank=0:pi/900:2*pi;
phase=30;

positionEX = @(crank) a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 - 59.75;
positionIN = @(crank) a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 - 58.75;

See that I subtracted off the lower bounds, so you can
just look for zero crossings. Now use ezplot.

ezplot(positionEX,[-50,50])
figure
ezplot(positionIN,[-50,50])

Hmm. This points out that both functions are just sine
waves, shifted, scaled, phase shifted. It also indicates
there is NO minimum value of crank that satisfies both
constraints. If you can find one solution, then you can
find infinitely many solutions, all of which are less than
the chosen value. As such, no solver will succeed as you
have currently described the problem.

This tells me you need to better define your problem.
Are you looking for the first non-negative solution?

John

Subject: Solving 2 Equations

From: Michael Boyd

Date: 28 Feb, 2012 13:34:13

Message: 7 of 10

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <jiikjq$5n5$1@newscl01ah.mathworks.com>...
> "Michael Boyd" wrote in message <jiiegm$gs0$1@newscl01ah.mathworks.com>...
> > Hi guys,
> >
> > I am trying to find the value of variable, 'crank', that satisfies 2 equations.
> >
> > Here is the script.
> >
> > clc
> > clear all
> >
> > a=13.5;
> > l=48;
> > crank=0:pi/900:2*pi;
> > phase=30;
> >
> > positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> > positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
> >
> > I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
> >
> > I am unsure how you would do this.
>
> Because you are thinking of it in the wrong way. You are
> NOT solving two equations. You wish to minimize crank,
> subject to a set of nonlinear constraints on it.
>
> And, yes, you will want to set up a function for at least
> the nonlinear constraints. The objective function for
> fmincon can be done as an anonymous function.
>
> If you prefer not to do it that way (or don't have the
> optimization toolbox), you could also use fminbnd.
>
> a=13.5;
> l=48;
> crank=0:pi/900:2*pi;
> phase=30;
>
> positionEX = @(crank) a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 - 59.75;
> positionIN = @(crank) a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 - 58.75;
>
> See that I subtracted off the lower bounds, so you can
> just look for zero crossings. Now use ezplot.
>
> ezplot(positionEX,[-50,50])
> figure
> ezplot(positionIN,[-50,50])
>
> Hmm. This points out that both functions are just sine
> waves, shifted, scaled, phase shifted. It also indicates
> there is NO minimum value of crank that satisfies both
> constraints. If you can find one solution, then you can
> find infinitely many solutions, all of which are less than
> the chosen value. As such, no solver will succeed as you
> have currently described the problem.
>
> This tells me you need to better define your problem.
> Are you looking for the first non-negative solution?
>
> John

I am trying to find the first 'crank' where both positionEXand positionIN are more than or equal to 59.75 and 58.75 respectively.

The reason I am trying to do it this way is because I plan to change other variables and loop this code.

Subject: Solving 2 Equations

From: Steven_Lord

Date: 28 Feb, 2012 14:29:07

Message: 8 of 10



"Michael Boyd" <mikeboyd@btinternet.com> wrote in message
news:jiil4l$7b7$1@newscl01ah.mathworks.com...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message
> <jiikjq$5n5$1@newscl01ah.mathworks.com>...
>> "Michael Boyd" wrote in message
>> <jiiegm$gs0$1@newscl01ah.mathworks.com>...
>> > Hi guys,
>> >
>> > I am trying to find the value of variable, 'crank', that satisfies 2
>> > equations.

Inequalities, technically, but that's okay.

*snip*

>> Hmm. This points out that both functions are just sine
>> waves, shifted, scaled, phase shifted. It also indicates
>> there is NO minimum value of crank that satisfies both
>> constraints. If you can find one solution, then you can
>> find infinitely many solutions, all of which are less than
>> the chosen value. As such, no solver will succeed as you
>> have currently described the problem.
>>
>> This tells me you need to better define your problem.
>> Are you looking for the first non-negative solution?
>>
>> John
>
> I am trying to find the first 'crank' where both positionEXand positionIN
> are more than or equal to 59.75 and 58.75 respectively.
>
> The reason I am trying to do it this way is because I plan to change other
> variables and loop this code.

Run this code after defining the positionEX and positionIN functions using
John's example:

x = -50:0.01:50;
plot(x, positionEX(x), 'b-', x, positionIN(x), 'r-');
line([-50 50], [0 0], 'Color', 'k');

By the way John defined positionEX and positionIN, the solution you're
looking for must have both the blue line and the red line above the black
line simultaneously. You can see that doesn't happen, so your problem has no
solution.

Even if your system did have a solution, it would have an infinite number of
solutions from the fact that the sine and cosine functions are periodic with
period 2*pi: sin(x-2*pi) = sin(x) and cos(x-2*pi) = cos(x), Thus
positionEX(crank-2*pi) = positionEX(crank) and positionIN(crank-2*pi) =
positionIN(crank). Therefore if there were a solution you could always
generate a smaller solution and so none of the solutions can be the "first"
solution.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Solving 2 Equations

From: John D'Errico

Date: 28 Feb, 2012 14:44:09

Message: 9 of 10

"Michael Boyd" wrote in message <jiil4l$7b7$1@newscl01ah.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <jiikjq$5n5$1@newscl01ah.mathworks.com>...
> > "Michael Boyd" wrote in message <jiiegm$gs0$1@newscl01ah.mathworks.com>...
> > > Hi guys,
> > >
> > > I am trying to find the value of variable, 'crank', that satisfies 2 equations.
> > >
> > > Here is the script.
> > >
> > > clc
> > > clear all
> > >
> > > a=13.5;
> > > l=48;
> > > crank=0:pi/900:2*pi;
> > > phase=30;
> > >
> > > positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> > > positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
> > >
> > > I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
> > >
> > > I am unsure how you would do this.
> >
> > Because you are thinking of it in the wrong way. You are
> > NOT solving two equations. You wish to minimize crank,
> > subject to a set of nonlinear constraints on it.
> >
> > And, yes, you will want to set up a function for at least
> > the nonlinear constraints. The objective function for
> > fmincon can be done as an anonymous function.
> >
> > If you prefer not to do it that way (or don't have the
> > optimization toolbox), you could also use fminbnd.
> >
> > a=13.5;
> > l=48;
> > crank=0:pi/900:2*pi;
> > phase=30;
> >
> > positionEX = @(crank) a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 - 59.75;
> > positionIN = @(crank) a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 - 58.75;
> >
> > See that I subtracted off the lower bounds, so you can
> > just look for zero crossings. Now use ezplot.
> >
> > ezplot(positionEX,[-50,50])
> > figure
> > ezplot(positionIN,[-50,50])
> >
> > Hmm. This points out that both functions are just sine
> > waves, shifted, scaled, phase shifted. It also indicates
> > there is NO minimum value of crank that satisfies both
> > constraints. If you can find one solution, then you can
> > find infinitely many solutions, all of which are less than
> > the chosen value. As such, no solver will succeed as you
> > have currently described the problem.
> >
> > This tells me you need to better define your problem.
> > Are you looking for the first non-negative solution?
> >
> > John
>
> I am trying to find the first 'crank' where both positionEXand positionIN are more than or equal to 59.75 and 58.75 respectively.
>
> The reason I am trying to do it this way is because I plan to change other variables and loop this code.

But you paid no attention to what I said. Read it again.

If any solution exists, then there will be infinitely many
of them. There will never be a FIRST solution. You can
always find a solution that is more negative than any
given solution.

John

Subject: Solving 2 Equations

From: Torsten

Date: 28 Feb, 2012 14:48:29

Message: 10 of 10

On 28 Feb., 15:44, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> "Michael Boyd" wrote in message <jiil4l$7b...@newscl01ah.mathworks.com>...
> > "John D'Errico" <woodch...@rochester.rr.com> wrote in message <jiikjq$5n...@newscl01ah.mathworks.com>...
> > > "Michael Boyd" wrote in message <jiiegm$gs...@newscl01ah.mathworks.com>...
> > > > Hi guys,
>
> > > > I am trying to find the value of variable, 'crank', that satisfies 2 equations.
>
> > > > Here is the script.
>
> > > > clc
> > > > clear all
>
> > > > a=13.5;
> > > > l=48;
> > > > crank=0:pi/900:2*pi;
> > > > phase=30;
>
> > > > positionEX=a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5;
> > > > positionIN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5;
>
> > > > I am trying to find the first 'crank' value when 'positionEX' is greater or equal to 59.75 AND when 'positionIN' is more than 58.75.
>
> > > > I am unsure how you would do this.
>
> > > Because you are thinking of it in the wrong way. You are
> > > NOT solving two equations. You wish to minimize crank,
> > > subject to a set of nonlinear constraints on it.
>
> > > And, yes, you will want to set up a function for at least
> > > the nonlinear constraints. The objective function for
> > > fmincon can be done as an anonymous function.
>
> > > If you prefer not to do it that way (or don't have the
> > > optimization toolbox), you could also use fminbnd.
>
> > > a=13.5;
> > > l=48;
> > > crank=0:pi/900:2*pi;
> > > phase=30;
>
> > > positionEX = @(crank) a*cos(crank)+(l^2-a^2*sin(crank).^2).^0.5 - 59.75;
> > > positionIN = @(crank) a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5 - 58.75;
>
> > > See that I subtracted off the lower bounds, so you can
> > > just look for zero crossings. Now use ezplot.
>
> > > ezplot(positionEX,[-50,50])
> > > figure
> > > ezplot(positionIN,[-50,50])
>
> > > Hmm. This points out that both functions are just sine
> > > waves, shifted, scaled, phase shifted. It also indicates
> > > there is NO minimum value of crank that satisfies both
> > > constraints. If you can find one solution, then you can
> > > find infinitely many solutions, all of which are less than
> > > the chosen value. As such, no solver will succeed as you
> > > have currently described the problem.
>
> > > This tells me you need to better define your problem.
> > > Are you looking for the first non-negative solution?
>
> > > John
>
> > I am trying to find the first 'crank' where both positionEXand positionIN are more than or equal to 59.75 and 58.75 respectively.
>
> > The reason I am trying to do it this way is because I plan to change other variables and loop this code.
>
> But you paid no attention to what I said. Read it again.
>
> If any solution exists, then there will be infinitely many
> of them. There will never be a FIRST solution. You can
> always find a solution that is more negative than any
> given solution.
>
> John- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

It seems the OP wants to restrict the variable "crank" to [0,2*pi],
as can be seen by the settings in his first post.

Best wishes
Torsten.

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