## Functions¶

1. Build a linear function $f(x) = -\frac{1}{2}x + 4$ on $x \in [-3, 3]$; create a graph and table.
In [1]:
import matplotlib.pyplot as plt
import numpy as np

In [2]:
def f(x): return -0.5*x + 4 #function
x = np.linspace(-3, 3, 100) #domain

In [3]:
plt.plot(x, f(x)) #plot of function on domain

Out[3]:
[<matplotlib.lines.Line2D at 0x7f30cde24fd0>]
In [ ]:
import pandas as pd

In [ ]:
f1 = pd.DataFrame({'x': x, 'f(x)': f(x)})

In [ ]:
f1

Out[ ]:
x f(x)
0 -3.000000 5.500000
1 -2.939394 5.469697
2 -2.878788 5.439394
3 -2.818182 5.409091
4 -2.757576 5.378788
... ... ...
95 2.757576 2.621212
96 2.818182 2.590909
97 2.878788 2.560606
98 2.939394 2.530303
99 3.000000 2.500000

100 rows Ã— 2 columns

1. Build a linear function $f(x) = \frac{5}{2}x - 7$ on $x \in [-3, 3]$; create a graph and table.
In [ ]:
def f(x): return (5/2)*x - 7
x = np.linspace(-3, 3, 1000)

In [ ]:
plt.plot(x, f(x))

Out[ ]:
[<matplotlib.lines.Line2D at 0x7f7199b492b0>]
In [ ]:
f2 = pd.DataFrame({'x': x, 'f(x)': f(x)})

Out[ ]:
x f(x)
0 -3.000000 -14.500000
1 -2.993994 -14.484985
2 -2.987988 -14.469970
3 -2.981982 -14.454955
4 -2.975976 -14.439940
1. Build a quadratic function $f(x) = 3(x - 1)^2 - 4$ on $x \in [-5, 5]$; create a graph and table.
In [ ]:
def f(x): return 3*(x - 1)**2 - 4
x = np.linspace(-5, 5, 100)
plt.plot(x, f(x))

Out[ ]:
[<matplotlib.lines.Line2D at 0x7f7199aacb70>]
In [ ]:
f3 = pd.DataFrame({'x': x, 'f(x)': f(x)})

Out[ ]:
x f(x)
0 -5.00000 104.000000
1 -4.89899 100.394245
2 -4.79798 96.849709
3 -4.69697 93.366391
4 -4.59596 89.944291
1. Build a cosine function $f(x) = 2\cos{x^2}$ on $x \in [-3\pi, 3\pi]$; create a graph and table.
In [ ]:
def f(x): return 2*np.cos(x**2)
x = np.linspace(-3*np.pi, 3*np.pi, 1000)
plt.plot(x, f(x))

Out[ ]:
[<matplotlib.lines.Line2D at 0x7f7199a908d0>]
In [ ]:
f4 = pd.DataFrame({'x': x, 'f(x)': f(x)})

Out[ ]:
x f(x)
0 -9.424778 1.302076
1 -9.405910 1.748857
2 -9.387041 1.976957
3 -9.368173 1.959361
4 -9.349304 1.699691
1. Build an exponential function $f(x) = 3*(100)^x$ on $x \in [-3, 3]$; create a graph and table.
In [ ]:
def f(x): return 3*(100**x)
x = np.linspace(-3, 3, 100)
plt.plot(x, f(x))

Out[ ]:
[<matplotlib.lines.Line2D at 0x7f71999fdf98>]
In [ ]:
f5 = pd.DataFrame({'x': x, 'f(x)': f(x)})