Determine a polynomial $f(x)$ of degree 3 from the following information:

- $f(x)$ has a stationary point at $P(0|4)$
- $f(x)$ has an inflection point at $Q(2|2)$

Plot the graph of the resulting function for $-2.1 \le x \le 6.1$.

## Solution

The first step consists of some initialisations


```python
# Initialisations
from sympy import *
init_printing()

import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina' # for OSX only

import numpy as np

from IPython.display import display, Math

from fun_expr import Function_from_Expression as FE
```

Then, the function $f(x)$ is defined as

$$
  f(x) = a\,x^3 + b\,x^2 + c\,x + d
$$

with unknown coefficients $a\cdots d$.


```python
# define f

x = Symbol('x')

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

f = FE(x, a*x**3 + b*x**2 + c*x + d)
f_1 = f.diff(x)
f_2 = f.diff(x,2)

display(Math("f(x)="+latex(f(x))))
display(Math("f'(x)="+latex(f_1(x))))
display(Math("f''(x)="+latex(f_2(x))))
```


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



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



$$f''(x)=6 a x + 2 b$$


The unknown coefficients are determined by the conditions

\begin{align*}
  f(x_s) &= y_s \\
  f(x_i) &= y_i \\
  f'(x_s) &= 0 \\
  f''(x_i) &= 0 \\
\end{align*}

Here, $(x_s|y_s)$ is the stationary point and $(x_i|y_i)$ the inflection point.


```python
# known information
x_s, y_s = 0,4
x_i, y_i = 2,2
p_s = (x_s,y_s) # stationary point
p_i = (x_i,y_i) # inflection point

# equations
eqns = [Eq(f(x_s),y_s), 
        Eq(f_1(x_s),0),
        Eq(f(x_i),y_i),
        Eq(f_2(x_i),0)]

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

The resulting system of equations is solved


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

... and the solution substituted into $f$, $f'$ and $f''$.


```python
# substitute solution
f = f.subs(sol)
f_1 = f_1.subs(sol)
f_2 = f_2.subs(sol)
display(Math('f(x)='+latex(f(x))))
display(Math("f'(x)="+latex(f_1(x))))
display(Math("f''(x)="+latex(f_2(x))))
```


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



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



$$f''(x)=\frac{3 x}{4} - \frac{3}{2}$$


The resulting function $f(x)$ is plotted over $-2.1 \le x \le 6.1$ 


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

# x-values
lx = np.linspace(-2.1,6.1)

# plot
ax.plot(lx,f.lambdified(lx),label=r'$y={f}$'.format(f=latex(f(x))))
ax.scatter(*zip(*[p_s,p_i]))

# refine plot
ax.axhline(0,c='k')
ax.axvline(0,c='k')
ax.grid(True)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend(loc='best')

# show plot
plt.show()
```

4c42f447818b401f043a151ae1bfb2ecf10dfda0
ipynb
Jupyter Notebook
docs/04-determine_polynomial_of_degree_3.ipynb
w-meiners/fun-expr
a44f0366f08c8c2d2eb2702176698bfe3f6febed
MIT
2017-12-20T16:16:40.000Z
2022-02-28T11:06:38.000Z
docs/04-determine_polynomial_of_degree_3.ipynb
w-meiners/fun-expr
a44f0366f08c8c2d2eb2702176698bfe3f6febed
MIT
docs/04-determine_polynomial_of_degree_3.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