Cannot use fzero function

9 次查看(过去 30 天)
Tugulea Alexa
Tugulea Alexa 2019-5-9
编辑: Stephan 2019-5-9
%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?

回答(2 个)

Stephan
Stephan 2019-5-9
编辑:Stephan 2019-5-9
i recommend to use fminunc:
x=fminunc(@funct,pi/2);
function y = funct(x)
y=7.*sin(2.*pi.*x).^2
end
  2 个评论
John D'Errico
John D'Errico 2019-5-9
编辑:John D'Errico 2019-5-9
+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.
Stephan
Stephan 2019-5-9
编辑:Stephan 2019-5-9
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!

请先登录,再进行评论。


John D'Errico
John D'Errico 2019-5-9
编辑:John D'Errico 2019-5-9
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.

类别

Help CenterFile Exchange 中查找有关 Function Handles 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by