Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polynomials
#1
I need to write a program that reads two numbers. Than used them in this polynomial: 3*x**3 - 5x + 0.8 and calculate all the values between them with a step of 0.5. I used range (value, value, 0.5). But only x will be defined, how do i define the other value? Any sugestions for a program like this?
Reply
#2
Show the code that you have written so far so that suggestions can be made
Reply
#3
#!/usr/bin/env python3
a = -5.0
b =  5.0

x = a
while x < b:
    print(x, 3*x**3 - 5*x + 0.8)
    x = x + 0.5
Reply
#4
For numerical operations you should look for numpy.
There is a convenience function for polynomials.

numpy.poly1d
Quote:A convenience class, used to encapsulate "natural" operations on
polynomials so that said operations may take on their customary
form in code (see Examples).

Here a small example with your values:

import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(-20, 20, 512) # from -20 to 20 (inclusive), 512 values
poly = np.poly1d([3, 0, -5, 0.8]) # your polynomial
y = poly(x)

plt.plot(x,y)
plt.show()
   
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Good idea with mathplot
Reply


Forum Jump:

User Panel Messages

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