简体   繁体   中英

How can I check to see the number of iterations Newton's method takes to run?

So basically I want to grab the number of iterations it takes my newton's method to find the root, and then take that number and apply it to my color scheme to make the longer the amount of iterations, the darker the color, and the fewer, the more full the color.

so here's my code

from numpy import *
import pylab as pl
def myffp(x):
    return x**3 - 1, 3*(x**2)    
def newton( ffp, x, nits):
    for i in range(nits):
        #print i,x
        f,fp = ffp(x)
        x = x - f/fp
    return x    
q = sqrt(3)/2
def leggo(xmin=-1,xmax=1,jmin=-1,jmax=1,pts=1000,nits=30):
    x = linspace(xmin, xmax, pts)
    y = linspace(jmin, jmax, pts)*complex(0,1)
    x1,y1 = meshgrid(x,y)                   
    n = newton(myffp,x1+y1,nits)                  #**here is where i wanna see the number of iterations newton's method takes to find my root** 
    r1 = complex(1,0)  
    r2 = complex(-.5, q)
    r3 = complex(-.5,-q)
    data = zeros((pts,pts,3))   
    data[:,:,0] = abs(n-r1)         #**and apply it here**
    data[:,:,2] = abs(n-r2)
    data[:,:,1] = abs(n-r3)
    pl.show(pl.imshow(data))    
leggo() 

The main problem is finding the number of iterations, I can then figure out how to apply that to darkening the color, but for now it's just finding the number of iterations it takes for each value ran through newton's method.

Perhaps the simplest way is to just refactor your newton function so that it keeps track of the total iterations and then returns it (along with the result, of course), eg,

def newton( ffp, x, nits):
    c = 0                   # initialize iteration counter
    for i in range(nits):
        c += 1              # increment counter for each iteration 
        f, fp = ffp(x)
        x = x - f/fp
    return x, c             # return the counter when the function is called

so in the main body of your code, change your call to newton , like so:

res, tot_iter = newton(myffp, x, nits)

the number of iterations in the last call to newton is stored in tot_iter


As aside, your implementation of Newton's Method seems to be incomplete.

for instance, it's missing a test against some convergence criterion.

Here's a simple implementation in python that works:

def newtons_method(x_init, fn, max_iter=100):
    """
    returns: approx. val of root of the function passed in, fn;
    pass in: x_init, initial value for the root; 
    max_iter, total iteration count not exceeded;
    fn, a function of the form: 
    def f(x): return x**3 - 2*x
    """
    x = x_init
    eps = .0001
    # set initial value different from x_init so at lesat 1 loop
    x_old = x + 10 * eps        
    step = .1
    c = 0
    # (x - x_old) is convergence criterion
    while (abs(x - x_old) > eps) and (c < max_iter):
        c += 1
        fval = fn(x)
        dfdx = (fn(x + step)) - fn(x) / step
        x_old = x
        x = x_old - fval / dfdx
    return x, c

The code you're currently using for newton() has a fixed number of iterations ( nits - which is being passed in as 30), so the results would be kind of trivial and uninteresting.

It looks like you're trying to generate a Newton fractal -- the method you're trying to use is incorrect; the typical coloring mode is based on the output of the function, not the number of iterations. See the Wikipedia article for a full explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM