Skip to article frontmatterSkip to article content

Graf polinoma 4tog stupnja

import numpy as np
import matplotlib.pyplot as plt

Graf polinoma 4tog stupnja

Polinom 4tog stupnja bit će definiran kao

P(x)=ax4+bx3+cx2+dx+e,P(x) = ax^4 + bx^3 + cx^2 + dx + e,

a,b,c,d,e[5,5]a, b, c, d, e \in [-5, 5]

x[10,10]x \in [-10, 10]
xs = np.linspace(-10, 10, 100)
coeffs = np.random.uniform(-5, 5, 5)
powers = [xs**4, xs**3, xs**2, xs**1, xs**0]
ys = np.dot(coeffs, powers)
def create_title(coeffs): # Ispisuje slučano generirane koeficijente u obliku f(x) = ax^4 + bx^3 + cx^2 + dx + e
    return f"f(x) = {coeffs[0]}x^4{'+' if coeffs[1] > 0 else ''}{coeffs[1]}x^3{'+' if coeffs[2] > 0 else ''}{coeffs[2]}x^2{'+' if coeffs[3] > 0 else ''}{coeffs[3]}x{'+' if coeffs[4] > 0 else ''}{coeffs[4]}"

ax = plt.gca()
ax.plot(xs, ys)
ax.set_title(create_title(coeffs))
<Figure size 640x480 with 1 Axes>