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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by buxZED
on 24 Feb 2011

y=@(T) I*T*sind(A)-(0.5*G*(T^2)); root = fzero(y, 0)

abouve eqation is for a projectile that projected at an angle (gose in a kind of ar half oval shape)

want to find the time it hist ground but fzero only gives me the biginning which is 0

*No products are associated with this question.*

Answer by Honglei Chen
on 17 Sep 2012

Accepted answer

The anwser is in the comments above

From Matt Fig

rt = 0; d = .1; cnt = 1; while rt<.1 rt = fzero(y,rt+d*cnt); cnt = cnt + 1; end

Matt Fig
on 17 Sep 2012

Digging deep, Honglei! How did you come across this one from over a year ago?

## 23 Comments

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3788

Maybe it just doesn't like buxZED... won't let me submit an answer to this one.

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3789

root = fzero(y,[eps, realmax])

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3791

any clue why?

can you post as a comment?

## Paulo Silva (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3792

Hey Walter, answers stop working for me too :(

We're sorry, but something went wrong.

We've been notified about this issue and we'll take a look at it shortly.

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3793

??? Error using ==> fzero at 260

Function values at interval endpoints must be finite and real.

Error in ==> p34 at 26

root = fzero(y,[eps, realmax])

>>

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3796

root = fzero(y,eps, realmax)

gives me a - answer, imposible

## Matt Fig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3797

Plot the function, look at the plot and pick a closer guess than zero.

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3799

@matt Fig

that was my inicial thought

but we are not alowed to use neumerical methods or guess work

## Matt Fig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3802

You do realize that FZERO is a numerical root finder, right?

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3806

is there a way to force fzero to go to the next positive root?

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3809

Ummm, make it fzero(y,[eps,sqrt(realmax)/G]

Using fzero(y,eps,realmax) would cause realmax to be interpreted as an Option, and then it would use eps as the starting guess, not constrained from going backwards.

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3811

No, there is no way to force fzero to go to the next positive root. You can only exclude the area you already searched from the area fzero is to search next.

## Matt Fig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3812

rt = 0;

d = .1;

cnt = 1;

while ~rt

rt = fzero(y,rt+d*cnt);

cnt = cnt + 1;

end

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3820

it works

but i want to understand what while ~rt means (what dose ~ say)

dose this stop counting as it finds the first root after zero?

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3821

~ is logical negation. It is true if and only if the thing being tested is exactly 0.

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3832

so dose this stops finding roots after it reached the 1st root after 0?

## Matt Fig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3834

When rt is not zero, then ~rt is false. That would stop the WHILE loop.

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3835

y=@(T) I*T*sind(A)-(0.5*G*(T^2));

%root = fzero(y, eps, realmax)

rt = 0;

d = .1;

cnt = 1;

while ~rt

rt = fzero(y,rt+d*cnt);

cnt = cnt + 1;

end

fprintf('the root is %g\n' , rt)

gives me the root is 7.11215e-026

but its not posible as the projectile takes around 1.5sec to reach maximum hight, the root should be around 3

can you point me in the right derection?

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3836

is there a way to systematicaly increase the T value till Y become zero and get that value?

## Matt Fig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3837

Change from:

while ~rt

to:

while rt<.1

or similar. It might help if you actually gave us some values for I, A and G.

## buxZED (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3838

it worked Matt

thank you so much

and also ~ would allow me to use matlab in many new ways

thank you again :D

## Matt Tearle (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3897

Can I suggest using an initial guess of 2*I*sind(A)/G? Just sayin'...

## Matt Fig (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1996#comment_3899

Come on now, Matt! Where is the fun of solving the equation with your brain instead of making MATLAB do it?