/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 15 Create a user-defined function \... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Create a user-defined function \(f(x)\) that returns the value \(1+\frac{1}{2} \tanh 2 x\), then use a central difference to calculate the derivative of the function in the range \(-2 \leq x \leq 2\). Calculate an analytic formula for the derivative and make a graph with your numerical result and the analytic answer on the same plot. It may help to plot the exact answer as lines and the numerical one as dots. (Hint: In Python the tanh function is found in the math package, and it's called simply tanh.)

Short Answer

Expert verified
Define the function and import libraries. Use central difference to compute the numerical derivative, calculate the analytic derivative, and plot both results.

Step by step solution

01

Define the function

Define the user-defined function as specified. In Python, this can be easily done using a def statement.
02

Import necessary libraries

Import the math package to access the tanh function and other necessary packages like numpy for numerical computations and matplotlib for plotting.
03

Implement central difference

Using numpy, create an array of x values within the specified range -2 ≤ x ≤ 2. Then, utilize the central difference formula \(f'(x) ≈ \frac{f(x+h)-f(x-h)}{2h}\) for numerical differentiation.
04

Compute the analytic derivative

Calculate the derivative analytically by hand, knowing that the derivative of tanh(x) is \(1 - \tanh^2(x)\). The derivative of f(x) = 1 + 1/2 tanh(2x) can be found as f'(x) = 1/2 \cdot 2 (1 - \tanh^2(2x)) = (1 - \tanh^2(2x)).
05

Plot the results

Use matplotlib to plot both the numerical and analytical results. Plot the exact (analytical) answer as a line and the numerical one as dots.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Central Difference Method
The Central Difference Method is a numerical technique to approximate the derivative of a function. This method is very useful when you don't have the exact form of the derivative or when dealing with discretized data.
Here's how it works. Suppose you have a function \( f(x) \). The central difference method formula to approximate \( f'(x) \) is:
\[ f'(x) \approx \frac{f(x+h) - f(x-h)}{2h} \]
This formula gives us the slope of the function at a point \( x \) by looking at the points just to its left and right, separated by a small interval \( h \). The central difference method is more accurate than forward or backward difference methods because it takes into account the change in the function on both sides of \( x \).
To implement this in the Python programming language, we can use the numpy library for handling array operations efficiently. Here, \( h \) can be defined as a small value, like 0.01.
Analytic Derivative
The analytic derivative means determining the derivative of a function using calculus rather than numerical methods. For the function given in the exercise, \( f(x) = 1 + \frac{1}{2} \tanh(2x) \), we need to find its derivative analytically.
First, recall that the derivative of \( \tanh(x) \) is given by:
\[ \frac{d}{dx} \tanh(x) = 1 - \tanh^2(x) \]
Given the function \( f(x) \), its derivative can be computed step by step:
1. The derivative of \(1\) is \(0\).
2. The derivative of \( \frac{1}{2} \tanh(2x) \) requires the chain rule:
\[ \frac{d}{dx} \left( \frac{1}{2} \tanh(2x) \right) = \frac{1}{2} \cdot \frac{d}{dx} \tanh(2x) \cdot \frac{d}{dx} (2x) \]
3. Using the chain rule, we get:
\[ \frac{1}{2} \cdot (1 - \tanh^2(2x)) \cdot 2 = 1 - \tanh^2(2x) \]
Thus, the analytic derivative is:
\[ f'(x) = 1 - \tanh^2(2x) \]
This exact expression can be used to compare against the numerical approximation.
Python Programming
Python is a versatile programming language that is very useful for scientific computing. By utilizing libraries such as math, numpy, and matplotlib, you can perform complex numerical methods and visualize results effortlessly.
Here's a quick rundown to solve our problem in Python:
1. **Define the function**:
def f(x):
     return 1 + 1/2 * math.tanh(2 * x)
2. **Import necessary libraries**:
import math
import numpy as np
import matplotlib.pyplot as plt
3. **Central Difference Method**:
Create an array of x values using numpy:
x = np.linspace(-2, 2, 400)
Define h as a small step:
h = 0.01
Compute the numerical derivative:
numerical_derivative = (f(x + h) - f(x - h)) / (2 * h)
4. **Analytic Derivative**:
Using numpy for vectorized operations, define the exact derivative:
analytic_derivative = 1 - np.tanh(2 * x)**2
5. **Plot the Results**:
Use matplotlib to create the plot:
plt.plot(x, analytic_derivative, label='Analytic Derivative')
plt.scatter(x, numerical_derivative, label='Numerical Derivative', color='red', marker='o')
plt.legend()
plt.show()
This code will produce a graph comparing the numerical and analytical results, helping you visualize the accuracy of the Central Difference Method.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

The diffraction limit of a telescope Our ability to resolve detail in astronomical observation is limited by the diffraction of light in our telescopes. Light from stars can be treated effectively as coming from a point source at infinity. When such light, with wavelength \(\lambda\), passes through the circular aperture of a telescope (which we'll assume to have unit radius) and is focused by the telescope in the focal plane, it produces not a single dot, but a circular diffraction pattern consisting of central spot surrounded by a series of concentric rings. The intensity of the light in this diffraction pattern is given by $$ I(r)=\left(\frac{J_{1}(k r)}{k r}\right)^{2} $$ where \(r\) is the distance in the focal plane from the center of the diffraction pattern, \(k=2 \pi / \lambda\), and \(J_{1}(x)\) is a Bessel function. The Bessel functions \(J_{m}(x)\) are given by $$ J_{m}(x)=\frac{1}{\pi} \int_{0}^{\pi} \cos (m \theta-x \sin \theta) \mathrm{d} \theta, $$ where \(m\) is a nonnegative integer and \(x \geq 0\). a) Write a Python function \(J(m, x)\) that calculates the value of \(J_{m}(x)\) using Simpson's rule with \(N=1000\) points. Use your function in a program to make a plot, on a single graph, of the Bessel functions \(J_{0}, J_{1}\) and \(J_{2}\) as a function of \(x\) from \(x=0\) to \(x=20\). b) Make a second program that makes a density plot of the intensity of the circular diffraction pattern of a point light source with \(\lambda=500 \mathrm{~nm}\), in a square region of the focal plane, using the formula given above. Your picture should cover values of \(r\) from zero up to about \(1 \mu \mathrm{m}\).

Quantum uncertainty in the harmonic oscillator In units where all the constants are 1 , the wavefunction of the \(n\)th energy level of the one-dimensional quantum harmonic oscillator-i.e., a spinless point particle in a quadratic potential well-is given by $$ \psi_{n}(x)=\frac{1}{\sqrt{2^{11} n ! \sqrt{\pi}}} \mathrm{e}^{-x^{2} / 2} H_{n}(x), $$ for \(n=0 \ldots \infty\), where \(H_{n}(x)\) is the \(n\)th Hermite polynomial. Hermite polynomials satisfy a relation somewhat similar to that for the Fibonacci numbers, although more complex: $$ H_{n+1}(x)=2 x H_{n}(x)-2 n H_{n-1}(x) . $$ The first two Hermite polynomials are \(H_{0}(x)=1\) and \(H_{1}(x)=2 x\). a) Write a user-defined function \(\mathrm{H}(\mathrm{n}, \mathrm{x})\) that calculates \(H_{n}(x)\) for given \(x\) and any integer \(n \geq 0\). Use your function to make a plot that shows the harmonic oscillator wavefunctions for \(n=0,1,2\), and 3, all on the same graph, in the range \(x=-4\) to \(x=4\). Hint: There is a function factorial in the ath package that calculates the factorial of an integer. b) Make a separate plot of the wavefunction for \(n=30\) from \(x=-10\) to \(x=10\). Hint If your program takes too long to run in this case, then you're doing the calculation wrong-the program should take only a second or so to run. c) The quantum uncertainty in the position of a particle in the \(n\)th level of a harmonic oscillator can be quantified by its root-mean-square position \(\sqrt{\left\langle x^{2}\right\rangle}\), where $$ \left\langle x^{2}\right\rangle=\int_{-\infty}^{\infty} x^{2}\left|\psi_{n}(x)\right|^{2} \mathrm{~d} x . $$ Write a program that evaluates this integral using Gaussian quadrature on 100 points, then calculates the uncertainty (i.e., the root-mean-square position of the particle) for a given value of \(n\). Use your program to calculate the uncertainty for \(n=5\). You should get an answer in the vicinity of \(\sqrt{\left\langle x^{2}\right\rangle}=2.3 .\)

Heat capacity of a solid Debye's theory of solids gives the heat capacity of a solid at temperature \(T\) to be $$ C_{V}=9 V \rho k_{B}\left(\frac{T}{\theta_{D}}\right)^{3} \int_{0}^{\otimes_{D} / T} \frac{x^{4} \mathrm{e}^{x}}{\left(e^{x}-1\right)^{2}} \mathrm{~d} x $$ where \(V\) is the volume of the solid, \(\rho\) is the number density of atoms, \(k_{B}\) is Boltzmann's constant, and \(\theta_{D}\) is the so-called Debye temperature, a property of solids that depends on their density and speed of sound. a) Write a Python function \(\mathrm{cv}(\mathrm{T})\) that calculates \(C_{V}\) for a given value of the temperature, for a sample consisting of 1000 cubic centimeters of solid aluminum, which has a number density of \(\rho=6.022 \times 10^{28} \mathrm{~m}^{-3}\) and a Debye temperature of \(\theta_{D}=428 \mathrm{~K}\). Use Gaussian quadrature to evaluate the integral, with \(N=50\) sample points. b) Use your function to make a graph of the heat capacity as a function of temperature from \(T=5 \mathrm{~K}\) to \(T=500 \mathrm{~K}\).

Consider the integral $$ E(x)=\int_{0}^{x} \mathrm{e}^{-t^{2}} \mathrm{~d} t . $$ a) Write a program to calculate \(E(x)\) for values of \(x\) from 0 to 3 in steps of \(0.1 .\) Choose for yourself what method you will use for performing the integral and a suitable number of slices. b) When you are convinced your program is working, extend it further to make a graph of \(E(x)\) as a function of \(x\). If you want to remind yourself of how to make a graph, you should consult Section 3.1, starting on page 88 . Note that there is no known way to perform this particular integral analytically, so numerical approaches are the only way forward.

Rearranging Eq. (5.19) into a slightly more conventional form, we have: $$ \int_{a}^{b} f(x) \mathrm{d} x=h\left[\frac{1}{2} f(a)+\frac{1}{2} f(b)+\sum_{k=1}^{N-1} f(a+k h)\right]+\frac{1}{12} h^{2}\left[f^{\prime}(a)-f^{\prime}(b)\right]+\mathrm{O}\left(h^{4}\right) . $$ This result gives a value for the integral on the left which has an error of order \(h^{4}-a\) factor of \(h^{2}\) better than the error on the trapezoidal rule and as good as Simpson's rule. We can use this formula as a new rule for evaluating integrals, distinct from any of the others we have seen in this chapter. We might call it the "Euler-Maclaurin rule." a) Write a program to calculate the value of the integral \(\int_{0}^{2}\left(x^{4}-2 x+1\right) \mathrm{d} x\) using this formula. (This is the same integral that we studied in Example 5.1, whose true value is \(4.4\).) The order- \(h\) term in the formula is just the ordinary trapezoidal rule; the \(h^{2}\) term involves the derivatives \(f^{\prime}(a)\) and \(f^{\prime}(b)\), which you should evaluate using central differences, centered on \(a\) and \(b\) respectively. Note that the size of the interval you use for calculating the central differences does not have to equal the value of \(h\) used in the trapezoidal rule part of the calculation. An interval of about \(10^{-5}\) gives good values for the central differences. Use your program to evaluate the integral with \(N=10\) slices and compare the accuracy of the result with that obtained from the trapezoidal rule alone with the same number of slices. b) Good though it is, this integration method is not much used in practice. Suggest a reason why not.

See all solutions

Recommended explanations on Physics Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.