Finding the right window for graphing function f(x), in order to show off its interesting features.

Andy Long
Fall, 2016

Clear["Global`*"] (* This removes any definitions that may be in place already; like if you've already been doing some Mathematica, and talking about other functions named f, x-values named a and b, etc. *)

Suppose

f[x_]:=(x^2-Sin[x])/4

What would be a good window in which to view this function?

Plot[f[x]]

Mathematica barfs if I don’t give it a window, but I have no idea. So I try something, kind of a standard in the calculator industry:

Plot[f[x],{x,-10,10}]

But is that a good window? Turns out it’s hiding some interesting action: let’s zoom in on the origin:

Plot[f[x],{x,-1,1}]

Now we’re on to something. We can see some roots -- places where the function is equal to 0 (equivalent to where the curve cuts the x-axis), and a minimum -- a place where the function is low. 

So how would we figure out the “best” window for showing off this function?

Plot[f[x],{x,-100,100}]

That just looks like a quadratic. That makes sense. When x gets big, the sine function stays around [-1,1], whereas the x^2 term is zooming off to infinity. So it kind of squashes the influence of the sine.

Plot[f[x],{x,-1,2},PlotLabel->f[x]]

That looks pretty good: it has the roots, and it has the minimum of the function. We don’t know exactly where these are found yet (although the origin looks like a root).

How would we find the x-values of the roots and the minimum? Those are problems that calculus can help us to solve.....

One thing that you could do is zoom in: if you do, and you zoom far enough in (and still catch the root) then you’ll see that the graph looks pretty linear (and the more you zoom, the more linear it looks!):

Plot[f[x],{x,.85,.90},PlotLabel->f[x]]

We might add in the straight line obtained by using two (arbitrary) points from the x axis, based on this graph of f: let me choose .87 and .88. They surround the true root.

a=.87;
b=.88;
fa=f[a]
fb=f[b]
m=(fb-fa)/(b-a)

linearapprox[x_]:=m(x-a)+fa
p1=Plot[{f[x],linearapprox[x]},{x,.85,.90},PlotLabel->f[x]];
p2=ListPlot[{{a,fa},{b,fb}}];
Show[{p1,p2}]

The root of linearapprox is very close to the root of f, as you can see: so we use the easily obtained root of linearapprox as our (approximate) solution. 
So we solve for where m(x-a)+fa=0 -- that’s easy!

root=a-fa/m
linearapprox[root] (* Note that it's not exactly a root! *)
f[root] (* And, unsurprisingly, it's not a root of f, either. *)
p3=ListPlot[{{root,f[root]}}];
Show[{p1,p2,p3}] (* Now let's plot everything together: *)

Now what does Mathematica say if we ask for a root of f?

Solve[f[x]==0,x]

Wow, with our primitive methods we’ve got a solution, and Mathematica doesn’t! We’re feeling powerful, I hope..... Let’s try NSolve -- that’s a numerical solver.

NSolve[f[x]==0,x]

Barfs again! Well, there’s one more trick up Mathematica’s sleeve: FindRoot. Notice that I have to give it a starting approximate value, so I try 2:

bestroot=FindRoot[f[x]==0,{x,2}]
bestroot=Replace[x,bestroot]

Hey! That’s pretty darned close to the answer we got with our silly straight line method. 
Maybe straight lines are more powerful than we think....

But we had to zoom in to get close, and then wasted time picking some points ourselves. Is there an automatic way?

Mathematica’s FindRoot is probably doing what I’m going to suggest next (if you all don’t suggest it first!). 

Newton’s method is another straight line approach, but it requires just a starting guess. Let’s see how this one works.

First of all, I’m going to use the derivative function, denoted f’(x) -- our star of the semester -- which we’ll be studying in much detail throughout the next 14 weeks.  It produces the slope of the graph at any point.

x1=2;
m=f'[x1];
newtons[x_]:= x - ((f[x])/(f'[x]))
Plot[{f[x],m(x-x1)+f[x1]},{x,bestroot-(x1-bestroot),x1}]
xnext=x1

N[xnext]
xnext=newtons[xnext];
N[xnext]
m=f'[xnext];
Plot[{f[x],m(x-xnext)+f[xnext]},{x,bestroot-(xnext-bestroot),xnext}]

Just for fun, let’s have a look at that derivative function f’[x], which is the star of the show in calculus: do you see that it represents the slope of the graph of f at any value of x? What values make perfect sense?

Plot[{f[x],f'[x]},{x,-1,2}]

xnext=2
bestroot=FindRoot[f'[x],{x,xnext}]
bestroot=Replace[x,bestroot]
newtons[x_]:= x - ((f'[x])/(f''[x]))

N[xnext]
xnext=newtons[xnext];
N[xnext]
m=f''[xnext];
Plot[{f'[x],m(x-xnext)+f'[xnext]},{x,bestroot-(xnext-bestroot),xnext}]