MATLAB Answers

0

Cannot use fzero function

Asked by Tugulea Alexa on 9 May 2019
Latest activity Edited by Stephan
on 9 May 2019
%this is my funcion
funcion y = funct(x)
y=7.*sin(2.*pi.*x).^2
>> x=fzero('funct',pi/2);
??? Error using ==> fzero
FZERO cannot continue because user supplied function_handle ==> funct
failed with the error below.
Error using ==> feval
Undefined function 'funct'.
How can I solve it?

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Stephan
on 9 May 2019
Edited by Stephan
on 9 May 2019

i recommend to use fminunc:
x=fminunc(@funct,pi/2);
function y = funct(x)
y=7.*sin(2.*pi.*x).^2
end

  2 Comments

+1, but note that fminunc wil work here only because the minimum will be where the function value is also zero.
Had the zero been of a different function where the root is at a zero crossing, then fminunc would not be appropriate.
knowing that the minimum function value can only be zero, due to the '^2' - it is easy to decide for this way.
But i am always a fan of your answers and i am always learning by reading your contributions.
thx4this!

Sign in to comment.


Answer by John D'Errico
on 9 May 2019
Edited by John D'Errico
on 9 May 2019

My guess is you did not actually save that function as an m-file on your search path. This is why MATLAB could not find the function.
Another possibility is that you saved it using the invalid keyword funcion, missspelling the work function. That will probably cause the code to fail too.
funcion y = funct(x)
y=7.*sin(2.*pi.*x).^2
Instead, I suggest you simply learn to use function handles, of this form:
funct = @(x) 7.*sin(2.*pi.*x).^2;
A virtue of a function handle as written there, is you need never even save an m-file.
However, as you can see what happens when we try to use fzero, it fails.
fzero(funct,1.25)
Exiting fzero: aborting search for an interval containing a sign change
because NaN or Inf function value encountered during search.
(Function value at -2.87631e+307 is NaN.)
Check function or try again with a different starting value.
ans =
NaN
This is because fzero absolutely needs to see a zero crossing. And since the square of the sine function is ALWAYS non-negative, then fzero must fail.
For this reason, Stephan is correct (and why I gave a +1 there) in that fzero will not work. However, a better choice than either fzero of fminbnd is a tool like fsolve.
[x,fval,exitflag] = fsolve(funct,1.25)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x =
1.5
fval =
9.4485e-31
exitflag =
1
As you can see, it was quite successful, whereas, on a very similar function, perhaps as close as:
funct = @(x) 7.*sin(2.*pi.*x).^2 - 0.1;
fminbnd will clearly give the wrong solution, because the minimum and the zero do not occur at the same place.
Note that fsolve will handle both problems with no issues.

  0 Comments

Sign in to comment.