What does fzero take as input for the function?

1 view (last 30 days)
If i have a function that starts as follows:
function [y,index] = findsmode(x)
and if i then use
sigma=fzero(@findmode,500);
then what does the function findmode take as input x?
Also, if i put
disp(*)
within the function file with * being some variable calculated in the function, why does the value show up in my command window multiple times, while it is not being iterated.
I hope someone can explain this to me

Accepted Answer

Steven Lord
Steven Lord on 22 Jan 2021
then what does the function findmode take as input x?
fzero will call your findmode function repeatedly with various values as it tries to search for a root of findmode.
You can kind of think of this like someone walking along a path. They look left one pace to determine if that location on the path is higher or lower than where they are, then look right one pace to determine the same thing, decide which way to step based on whether they're higher or lower than their destination, and repeat this process.
why does the value show up in my command window multiple times, while it is not being iterated.
You may not have a loop inside your function but fzero will call your function repeatedly as part of its search.
  3 Comments
Steven Lord
Steven Lord on 22 Jan 2021
Use optimset to set the 'Display' and/or 'OutputFcn' options.
fzero(@sin, 1, optimset('Display', 'iter'))
Search for an interval around 1 containing a sign change: Func-count a f(a) b f(b) Procedure 1 1 0.841471 1 0.841471 initial interval 3 0.971716 0.825854 1.02828 0.856414 search 5 0.96 0.819192 1.04 0.862404 search 7 0.943431 0.809577 1.05657 0.870673 search 9 0.92 0.795602 1.08 0.881958 search 11 0.886863 0.775093 1.11314 0.897089 search 13 0.84 0.744643 1.16 0.916803 search 15 0.773726 0.698805 1.22627 0.941237 search 17 0.68 0.628793 1.32 0.968715 search 19 0.547452 0.520513 1.45255 0.993017 search 21 0.36 0.352274 1.64 0.997606 search 23 0.0949033 0.0947609 1.9051 0.94464 search 24 -0.28 -0.276356 1.9051 0.94464 search Search for a zero in the interval [-0.28, 1.9051]: Func-count x f(x) Procedure 24 -0.28 -0.276356 initial 25 0.214567 0.212924 interpolation 26 -0.00065816 -0.00065816 interpolation 27 5.06172e-06 5.06172e-06 interpolation 28 -3.62624e-13 -3.62624e-13 interpolation 29 1.54849e-24 1.54849e-24 interpolation 30 1.54849e-24 1.54849e-24 interpolation Zero found in the interval [-0.28, 1.9051]
ans = 1.5485e-24
As far as I'm aware there isn't a predefined OutputFcn that plots x and the value of the function at the x value for each iteration but it is possible to write one. See this documentation page for more information.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!