Filter löschen
Filter löschen

How to use fzero and choose correctly initial guess?

13 Ansichten (letzte 30 Tage)
Khanh
Khanh am 26 Okt. 2014
Kommentiert: Star Strider am 27 Okt. 2014
Hi,
May I know how to solve the following equation with fzero and get two numbers: -2 and +2.
When I used the following code, it returned x=-2
fzero(@(x) x^2-4,0)
And this returned x=+2
fzero(@(x) x^2-4,1)
And furthermore, how can I choose correctly the initial guess number? I found that the answer will depend on the initial guess.

Akzeptierte Antwort

Star Strider
Star Strider am 26 Okt. 2014
Whenever I have a function I want to use fzero with, I plot it to see how many zeros it has and about where they are. I then choose my initial estimates based on that.
A more mathematically correct approach involves taking the vector you want the zeros of, then multiplying it by a one-position circularly-shifted version of itself, using the circshift function. This produces negatives at the zero crossings that are easy to test for and determine the indices of using the find function. I then use the x-values at those indices as the initial estimates. The initial estimates don’t have to be perfect, just close enough. If there are several zero-crossings, you will get several answers from fzero. It is up to you to choose the ‘correct’ zeros.
  4 Kommentare
Khanh
Khanh am 27 Okt. 2014
Great thanks!. It makes me easier to understand your means.
Star Strider
Star Strider am 27 Okt. 2014
My pleasure! I apologise for not including that in my original answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Optimization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by