Finding Roots of a Function

If I use fzero to find the roots of a function, in one instance I evaluate when x = 4.2 and get a root of 3.6652. In another instance I evaluate the function at x = 4.3 and get a root of 4.7124. Why might these roots be different at such close numbers?

9 Comments

The easiest way to understand what your function is doing is to plot it.
Then you know.
Your function might have two roots, hence two different results. What value do you get when you plug those numbers to your function? Both give close to or equal to 0?
John D'Errico
John D'Errico on 30 Nov 2018
Edited: John D'Errico on 30 Nov 2018
So, you knew you were not supposed to ask for help on your homework, yet you asked it anyway, and now after you got your answer, you want to delete the question, to hide your poor choice. Your poor decision making is not our problem. In fact, leaving this question here is perhaps good for you, to help you learn the lesson you should have known already.
Next time don't ask a question that you are not allowed to ask.
Breanna Elliott
Breanna Elliott on 30 Nov 2018
Edited: Breanna Elliott on 30 Nov 2018
No, John, this question was asked September 22nd because quite frankly I paid to use this program. I did not find out until today, November 29th, that it is wrong for us to ask questions on this site. If it stays up then I hope that it is of help to someone else. I thank the ones for answering for taking the time to help.
There is no charge for using MATLAB Answers. Most of the service in MATLAB Answers is provided by volunteers. The volunteer answer publicly placed questions with publicly placed answers, so that everyone who cares to look has an opportunity to learn and contribute.
The most active volunteers are fairly firm in their dislike of having questions closed or edited away after they have volunteered their time, as that kind of removal turns the transaction into free private consulting, and they did not agree to be providing free private consulting.
The question you asked is not one where you asked how to write code to solve your homework problem. Had you done that, I would not have spent the time to be one of those to answer your question. (I might have closed it instead.) So asking the rather innocuous question that you did should not be a problem anyway. It was really more of a question about mathematics, and how root finders react to subtly different starting points. Finally, I did answer your question because it seemed to show some interest in understanding a code like fzero and why it behaves as it does. Interest in learning should never be proscribed, and is not something your teacher would want to prohibit.
Yes John, that is why I asked it because I didn't understand fzero and I appreciate the input you gave. It was not a direct homework question. Posting on this site in regards to homework questions is prohibited, though, and I did not know that until some classmates were caught today posting our whole course project online. I was not asking for it to be deleted just because I received an answer.
You can contact John Kelly to request the removal, but removing it would be against policy. In over 7 years, I only know of one case in which it was done, in a situation involving proprietary information revealed by a (non-Mathworks) employee. Historically, we have refused university professors directly asking for students' questions to be removed.
John D'Errico
John D'Errico on 30 Nov 2018
Edited: John D'Errico on 30 Nov 2018
Suppose I just bought a new car. A nice shiny new one, that has a speedometer that goes to 150 miles per hour. I paid a lot of money for it, so it is time to use my shiny new toy.
Rev it up, pull out onto the freeway, and push that little baby up to 150 MPH. Of course, the nice policeman does not see it my way, that I claim to have not known the speed limit was only 55 mph seems irrelevant to him. So can you let me go and skip the ticket? Lets just forget it ever happened? Hey, those handcuffs hurt!
As I said, I don't think your instructor should get bent out of shape for asking a mathematical question about something that you did not understand. As I said, this did not appear to be a doiit4me, where you were asking for someone to do your work for you. I'd not have answered it in the first place if you did.
Now I don't know where your question arose, in what context. Suppose your homework assignment was to use fzero, starting at two distinct places, and asking why you get significantly different results each time. Then you posted this question, asking for someone to explain exactly what it was your teacher wanted YOU to explain? Clearly you would deserve jail time, or at least need to stand in front of a judge to receive your sentence.
On the other hand, suppose you were using fzero to solve a problem, and noticed this interesting event, but did not understand what was happening? Then asking a question on this or any site would be encouraged, because it arose from your own curiosity about the code. The two cases are fundamentally different, although your instructor may have a hard line on posting any question at all because too many students have chosen to post their homework.
We cannot know which case this is, and only your instructor can serve a sentence. But hiding it under a rug seems wrong.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 22 Sep 2018
You probably used a Newton-style method. Newton-style methods typically involve a prediction step that can overshoot the true root badly enough that the predicted location ends up closer to a different root. This can happen multiple times from a given starting point, and the exact location ended up in can depend upon the numeric accuracy of the calculations, so you can end up finding a root several roots away from the starting one. The details turn out to be one of the significant ways that fractal images are generated.
Close is a relative thing.
For example, suppose you have the function
f_x = @(x) (x-1).*(x-1.5);
Now it should be perfectly clear that it has two roots, one at x==1, and the second at x==1.5. Are they close? Again, it is all relative.
If you start a root finder close to one of the roots, it will find that root. Start it near the other root, and you get the second root. That the two roots are close is just your perception of what "close" means in context.
fzero(f_x,1.28)
ans =
1
fzero(f_x,1.3)
ans =
1.5
So here, I started fzero at two points that were quite "close", yet it finds a different root. Does that make sense? OF COURSE IT DOES!!!!!
In fact, fzero will find one or the other of those roots if I started it out at any point on the real line. So given two roots, and a solver that can find both of them, depending on where I start it out, then there must be at least one location where if I change my start point by some infinitesimal amount, that tiny change will result in finding the other root.

Tags

Asked:

on 22 Sep 2018

Edited:

on 30 Nov 2018

Community Treasure Hunt

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

Start Hunting!