Plot a Polynomial

I created a GUI to accept a Polynomial and stored it into a variable called ‘equation’.

I used that ‘equation’ variable and used the ‘solve’ function to solve it and it worked just fine.

However, when I try to use the same variable, ‘equation’, along with the ‘linspace’, in the ‘plot’ function to plot the graph of the function, it gives me an ‘Exception in Tkinter call back’ and rejects the plotting exercise.

If instead of using the ‘equation’ variable, I enter the actual function, like x**2+5*x-6, directly into the ‘plot’ function, it works fine.

I have extensive experience using Assembler language and being able to get complete control of the machine down to the bit level.

Using APIs is new to me and very unfriendly

I looks like you are trying to apply a SymPy expression to a NumPy array as if the expression were callable. A SymPy expression is symbolic. It can contain several symbols, like `x**2 + y**2 + x*y`

. It is meant for symbolic computations, not numeric computations. You cannot apply it to an array directly. However, SymPy supports turning its expressions into numeric functions, as explained here:

https://docs.sympy.org/latest/modules/numeric-computation.html#lambdify

You want something like this:

```
import numpy as np
import sympy
from sympy.abc import x
import matplotlib.pyplot as plt
f = x**2 + x + 1
X = np.linspace(-10, 10)
# Won't work -- f is not a function.
# Y = f(X)
# Works.
Y = sympy.lambdify(x, f, "numpy")(X)
plt.plot(X, Y)
plt.show()
```

Thanks a lot.

I’ll study what you gave me and introduce it to my code.

I entered it exactly the way you gave it to me and it almost worked.

It generated the y values from -10 to 10 but under the entry just before the 10.0, it gives me an error message

SyntaxError: invalid syntax

I placed a lot of print statements in the code to trace it and apparently it is the

y = sympy.lambdify(x, f, “numpy”)(x)

That is causing the error.

In any case, as I said, it is very, very close to give me the plot

The traceback message should point you to the error. Could you paste it completely? I don’t see a syntax error in the line you quoted, so make sure to post the traceback literally (so the quotes for example are not transformed into curly quotes). In Discourse, here is the way to do this:

```
```
Three backticks above.
Your code or error message goes here.
Three backticks at the end.
```
```

your code works beautifully, thanks