You do not need to do the next command when using the IPython notebook on SageMathCloud, but it may be needed if you open this notebook on another system where the defaults are set differently. It does "from pylab import *" so numpy and plotting commands are available, and sets it up for plots to appear in the notebook webpage
In [4]:
%pylab inline
In [5]:
def newton(fvals, x0, tol):
xk = x0
kmax = 30 # maximum number of iterations
print " k xk f(xk)"
for k in range(kmax):
fxk, fpxk = fvals(xk) # evaluate f(x) and f'(x)
print "%4i %22.15f %22.15f" % (k, xk, fxk)
if abs(fxk) < tol:
break #leave the loop if tolerance satisfied
xk = xk - fxk / fpxk # update xk using Newton's method
return xk
Find the two points where the circle and line intersect in the plot below. The equation of the circle is $(x-1)^2 + y^2 = 4$ and the curve $y = \cos(x)$. Combine these equations by plugging $\cos(x)$ in for $y$ in the equation for the circle. This gives a single nonlinear equation in $x$ that you can solve using Newton's method.
Try to find both intersections and then replot the circle with these two points as red dots.
In [15]:
theta = linspace(0, 2*pi, 1000)
x = 1 + 2*cos(theta)
y = 2*sin(theta)
plot(x,y,'b')
x = linspace(-2,4,1000)
y = cos(x)
plot(x,y,'b')
axis('scaled') # so circle looks circular
Out[15]:
In [16]:
def fvals(x):
f = (x-1)**2 + cos(x)**2 - 4.
fprime = 2*(x-1) - 2*cos(x)*sin(x)
return f,fprime
In [17]:
x = linspace(-2,4,1000)
fx, fpx = fvals(x)
plot(x,fx)
plot(x,zeros(len(x)), 'k') # x-axis
Out[17]:
Find the two roots via two different starting values:
In [18]:
x0 = -1.
xk1 = newton(fvals, x0, 1e-10)
In [19]:
x0 = 2.
xk2 = newton(fvals, x0, 1e-10)
Plot the solutions found:
In [20]:
theta = linspace(0, 2*pi, 1000)
x = 1 + 2*cos(theta)
y = 2*sin(theta)
plot(x,y,'b')
x = linspace(-2,4,1000)
y = cos(x)
plot(x,y,'b')
axis('scaled') # so circle looks circular
plot([xk1,xk2], [cos(xk1),cos(xk2)],'ro')
Out[20]:
In [20]: