Numpy.root gibt den korrekt gezeichneten x-Achsenabschnitt zurück, beschriftet ihn jedoch in Python-in-Excel falsch
Posted: 22 Dec 2024, 19:26
Ich erstelle Programme, die Arbeitsblätter für Schüler erstellen. In diesem speziellen Programm muss der Student ein in faktorisierter Form gegebenes Polynom auf einem leeren Gitter skizzieren.

Ich habe Mit einem Grafikrechner wird bestätigt, dass die Koeffizienten korrekt sind. Sie produzieren die erwarteten Wurzeln. Ich habe das Python-Skript in Spyder ausgeführt und es hat wie erwartet funktioniert, wobei alle Wurzeln korrekt beschriftet waren. Hier ist das Python-Skript, das in die dritte Tabellenzelle von links eingefügt wird:
import numpy as np
import matplotlib.pyplot as plt
a = xl("K2")
b = xl("L2")
c = xl("M2")
d = xl("N2")
e = xl("O2")
r1 = xl("F2")
r2 = xl("G2")
r3 = xl("H2")
r4 = xl("I2")
r5 = xl("J2")
# Generate x values
x = np.linspace(r1-1, r3+1, 1000)
# Calculate y values for the polynomial
y = a*x**3 + b*x**2 + c*x + d
def polynomial(x):
return a*x**3 + b*x**2 + c*x + d
# Find x-intercepts using numpy's roots function
coefficients = [a, b, c, d] # coefficients of expanded polynomial
x_intercepts = np.roots(coefficients)
# Create the plot
plt.figure(figsize=(3,3), dpi = 400)
plt.plot(x, y, 'b-')
# Plot x-intercepts and add labels
for x_int in x_intercepts:
plt.plot(x_int, polynomial(x_int), 'ro', zorder = 20) # 'ro' for red circle markers
plt.text(x_int, 20, f'{int(x_int)}', color = "red", fontsize=9, ha = "center")
# Customize the plot
plt.axhline(y=0, color='black', linestyle='-', alpha=1) # x-axis
plt.axvline(x=0, color='black', linestyle='-', alpha=1) # y-axis
plt.xticks([])
plt.yticks([])
plt.xlabel('x')
plt.ylabel('y')
plt.show()
- Excel generiert zufällige Wurzeln in einem begrenzten Intervall.
- Excel berechnet dann die Koeffizienten für das erweiterte (nicht faktorisierte) Polynom.
- Ein Python-Skript in einer angrenzenden Zelle nimmt dann die Koeffizienten und zeichnet einen Graphen des Polynoms mit x-Achsenabschnitte aufgezeichnet und beschriftet, für den Arbeitsblattschlüssel.

Ich habe Mit einem Grafikrechner wird bestätigt, dass die Koeffizienten korrekt sind. Sie produzieren die erwarteten Wurzeln. Ich habe das Python-Skript in Spyder ausgeführt und es hat wie erwartet funktioniert, wobei alle Wurzeln korrekt beschriftet waren. Hier ist das Python-Skript, das in die dritte Tabellenzelle von links eingefügt wird:
import numpy as np
import matplotlib.pyplot as plt
a = xl("K2")
b = xl("L2")
c = xl("M2")
d = xl("N2")
e = xl("O2")
r1 = xl("F2")
r2 = xl("G2")
r3 = xl("H2")
r4 = xl("I2")
r5 = xl("J2")
# Generate x values
x = np.linspace(r1-1, r3+1, 1000)
# Calculate y values for the polynomial
y = a*x**3 + b*x**2 + c*x + d
def polynomial(x):
return a*x**3 + b*x**2 + c*x + d
# Find x-intercepts using numpy's roots function
coefficients = [a, b, c, d] # coefficients of expanded polynomial
x_intercepts = np.roots(coefficients)
# Create the plot
plt.figure(figsize=(3,3), dpi = 400)
plt.plot(x, y, 'b-')
# Plot x-intercepts and add labels
for x_int in x_intercepts:
plt.plot(x_int, polynomial(x_int), 'ro', zorder = 20) # 'ro' for red circle markers
plt.text(x_int, 20, f'{int(x_int)}', color = "red", fontsize=9, ha = "center")
# Customize the plot
plt.axhline(y=0, color='black', linestyle='-', alpha=1) # x-axis
plt.axvline(x=0, color='black', linestyle='-', alpha=1) # y-axis
plt.xticks([])
plt.yticks([])
plt.xlabel('x')
plt.ylabel('y')
plt.show()