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:
is a loop going to be the best method?

Subject: is a loop going to be the best method?

From: Will Horton

Date: 9 Feb, 2009 13:36:01

Message: 1 of 9

I have a problem involving calculating the force required to launch a projectile vertically 10m.
It is simple enough to calculate the kinetic energy it initially needs, but the problem occurs when the drag force is included as it is dependant on the velocity.
I was just wondering if the best method for me to use would be to create a loop that plots all of the heights that the object will reach for a range of initial velocities.

Thanks
Will

Subject: is a loop going to be the best method?

From: David

Date: 9 Feb, 2009 13:44:01

Message: 2 of 9

"Will Horton" <s0680024@sms.ed.ac.uk> wrote in message <gmpbg1$doh$1@fred.mathworks.com>...
> I have a problem involving calculating the force required to launch a projectile vertically 10m.
> It is simple enough to calculate the kinetic energy it initially needs, but the problem occurs when the drag force is included as it is dependant on the velocity.
> I was just wondering if the best method for me to use would be to create a loop that plots all of the heights that the object will reach for a range of initial velocities.
>
> Thanks
> Will

that is of course one way to do it if you can't come up with a closed form solution for the equations.

Subject: is a loop going to be the best method?

From: John D'Errico

Date: 9 Feb, 2009 14:06:01

Message: 3 of 9

"Will Horton" <s0680024@sms.ed.ac.uk> wrote in message <gmpbg1$doh$1@fred.mathworks.com>...
> I have a problem involving calculating the force required to launch a projectile vertically 10m.
> It is simple enough to calculate the kinetic energy it initially needs, but the problem occurs when the drag force is included as it is dependant on the velocity.
> I was just wondering if the best method for me to use would be to create a loop that plots all of the heights that the object will reach for a range of initial velocities.
>
> Thanks
> Will

This is generally a poor solution for that class of
problem. While it will work to some extent, suppose
you had two or three parameters? Would you then
evaluate your function for all possible combinations
of the several parameters?

Better is to enter the world of optimization.
Optimization tools attempt to search for a minimum
or maximum value of some objective function, or
to find a root of some objective function. In your
case, you probably want to use fzero.

fzero will take a continuous supplied function of one
parameter and find a value of its input parameter
that returns zero (within a tolerance.) In your case,
I'll assume that you have a function that returns the
final maximum height achieved due to an initial
kinetic energy imparted to the projectile. Suppose
you were to subtract that desired height from your
function? Then when you choose the correct
initial kinetic energy, you should get a resulting
difference of zero.

Learn to use optimization tools to solve these
problems.

John

Subject: is a loop going to be the best method?

From: Roger Stafford

Date: 9 Feb, 2009 19:59:01

Message: 4 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gmpd89$7np$1@fred.mathworks.com>...
> .....
> In your case, you probably want to use fzero.
> .....

  Here's a question for you, John. To use 'fzero', Will would have to solve a differential equation for each different initial velocity with the resulting height to be used as the output of a user-defined function. Can he be sure that 'fzero' will only call on this function one initial velocity at a time? Some of the optimization functions feel free to make calls on objective functions with an entire vector of different values simultaneously and that could be a disaster timewise in this case if 'fzero' behaved that way. Or to put the question another way, can 'fzero' be instructed to be very parsimonious in the number of different calls it makes on its given objective function?

Roger Stafford

Subject: is a loop going to be the best method?

From: John D'Errico

Date: 9 Feb, 2009 23:46:02

Message: 5 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gmq1u5$3jv$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gmpd89$7np$1@fred.mathworks.com>...
> > .....
> > In your case, you probably want to use fzero.
> > .....
>
> Here's a question for you, John. To use 'fzero', Will would have to solve a differential equation for each different initial velocity with the resulting height to be used as the output of a user-defined function. Can he be sure that 'fzero' will only call on this function one initial velocity at a time? Some of the optimization functions feel free to make calls on objective functions with an entire vector of different values simultaneously and that could be a disaster timewise in this case if 'fzero' behaved that way. Or to put the question another way, can 'fzero' be instructed to be very parsimonious in the number of different calls it makes on its given objective function?
>
> Roger Stafford

If you are worried about non-vectorized calls,
even if fzero does make multiple calls at distinct
parameter values, this is quite easy to trap,
calling the objective function in a loop. I don't
believe that is an issue however.

According to the help for fzero:

"FUN accepts real scalar input X and returns a real
scalar function value F, evaluated at X."

This suggests that fzero presumes its objective
function is not vectorized.

As for the total number of calls, a good 1-d
rootfinder like fzero will typically call its objective
function at a minimum of about 8-10 points,
but rarely more than about 20-25 times. (These
are guesses from when I wrote a few such tools
in my deep dark past writing in APL.) The idea
in the code is to use an algorithm that is quickly
convergent on functions that are well behaved,
but has guards on it so that for poorly behaved
functions, it can temporarily revert to a stable
scheme like bisection that has a known behavior.

John

Subject: is a loop going to be the best method?

From: Roger Stafford

Date: 10 Feb, 2009 00:17:02

Message: 6 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gmqf7q$hkj$1@fred.mathworks.com>...
> ......
> According to the help for fzero:
>
> "FUN accepts real scalar input X and returns a real
> scalar function value F, evaluated at X."
>
> This suggests that fzero presumes its objective
> function is not vectorized.
> ......

  Yes, I think you must be right, John. The documentation which I consulted (and didn't read thoroughly enough) says: "fun is the function whose zero is to be computed. It accepts a vector x and returns a scalar f, ...", the vector property presumably allowing for multiple-variable functions.

Roger Stafford

Subject: is a loop going to be the best method?

From: Steven Lord

Date: 10 Feb, 2009 16:47:28

Message: 7 of 9


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:gmqh1u$nd8$1@fred.mathworks.com...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message
> <gmqf7q$hkj$1@fred.mathworks.com>...
>> ......
>> According to the help for fzero:
>>
>> "FUN accepts real scalar input X and returns a real
>> scalar function value F, evaluated at X."
>>
>> This suggests that fzero presumes its objective
>> function is not vectorized.

That's correct.

>> ......
>
> Yes, I think you must be right, John. The documentation which I
> consulted (and didn't read thoroughly enough) says: "fun is the function
> whose zero is to be computed. It accepts a vector x and returns a scalar
> f, ...", the vector property presumably allowing for multiple-variable
> functions.

Roger,

I'm not sure FZERO ever supported multi-variable functions. Are you sure
you're not reading the documentation for FSOLVE, which is in Optimization
Toolbox? FSOLVE supports multivariable systems, while FZERO (which is in
MATLAB itself) does not.

--
Steve Lord
slord@mathworks.com

Subject: is a loop going to be the best method?

From: Roger Stafford

Date: 10 Feb, 2009 19:39:01

Message: 8 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <gmsb30$lma$1@fred.mathworks.com>...
> Roger,
>
> I'm not sure FZERO ever supported multi-variable functions. Are you sure
> you're not reading the documentation for FSOLVE, which is in Optimization
> Toolbox? FSOLVE supports multivariable systems, while FZERO (which is in
> MATLAB itself) does not.
> --
> Steve Lord
> slord@mathworks.com

  Steve, I was misled by that sentence in the 'fzero' documentation. I quoted it accurately. It says: "fun is the function whose zero is to be computed. It accepts a vector x and returns a scalar f, the objective function evaluated at x." I think the word 'vector' must be a typographical error. It doesn't make any sense as a vector unless the output of 'fun' is also a vector of the same size. Also John's quotation of the help message stating that "FUN accepts real scalar input" bears this out.

Roger Stafford

Subject: is a loop going to be the best method?

From: Steven Lord

Date: 10 Feb, 2009 19:49:31

Message: 9 of 9


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:gmsl4l$3qo$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <gmsb30$lma$1@fred.mathworks.com>...
>> Roger,
>>
>> I'm not sure FZERO ever supported multi-variable functions. Are you sure
>> you're not reading the documentation for FSOLVE, which is in Optimization
>> Toolbox? FSOLVE supports multivariable systems, while FZERO (which is in
>> MATLAB itself) does not.
>> --
>> Steve Lord
>> slord@mathworks.com
>
> Steve, I was misled by that sentence in the 'fzero' documentation. I
> quoted it accurately. It says: "fun is the function whose zero is to be
> computed. It accepts a vector x and returns a scalar f, the objective
> function evaluated at x." I think the word 'vector' must be a
> typographical error. It doesn't make any sense as a vector unless the
> output of 'fun' is also a vector of the same size. Also John's quotation
> of the help message stating that "FUN accepts real scalar input" bears
> this out.

I think you're correct that the 'vector' in that statement was a typo.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

No tags are associated with 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