0

Given a function g(x), I want to find a fixed point to this function using fixed point iteration. Except for finding the point itself, I want to plot the graph to the function using matplotlib.pyplot, and include the vertical and horizontal bars that show how the iteration closes in on the fixed point (if one exists). Example picture

All help appreciated! /programming newbie

EDIT: Since I'm no too comfortable with generator objects yet, I've written the following code. It doesn't quite work though: what's wrong with it?

from matlibplot.axes import vlines, hlines

def fixpt(f, x, epsilon=1.0E-4, N=500, store=False):
    y = f(x)
    n = 0
    if store: Values = [(x, y)]
    while abs(y-x) >= epsilon and n < N:
        x = f(x)
        n += 1
        y = f(x)
        if store: Values.append((x, y))
        vlines(x, min(x, y), max(x, y), color='b')
        hlines(y, min(y, x), max(y, x), color='b')
    if store:
        return y, Values
    else:
        if n >= N:
            return "No fixed point for given start value"
        else: 
            return x, n, y
1
  • Cobweb plot Commented Dec 23, 2023 at 10:05

3 Answers 3

2
def fixedpoint(f,x):
    while x != f(x):
        yield x
        x = f(x)
    yield x

Usage: fixedpoint(g,some_starting_value).

Vertical and horizontal bars depend on plotting library. Specify which one you use.

Sign up to request clarification or add additional context in comments.

2 Comments

fixedpoint(g,some_starting_value) In this case, is g the derivative of a function f ?
@bibscy, no, it's the function you want to find fixed point of. OP wanted to find the fixed point of g, so the example usage shows that.
1

Your function looks fine. I am not familiar with vlines and hlines. I used your store arg to get the points, and plot them outside the function (it is generally better to separate problems like this).

I used only the plot function from matplotlib.pyplot, and the show function to display the graph.

from matplotlib import pyplot as plt
import numpy as np

def fixpt(f, x, epsilon=1.0E-4, N=500, store=False):
    y = f(x)
    n = 0
    if store: Values = [(x, y)]
    while abs(y-x) >= epsilon and n < N:
        x = f(x)
        n += 1
        y = f(x)
        if store: Values.append((x, y))
    if store:
        return y, Values
    else:
        if n >= N:
            return "No fixed point for given start value"
        else: 
            return x, n, y

# define f
def f(x):
    return 0.2*x*x

# find fixed point
res, points = fixpt(f, 3, store = True)

# create mesh for plots
xx = np.arange(0, 6, 0.1)

#plot function and identity
plt.plot(xx, f(xx), 'b')
plt.plot(xx, xx, 'r')

# plot lines
for x, y in points:
    plt.plot([x, x], [x, y], 'g')
    plt.plot([x, y], [y, y], 'g')

# show result
plt.show()

Comments

0

Here is how I thought it through :

from pylab import *

def f(x):
  return 8*x/(1 + 2*x)

def cobweb(x0, n, ax):
  xs = [x0]
  ys = [0]
  for i in range(1,n):
    if i % 2 == 0:
      xs.append(ys[-1])
      ys.append(ys[-1])
    else:
      xs.append(xs[-1])
      ys.append(f(xs[-1]))
  ax.plot(xs, ys, 'k--', lw=2.0)

x = linspace(0, 4, 100)

fig = figure()
ax  = fig.add_subplot(111)

ax.plot(x, x,    'k', lw=2.0)
ax.plot(x, f(x), 'r', lw=2.0)
cobweb(0.5, 50, ax)

ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')

grid()
show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.