An archive of Mark's Spring 2018 Numerical Analysis course.

Derivative of your personal function

mark

(10 pts)

Referring back to your personal function,

  1. Plot the derivative of your personal function, and
  2. use the symmetric difference quotient to compute the derivative at 0.123.
CeasarsRevenge

My randomly generated function is:
f(x) =x^9 + e^{x^9}
and its first derivative is 9x^8(e^{x^9}+1)

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import brentq
from scipy.misc import derivative
from numpy import exp
def f(x): return x**9 + exp(x**9)
def ff(x): return derivative (f, x, dx=0.001, n=1)
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(-1.2,1,100)
y = ff(x)
plt.plot(x,y)

The plot of this derivative is shown below.
derivative of personal function

Then using the symmetric difference quotient I computed the derivative for x=0.123 and got the answer 9.4358543201167322e-07

CestBeau

My personal function was f(x) = \sin(x^3)^3. To find the derivative numerically I simply used the symmetric difference quotient on the interval [0,1]. The graph below indicates f(x) in orange and f'(x) in blue. Sure enough, if we set h low enough then f'(.123)= 4.715×10^-7, the true value of the derivative at x=0.123.

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
def f(x):
    return (np.sin(x**3)**3)
xs = np.linspace(0,1,1000)
ys = f(xs)
plt.plot(xs,ys)
h = 0.001
def fprime(x,h):
    out = f(x+h)-f(x-h)
    out = out/(2*h)
    return out
yprime = [fprime(x,h) for x in xs]
plt.plot(xs,yprime)
fprime(.123,h)

derivative

brian

My randomly generated function was

f(x)=\sin^{9}(x)

As before, here is how it was defined

from numpy import sin
def f(x): return sin(x)**9

and here is its graph:

%matplotlib inline

import numpy as np
from matplotlib import pyplot as plt
x = np.arange(-2, 2, 0.01) 
y = f(x)
plt.plot(x,y)

output_4_0

Let’s find the derivative of this function numerically. To do so, first lets plot f' by itself.

def ff(x): return (f(x+.00001)-f(x-.00001))/2*(.0001)
x = np.arange(-2, 2, 0.01) 
y = ff(x)
plt.plot(x,y)
plt.savefig('plot.png')
plt.show()

output_6_0

Interesting. Next, lets plot f with f'

%matplotlib inline

import numpy as np
from matplotlib import pyplot as plt
x = np.arange(-2, 2, 0.01) 
y = f(x)
plt.plot(x,y)

def ff(x): return (f(x+.00001)-f(x-.00001))/2*(.0001)
x = np.arange(-2, 2, 0.000001) 
y = ff(x)
plt.plot(x,y)

output_8_1

Finally, lets calculate f'(.123) via the symmetric difference quotient

ff(.123)
4.5859031535014061e-16

Given the graph of f this value seems reasonable. Just to be sure, lets confirm this result with the derivative command from the scipy library

from scipy.misc import derivative

derivative(f, .123, dx=1e-6)
4.5859028781619318e-07

The values are approximately equal. Success!

anonymous_user

My personal function is

f(x) = 2x^3+\cos(x).

The plot of the derivative of this function, f'(x)=6x^2-\sin(x), is shown below along the code required to generate it and prepare for the rest of this task.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
h=0.0001
def f(x):
     return 2*x**3+np.cos(x)
def fprime(x):
     return 6*x**2-np.sin(x)
def df(x):
     return (f(x+h)-f(x-h))/(2*h)
xs = np.linspace(-4,4,2000)
plt.plot(xs,df(xs))

df

Note that in the code above, we defined a function df(x), which is defined as the symmetric difference quotient of f(x), with h fixed as h=0.0001. We wish to find the numerically computed value of df(x) at x=0.123, which we find to be df(0.123)=-0.0319160698198. Compared to the actual value of f'(0.123)=-0.0319160900243, we have agreement to the first 7 decimal places, or within approximately h^2.

nathan

My personal function is

f(x)=cos(x^{9}).

Using the chain rule, the derivation can be computed fairly easily. Simply,

f'(x) = -9x^{8}sin(x^{9}).

Let’s see what this function looks like plotted out.

import numpy as np
from matplotlib import pyplot as plt
from numpy import cos, sin
def ff(x):
    return -(9*x**8)*sin(x**9)
x = np.arange(-1.4, 1.4, 0.001) #xmin, xmax, stepsize
y = ff(x)
plt.plot(x,y)
plt.savefig('niceplot.png')
plt.show()

niceplot

Now for the heck of it, I want to compare this to the symmetric difference quotient using a step size of 0.001.

def f(x): #original function
    return cos(x**9)
def altFF(x): #using difference quotient
    return (f(x + 0.001) - f(x - 0.001))/(2*(0.001))
x = np.arange(-1.4, 1.4, 0.001) 
y = altFF(x)
plt.plot(x,y)
plt.show()

niceplot

Looks pretty much the same. But lets take the difference of altFF() (the difference quotient) and ff() (actual derivative) to examine how accurate using the difference quotient is with a step size of 0.001.

#comparing altFF to ff
x = np.arange(-1.4, 1.4, 0.001) 
y = altFF(x) - ff(x)
plt.plot(x,y)
plt.savefig('niceplot2.png')
plt.show()

niceplot2

We can see that biggest error in the [-1.4, 1.4] interval is about 0.4. This is respectable considering that the derivative near the endpoints or over 100. If we decrease the stepsize, then our error will be reduced considerably.

The derivative at 0.123 is:

altFF(0.123)
Out[]: 0.0
ff(0.123)
Out[]: -3.0382912570198824e-15

Interesting. The derivative is small enough that machine precision begins to be an issue.

Lorentz

My personal function is f(x)=x^3+x \sin (x) + e^x.

%matplotlib inline
import pylab
import numpy as np
def f(x):
    return x**3+x*np.sin(x)+np.exp(x)
xs = np.linspace(-5,5,1000)
ys = f(xs)

h = 10**-3
def f_p(x,h):
    out = f(x+h)-f(x-h)
    out = out/(2*h)
    return out

y_p = [f_p(x,h) for x in xs]

pylab.plot(xs,ys,'-b',label='Function')
pylab.plot(xs,y_p,'-k',label='Derivative')
pylab.legend(loc='upper left')
pylab.show()
pylab.savefig('funcAndDeriv.png')

f_p(0.123,h)

Here is the output and the value I got for f'(0.123)=1.4210333567150624.
willThisWork

Here is a fun alternative to using numerical differentiation for this particular function.

from sympy import diff, Symbol, lambdify, cos, sin, exp
import matplotlib.pyplot as plt
t=Symbol('t')
h=t**3+t*sin(t)+exp(t)

hdiff = diff(h,t)

lam_x = lambdify(t, hdiff, modules=['numpy'])
lam_x1 = lambdify(t, h, modules=['numpy'])

x_vals = np.linspace(-5, 5, 100)
y_vals = lam_x(x_vals)

x1_vals = np.linspace(-5, 5, 100)
y1_vals = lam_x1(x1_vals)

plt.plot(x_vals, y_vals)
plt.plot(x1_vals,y1_vals)
plt.show()

lam_x(0.123)

Using this method f'(0.123)=1.4210322499244334, which agrees with the previous method to the order of 10^{-7}.

jorho85

My personal function was f(x) = x^9(x^3 + e^{x^3}) which I defined in python as:

from numpy import exp
def f(x): return x**9*(x**3 + exp(x**3))

then I used the symmetric difference quotient to find the first derivative

h = 0.001
def ff(x,h): return (f(x+h)-f(x-h))/(2*h)
xs = np.linspace(-1,1,500)
ys = [ff(x,h) for x in xs]
plt.plot(xs,ys)

which produced the following image

pic1

then I used ff(x,h) where x=0.123 and h=0.001 and I got 4.7413669569090636e-07

theoldernoah

My personal function is f(x) = x^3 + e^x+sin(x^3).

We will define this as before:

from numpy import sin, exp, cos
def f(x): return x**3 + exp(x)+sin(x**3)

And graph it as before…

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
x = np.arange(-5,5,0.1)
y = f(x)
plt.plot(x,y)
plt.savefig('my_pic2.png')

my_pic2

We’ll now define and graph the derivative of my function, f'(x)= 3x^2 + e^x + 3x^2cos(x^3), in Python…

def fp(x): return 3*x**2 + exp(x) + 3*x**2*cos(x**3)
xp = np.arange(-5,5,0.1)
yp = fp(x)
plt.plot(xp,yp)
plt.savefig('my_pic3.png')

my_pic3

We will now define the symmetric difference quotient of f(x) with a fixed h=0.00001 and test the value of df(x) at x=0.123.

h = 0.00001
def df(x): return (f(x+h)-f(x-h))/(2*h)
df(0.123)

#Out: 
1.2216583425761485

If we compare this with our original fp(x)

fp(0.123)

#Out:
1.2216583423638705

We see that they agree up to the first 9 decimal places, not bad!

dumptruckman

My random function is:

x^{3} + \sin{\left (x^{3} \right )} + \cos{\left (x^{3} \right )}

This is defined in Python as:

from numpy import sin, cos
def f(x):
    return x**3 + sin(x**3) + cos(x**3)

A plot of the function over the interval [-2,2] using matplotlib looks like:

import numpy as np
%matplotlib inline
from matplotlib import pyplot as plt

x = np.arange(-2, 2, 0.001)
y = f(x)
plt.plot(x,y)
plt.show()

image

The derivative of my function is:

3x^2\left(1+\cos\left(x^3\right)-\sin\left(x^3\right)\right)

Plotted in Python this would look like:

def ff(x):
    return 3*x**2 * (1 + cos(x**3) - sin(x**3))

x = np.arange(-2, 2, 0.001)
y = ff(x)
plt.plot(x,y)
plt.show()

image

Computing the value of the derivative at 0.123 using the actual derivative can be done as so:

ff(0.123)

Which produce the value 0.090689462294596582.

Let’s compare this to the value that would be approximated by the symmetric difference quotient. We’ll use an h value of 0.0001.

x = 0.123
h = 0.0001

(f(x + h) - f(x - h))/(2*h)

This produces the value of 0.090689482107331187. This approximation is decent, correct to 6 significant figures.

Sampson

My personal random function is \left(e^{x} + \sin{\left (x \right )}\right) \sin{\left (x \right )} . Let’s plot it to get a sense of what it is doing with the following code:

from numpy import sin, exp
from matplotlib import pyplot as plt
def f(x): return (exp(x)+sin(x))*sin(x)
import numpy as np
x = np.arange(-2,2,.01)
y = f(x)
plt.ylim(-1,3)
plt.plot(x,y)
plt.grid(True)
plt.show()

Personal_Function

Now we need to find df numerically, and go ahead and plot it with f(x) using the following code:

h = 0.0001
def df(x): return (f(x+h)-f(x-h))/(2*h)
x = np.arange(-2,2,.01)
y = df(x)
plt.plot(x,y, color = 'b', linewidth = 1.5)

y = f(x)
plt.plot(x,y, color = 'r', linewidth = 1.5)

plt.grid()
plt.show()

Personal_Derivative
This looks pretty good! Now we can calculate df(.123) in addition to the scipy derivative.

from scipy.misc import derivative 
print(derivative(f , .123))
print(df(.123))

# Out 1.65613080255, 1.50461527923

These two results are pretty far apart unfortunately.

opernie

My personal function is f(x)=x^3+x+e^x+cos(x)
It’s just begging for it… the derivative: f'(x)=3x^2+1+e^x-sin(x).
Using the symmetric difference quotient on the interval [-2,2], the derivative is estimated numerically.

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
from numpy import sin, cos, exp
def f(x):
    return (x**3+x+exp(x)+cos(x))
xx = np.linspace(-2,2,1000)
y = f(x)
plt.plot(x,y)
h = 0.001
def ff(x,h):
    out = f(x+h)-f(x-h)
    out = out/(2*h)
    return out
yp = [ff(x,h) for x in xx]
plt.plot(xx,yprime)
ff(.123,h)

image

Note that f(x) is in orange and f'(x) is in blue and that f'(.123)=2.0535825398524388.

funmanbobyjo

My personalized function:

f(x)=\left(x^{3} + e^{x^{3}}\right) e^{x}

This is how I defined the code in python and generated a sample computation:

In [2]: from numpy import sin, cos, exp 
def f(x): return (x**3+exp(x**3))*(exp(x))
f(1)
Out[2]: 10.107337927389695

Taking the derivative gives the equation:

f(x)=\left(x^{3}+3x^2 + (3x^2+1)e^{x^{3}}\right) e^{x}

defined with code as

def f(x):
    return exp(x)*((3*(x)**2+1)*exp((x)**3)+x**3+3*(x)**2)

using the following code:

 %matplotlib inline
from matplotlib import pyplot as plt
 import numpy as np
 from numpy import exp
 def f(x):
     return exp(x)*((3*(x)**2+1)*exp((x)**3)+x**3+3*(x)**2)

 xs = np.linspace(0,1,1000)
 ys = f(xs)
 plt.plot(xs,ys)
 h = 0.001
   def fprime(x,h):
      out = f(x+h)-f(x-h)
     out = out/(2*h)
     return out
 yprime = [fprime(x,h) for x in xs]
 plt.plot(xs,yprime)
 fprime(.123,h)

I graphed the function and derivative and got the output for f(.123) which was equal to 3.0136816746014761.

dakota

My personalized function is e^{\cos(x)}\cos^3(x).

It’s first derivative is \sin(x)(e^{\cos(x)}\ ) \cos^2(x)(\cos(x) + 3).

The plot of this derivative is shown below.
deriv

Then, I can use the symmetric difference quotient \frac{f(x+h)-f(x-h)}{2h} to approximate the derivative of my function at 0.123 via the following code:

def f(x): return sin(x)*exp(cos(x))*((cos(x))**2)*(cos(x) + 3)
h = 10**-3
def approx(x,h): return f(x+h) - f(x-h)/2*h
approx(0.123,h)

The code returned the following answer of 1.3109460754655351.

mark

Uhhhhhh little code?

MatheMagician

My function was:

f(x)=x^3+x+2sin(x)

In python this is defined by:

import numpy as np
def f(x):
f(x)=x**3+x+2*sin(x)

We can then use the following code to produce the graph

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
def f(x):
    return (x**3+x+2*np.sin(x))
h = 0.001
def ff(x,h): return (f(x+h)-f(x-h))/(2*h)
xs = np.linspace(-1,1,500)
ys = [ff(x,h) for x in xs]
plt.plot(xs,ys)

image

To find the derivate

ff(.123,h)= 3.0302777334554052
Cornelius

My personal function is cos^2(x^3)).

Defining and plotting it over the interval [-2,2] in Python:

from numpy import cos, exp
from matplotlib import pyplot as plt
def f(x): return (cos(x**3))**2
import numpy as np
x = np.arange(-2,2,.01)
y = f(x)
plt.ylim(-1,3)
plt.plot(x,y)
plt.grid(True)
plt.show()

download

Taking the derivative of my function by hand we obtain the function f'(x) = -6x^2sin(x^3)cos(x^3).

Defining and plotting it in Python:

def ff(x):  
    return (-6)*(x**2)*sin(x**3)*cos(x**3)

    x = np.arange(-2,2,0.001)
    y = ff(x)
    plt.plot(x,y)
    plt.show(x)

download (1)

First let us find what the value would be if we evaluate 0.123 using Python directly:

ff(0.123)

This gives us -0.00016891795110172216.

Now to use the symmetric difference quotient to approximate with an h value of 0.0001:

x = 0.123

h = 0.0001

(f(x + h) - f(x - h))/(2*h)

Which returns -0.00016891832321785927.