# Determine polynomial functions of degree 2

From the graph $y=f(x)$ of a polynomial function $f$ of degree 2 the following three points are known:
$P(2|4)$, $Q(3|5)$, $R(-1|-3)$

Determine the equation of this function 

- in its normal form, $f(x) = a\,x^2 + b\,x + c$, and
- in its vertex form, $f_s(x) = a_s\,(x-x_s)^2 + y_s$


```python
# Initialisations

from sympy import *
init_printing()

import matplotlib.pyplot as plt

# usually you want this
%matplotlib inline 

# useful for OS X
%config InlineBackend.figure_format='retina' 

import numpy as np

from IPython.display import display, Math

from fun_expr import Function_from_Expression as FE
```


```python
# To define f, we first need a variable x
# and three coefficients a,b,c
# with these, f is defined
x = Symbol('x')

a,b,c = symbols('a,b,c')

f = FE(x, a*x**2 + b*x + c)
Math("f(x)="+latex(f(x)))
```




$$f(x)=a x^{2} + b x + c$$




```python
# define points and determine equations
x_1,y_1 = 2,4
x_2,y_2 = 3,5
x_3,y_3 = -1,-3

pts = [(x_1,y_1),(x_2,y_2),(x_3,y_3)]

eqns = [Eq(f(x_1),y_1),
        Eq(f(x_2),y_2),
        Eq(f(x_3),y_3)]

# display result
for eq in eqns:
    display(eq)
```


```python
# solve eqnations
sol = solve(eqns)

# display results
sol
```


```python
# substitute result into f
f = f.subs(sol)

# show result
Math("f(x)="+latex(f(x)))
```




$$f(x)=- \frac{x^{2}}{3} + \frac{8 x}{3}$$



To find the vertex form $f_s(x) = a_s\,(x-x_s)^2 + y_s$, a new function is defined


```python
# define f_s
x = Symbol('x')
a_s, x_s, y_s = symbols('a_s,x_s,y_s')

f_s = FE(x, a_s*(x-x_s)**2 + y_s)
Math("f_s(x)="+latex(f_s(x)))
```




$$f_s(x)=a_{s} \left(x - x_{s}\right)^{2} + y_{s}$$



The expanded coefficients of $f_s$ must be equal to the coefficients of the known function $f$.

First, we create an expression `expr`, to hold the expanded form of $f_s(x)$


```python
expr = f_s(x).expand()
expr
```

Then the method `expr.coeff` is used to get the coefficients of all powers of `x`:


```python
c_s = [expr.coeff(x,i) for i in range(3)]
c_s
```

In the same way, the list of coefficients of `f(x)` is determined.

Here, `f(x)` is an expression, an we can use the method `f(x).coeff` directly.


```python
c = [f(x).coeff(x,i) for i in range(3)]
c
```

This leads to a system of equations:


```python
eqns_s = [Eq(lc,rc) for lc,rc in zip(c_s,c)]

for eq in eqns_s:
    display(eq)
```

This system of equations could also be archieved by


```python
eqns_s = [Eq(f_s(x).expand().coeff(x,i),f(x).coeff(x,i)) for i in range(3)]

for eq in eqns_s:
    display(eq)
```

The solution gives the unknown values $a_s$, $x_s$ and $y_s$:


```python
sol_s = solve(eqns_s)
sol_s
```

Since the system of equations is not linear, the solution is a list of possible solutions. This list contains only one dict of solutions.


```python
# substitute sol_s into f_s
f_s = f_s.subs(*sol_s)
Math("f_s(x)="+latex(f_s(x)))
```




$$f_s(x)=- \frac{1}{3} \left(x - 4\right)^{2} + \frac{16}{3}$$




```python
# f and f_s define essentially the same function.
# To see this, we need both of them in expanded form
f(x) == f_s(x).expand()
```




    True



Display the result:


```python
# init new plot
fig, ax = plt.subplots()

# redefine x_s
x_s = x_s.subs(*sol_s)
d = 6

# the interval along the x-axis
lx = np.linspace(float(x_s-d),float(x_s+d))

# plot f(x), the given points and the vertex
ax.plot(lx,f.lambdified(lx),label=r"$y={f}$".format(f=latex(f(x))))
ax.scatter(*zip(*pts))
ax.scatter(x_s,f(x_s),c='r')

# refine plot
ax.axhline(0)
ax.axvline(0)
ax.grid(True)
ax.legend(loc='best')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('A polynomial of degree 2')

# show result
plt.show()
```

abdb61c9a48d1b8129ea20d1954b4a1824955e65
ipynb
Jupyter Notebook
docs/03-square_functions.ipynb
w-meiners/fun-expr
a44f0366f08c8c2d2eb2702176698bfe3f6febed
MIT
2017-12-20T16:16:40.000Z
2022-02-28T11:06:38.000Z
docs/03-square_functions.ipynb
w-meiners/fun-expr
a44f0366f08c8c2d2eb2702176698bfe3f6febed
MIT
docs/03-square_functions.ipynb
w-meiners/fun-expr
a44f0366f08c8c2d2eb2702176698bfe3f6febed
MIT
2020-01-27T09:50:59.000Z
2020-01-27T09:50:59.000Z
Qwen/Qwen-72B
1. YES
2. YES