To plot graph non linear function

Viewed 108

I want to plot graph of this function:

y = 2[1-e^(-x+1)]^2-2

When I plot a linear function, I used this code :

import matplotlib.pyplot as plt
import numpy as np

x = np.array(...)
y = np.array(...)
z = np.polyfit(x, y, 2)
p = np.poly1d(z)
xp = np.linspace(...)

_ = plt.plot(x, y, '.', xp, p(xp), '-')
plt.ylim(0, 200)
plt.show()

When the function is non-linear, it does not works becasue it hard to find each x,y value.

How can I plot a non-linear function?

1 Answers

I hate to be the one to break this news to you, but polynomials of order greater than one are technically nonlinear too.

When you plot in matplotlib, you're really supplying discreet x and y values at a resolution sufficient to be visually pleasing. In this case, you've chosen xp to determine the points you plot for the parabola. You then call p(xp) to generate an array of y-values at those locations.

There nothing stopping you from generating y-values for your formula of interest using simple numpy functions:

y = 2 * (1 - np.exp(1 - xp))**2 - 2
Related