help in fzero function

Here x, y,teta are vectors. When I call the function fzero it shows this message:
fzero(@(t)shkruajek(x,y,1,3,teta,0.1,4,t),0)
Exiting fzero: aborting search for an interval containing a sign change
because NaN or Inf function value encountered during search.
(Function value at -2.54232e+306 is NaN.)
Check function or try again with a different starting value.
NaN
can you help me to determine where is the problem here??
function [ek]=shkruajek(x,y,i,j,teta,speed,R,t)
ek=sqrt(((x(i)+ speed*cos(teta(i)*t)) - (x(j) + speed*sin(cos(teta(j)*t))))^ 2+ ((y(i) + speed*sin(teta(i)*t)) - (y(j) + speed*sin(teta(j)*t))).^ 2)-R;
end
a=fzero(@(t)shkruajek(x,y,i,j,teta,speed,R,t),0);

4 Commenti

John Chilleri
John Chilleri il 13 Gen 2017
Is it possible your function doesn't have a root?
ana take
ana take il 13 Gen 2017
I am sure that the root exist,if it is not real it should be imaginary.Maube i have'nt understand what fzero does. I tried this example: function [ekuac]=prova(R,t) ekuac=3*(t^2)+R; end
and I got this response function [ekuac]=prova(R,t) ekuac=3*(t^2)+R; end
the root of this equation is imaginary, why fzero produces NaN??
the response I got: ai = fzero(@(t)prova(R,t),0) Exiting fzero: aborting search for an interval containing a sign change because NaN or Inf function value encountered during search. (Function value at -8.581e+153 is Inf.) Check function or try again with a different starting value.
ai =
NaN
Walter Roberson
Walter Roberson il 13 Gen 2017
Note that only the newer versions of MATLAB support fzero on complex-valued functions.

Accedi per commentare.

 Risposta accettata

Walter Roberson
Walter Roberson il 13 Gen 2017

0 voti

You only provided a single bound, 0, to fzero. It makes guesses about where to look, and it happens to probe at -2.54232e+306 . Something in the calculation there is resulting in NaN being produced.
My guess is that at -2.54232e+306 you have multiple sub-expressions come out as infinite, and that you end up with infinity minus infinity (or negative infinity plus positive infinity). infinity minus infinity is NaN.
You should be considering giving two element bounding vector to fzero instead of just a single location.

Più risposte (1)

John Chilleri
John Chilleri il 13 Gen 2017
Modificato: John Chilleri il 13 Gen 2017

1 voto

Hello,
In addition to what Walter Roberson said as good practice, it would appear that your function does not have a root.
I say this because your t is only found within sine and cosine functions, and after fzero has tested one period then no other value of t will make a difference, so the fact that it does not find a root within one period and rather continues to eventually error at -2.5 e306 suggests there is no root.
Hope this helps!

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by