3

Im beginner at Python and I have a problem with this task:

  • Write a function which find roots of user's mathematical function using fixed-point iteration.
  • Use this function to find roots of: x^3 + x - 1.
  • Draw a graph of the dependence of roots approximation by the step number of iteration algorithm.

This is my first time using Python, so I really need help. This is my code, but its not working:

import math
import matplotlib.pyplot as plt
import numpy as np

def fixedp (function, x0, min = 0.001, max = 100):
  i = 0
  e = 1
  xp = []
  while (e > min and i < max):
    x = function(x0)
    e = norm(x0 - x)
    x0 = x
    xp.append(x0)
    i = i + 1
  return x, xp

fx = input("Wrote function : ")
function = lambda x: eval(fx)

x_start = 0.5
xf,xp = fixedp(function, x_start)

x = linspace(0,2,100)
y = function(x)
plot(x, y, xp, function(xp), 'bo', x_start, f(x_start), 'ro', xf, f(xf), 'go', x, x, 'k')
show()
  • 1
    Why is it not working? Have you debugged? Maybe give us an input and expected output? – Yonlif Apr 08 '20 at 14:24
  • ```i = 0 IndentationError: expected an indented block.``` I dont know how to fix it – nick_nick_nick Apr 08 '20 at 14:46
  • 1
    This is because you do not have `:` after the while. I recommend using some text editor to help you identify this kind of bugs. Pycharm, Noteped++ or Sublime are great – Yonlif Apr 08 '20 at 14:49
  • You should also know that specifically `x^3 + x - 1` will not converge. – Yonlif Apr 08 '20 at 15:04
  • I know and i dont know what to do with that. I wrote ``:`` after the while but there is the same error with ``i = 0`` – nick_nick_nick Apr 08 '20 at 15:15
  • You'll need to call `np.linspace` instead of `linspace` and `plt.plot` instead of `plot`. Also I would strongly advise that you don't use `eval` on user input as they could put any function, including system calls that read / modify your filesystem! – Simon Crane Apr 08 '20 at 16:09
  • Could you tell me how to do it without eval? – nick_nick_nick Apr 08 '20 at 16:15
  • There are some good ideas on this post: https://stackoverflow.com/a/9558001/12094894 – Simon Crane Apr 08 '20 at 18:26

1 Answers1

2

First of all I will note the the logic of your code is great and working. There are some issues with indentation and syntax so I rewrote your code.

import matplotlib.pyplot as plt
import numpy as np
from typing import Tuple, List
from math import *


def iteration(given_function, x0, min_error=0.001, max_iteration=3) -> Tuple[float, List]:
    i = 0
    error = 1
    xp = []
    x = None
    while error > min_error and i < max_iteration:
        x = given_function(x0)
        error = abs(x0 - x)
        x0 = x
        xp.append(x0)
        i += 1
    print(xp)
    return x, xp


def plot(xf, xp, x_start, given_function):
    function_v = np.vectorize(given_function)

    x = np.linspace(0, 2, 100)
    y = function_v(x)
    plt.plot(x, y)
    plt.plot(xp, function_v(xp), 'bo')
    plt.plot(x_start, given_function(x_start), 'ro')
    plt.plot(xf, given_function(xf), 'go')
    plt.plot(x, x, 'k')
    plt.show()


def main():
    fx = input("Write function: ")
    given_function = lambda x: eval(fx)

    x_start = 0.9
    xf, xp = iteration(given_function, x_start)

    plot(xf, xp, x_start, given_function)


if __name__ == '__main__':
    main()

Good luck with Python in the future!

Yonlif
  • 1,770
  • 1
  • 13
  • 31