Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot a function
#1
I want to plot a function in a interval. I'm trying to do it - but I'm stuck. someone who can help?

The answer I get on the y axis also looks strange

def f(x):
    return x**5+x**3+x
n=100    
xlist = [i*n for i in range(-1,1,100)]
ylist = [f(x) for x in (xlist)]
print(xlist,ylist)

outpot: [-100] [-10001000100] 
Reply
#2
The output is right.

You have to look how range(-1, 1, 100) works.
Range starts with -1, then it adds 100 for the next step, then it's compared to end.
If the value is bigger then the end value, then StopIteration is raised.

If you want to have a range like -1, -0.9, -0.8 ... +1, then use a factor.
Range can only handle integer, not floats.

xlist = [x/1000 for x in range(-1000,1000,1)]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(May-21-2019, 09:48 AM)DeaD_EyE Wrote: The output is right.

You have to look how range(-1, 1, 100) works.
Range starts with -1, then it adds 100 for the next step, then it's compared to end.
If the value is bigger then the end value, then StopIteration is raised.

If you want to have a range like -1, -0.9, -0.8 ... +1, then use a factor.
Range can only handle integer, not floats.

xlist = [x/1000 for x in range(-1000,1000,1)]

Thank you, it made a little more sense. But if I want to avoid the float,
can I write it in this way:
def f(x):
    return x**5 + x**3 +x
n=100
dx= (1/n-1)
xlist = [i*dx for i in range(-1,1)]
ylist = [f(x) for x in xlist]
print = (xlist,ylist)
Reply
#4
Quote:Thank you, it made a little more sense. But if I want to avoid the float,
can I write it in this way:

...
xlist = [i*dx for i in range(-1,1)]
...

range(-1,1) returns then -1 and 0.
If you want to have more samples between -1 and 1, you have to use floats or use a bigger range.
If you use range(-1,2), it will yield also the 1:
[-1, 0, 1]

You can also change your factor (multiply by 1000) and change your range from (-1,1) to (-1000, 1000) and if you want to include the endpoint, then (-1000, 1001).


I show another example. If you want to calculate the coordinates x and y to draw a circle, you need a range of 2π, cosine for x and sine for y.
from math import sin
from math import cos
from math import tau
# tau == 2*pi

import matplotlib.pyplot as plt


def plot_circle(samples):
    pi_factor = tau / (samples - 1)
    pi_list = [pi_factor * p for p in range(samples)]
    x = [cos(p) for p in pi_list]
    y = [sin(p) for p in pi_list]
    plt.plot(x,y)
    plt.show()
Plotting a circle with 4 samples:
plot_circle(4)
Output:
   
The resulting list from range(samples) is: 0, 1, 2, 3
To get a circle and not a triangle, you need more points.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020