Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Plot Taylor polynomial approximations to \(\sin x\). The sine function can be approximated by a polynomial according to the following formula: $$ \sin x \approx S(x ; n)=\sum_{j=0}^{n}(-1)^{j} \frac{x^{2 j+1}}{(2 j+1) !} $$ The expression \((2 j+1) !\) is the factorial (see Exercise 3.14). The error in the approximation \(S(x ; n)\) decreases as \(n\) increases and in the limit we have that \(\lim _{n \rightarrow \infty} S(x ; n)=\sin x\). The purpose of this exercise is to visualize the quality of various approximations \(S(x ; n)\) as \(n\) increases. The first part of the exercise is to write a Python function \(\mathrm{S}(\mathrm{x}\), (n) that computes \(S(x ; n)\). Use a straightforward approach where you compute each term as it stands in the formula, i.e., \((-1)^{j} x^{2 j+1}\) divided by the factorial \((2 j+1) !\). (We remark that Exercise A.16 outlines a much more efficient computation of the terms in the series.) The next part of the exercise is to plot \(\sin x\) on \([0,4 \pi]\) together with the approximations \(S(x ; 1), S(x ; 2), S(x ; 3), S(x ; 6)\), and \(S(x ; 12)\) Name of program file: plot_Taylor_sin.py.

Short Answer

Expert verified
Plot the sine function and its Taylor approximations using different orders, comparing them visually.

Step by step solution

01

Define the Function to Compute Taylor Series

Create a Python function that computes the Taylor series approximation for the sine function. Name the function `S(x, n)`, where `x` is the input angle in radians and `n` is the order of the Taylor polynomial approximation. Use a for loop to iterate through each term of the series up to `n`, and sum these terms to get the approximation. Each term is calculated as \((-1)^j\frac{x^{2j+1}}{(2j+1)!}\). Use the `math` library to calculate the power, factorial, and ensure proper evaluation of each term.
02

Set Up the Range and Plot

Use the `numpy` library to create an array of `x` values ranging from 0 to \(4\pi\). This is achieved by `numpy.linspace(0, 4*np.pi, num_points)` where `num_points` is the discretization level of your choice. Then, compute the actual sine values using `numpy.sin` for these `x` values.
03

Compute and Plot Approximations

For each specified order of approximation \(n = 1, 2, 3, 6, 12\), use the previously defined function `S(x, n)` to compute the Taylor series approximations for the sine function over the defined range. Store each result in a separate array.
04

Plotting with Matplotlib

Utilize the `matplotlib.pyplot` library to create the plots. Plot the true sine values as a reference. Then, plot each Taylor series approximation on the same graph. Use different line styles or colors for each approximation for distinction, and include a legend to identify each series order.
05

Observe and Compare

Set up the plot with appropriate labels, titles, and a grid. Make sure the x-axis is labeled as angle in radians, and include a legend to indicate which line corresponds to which polynomial approximation. Observations can be made on how the approximation accuracy improves as `n` increases by visual comparison with the true sine curve.

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 Vaia!

Key Concepts

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

Sine Function
The sine function, denoted as \( \sin x \), is a fundamental trigonometric function that describes the y-coordinate of a point on the unit circle as it traverses around the origin. It is periodic, with a period of \( 2\pi \), meaning it repeats its values every \( 2\pi \) radians. The sine wave is smooth and continuous, which can be mathematically defined for any real number. This function is essential in various fields such as physics, engineering, and signal processing.
The sine function is often approximated in computations because of its complex periodic nature. This is where Taylor series come into play, as they allow us to approximate \( \sin x \) using polynomials, which are simpler to compute. The quality of this approximation improves as more terms are added. Understanding this concept is crucial for both theoretical studies and practical applications.
Polynomial Approximation
Polynomial approximation involves approximating a function using a polynomial, which is a mathematical expression consisting of variables and coefficients. In the context of the sine function, a Taylor series is used to achieve this approximation. Given by the formula:

\[ \sin x \approx S(x ; n) = \sum_{j=0}^{n}(-1)^{j} \frac{x^{2j+1}}{(2j+1)!} \]

This series effectively creates a polynomial approximation of \( \sin x \). Each term in the series, \( (-1)^j \frac{x^{2j+1}}{(2j+1)!} \), represents a component of a polynomial that approximates the sine function more accurately as more terms (i.e., higher \( n \)) are included.
  • The term \( (-1)^j \) ensures that the polynomial alternates in sign, mimicking the sine function's peaks and troughs.
  • The factorial \( (2j+1)! \) in the denominator governs the rate of convergence, affecting the polynomial's steepness and curvature.
Increasing the order \( n \) of the polynomial increases its accuracy, allowing it to closely match \( \sin x \) over broader intervals.
Python Programming
Python, a versatile programming language, provides robust tools for computing Taylor series approximations. You can begin by defining a function, `S(x, n)`, that calculates these approximations by iterating through each term in the Taylor series.
  • Use a loop to compute each term \( (-1)^j \frac{x^{2j+1}}{(2j+1)!} \) up to \( n \). Ensure you utilize Python's `math` library for functions like `factorial` and `pow` for precise calculations.
  • The order of polynomial \( n \) dictates the number of terms, influencing the balance between computational cost and accuracy.
The complete Python script can incorporate the `numpy` library, which simplifies handling arrays and mathematical functions, such as generating an array of \( x \) values and computing the actual sine values with `numpy.sin`. Using Python in this way not only facilitates computations but also provides a foundation for further mathematical explorations and simulations.
Mathematical Visualization
Visualizing mathematical functions helps to understand their nature and behavior, and the approximation of sine using Taylor series is no exception. With Python, the `matplotlib.pyplot` library is often used for plotting, allowing you to visually compare the true \( \sin x \) curve with its polynomial approximations.
Create plots that display:
  • The actual \( \sin x \) across a period, e.g., from 0 to \( 4\pi \).
  • The Taylor series approximations \( S(x ; n) \) for different values of \( n \), such as 1, 2, 3, 6, and 12.
Each line in the plot can be differentiated using distinct colors or styles. A legend aids in identifying each approximation order. Observing these graphs reveals that as \( n \) increases, the polynomial curves more closely align with the sine wave, demonstrating visually how polynomial approximation works. Also, important features such as gridlines and axis labels enhance the plot's readability and interpretation, making it a valuable tool in educational settings for demonstrating the effectiveness of Taylor series.

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

Experience overflow in a function. When an object (ball, car, airplane) moves through the air, there is a very, very thin layer of air close to the object's surface where the air velocity varies dramatically \(^{18}\), from the same value as the velocity of the object at the object's surface to zero a few centimeters away. The change in velocity is quite abrupt and can be modeled by the functiion $$ v(x)=\frac{1-e^{x / \mu}}{1-e^{1 / \mu}} $$ where \(x=1\) is the object's surface, and \(x=0\) is some distance away where one cannot notice any wind velocity \(v\) because of the passing object \((v=0)\). The vind velocity coincides with the velocity of the object at \(x=1\), here set to \(v=1\). The parameter \(\mu\) is very small and related to the viscosity of air. With a small value of \(\mu\), it becomes difficult to calculate \(v(x)\) on a computer. Make a function \(v(x, m u=1 E-6\), exp=math.exp) for calculating the formula for \(v(x)\) using exp as a possibly user-given exponentional function. Let the v function return the nominator and denominator in the formula as well as the fraction (result). Call the v function for various \(\mathrm{x}\) values between 0 and 1 in a for loop, let mu be \(1 \mathrm{E}-3\), and have an inner for loop over two different exp functions: math. exp and numpy. exp. The output will demonstrate how the denominator is subject to overflow and how difficult it is to calculate this function on a computer. Also plot \(v(x)\) for \(\mu=1,0.01,0.001\) on \([0,1]\) using 10,000 points to see what the function looks like. Name of program file: boundary_layer_func1.py.

Apply a function to a vector. Given a vector \(v=(2,3,-1)\) and a function \(f(x)=x^{3}+x e^{x}+1\), apply \(f\) to each element in \(v .\) Then calculate \(f(v)\) as \(v^{3}+v * e^{v}+1\) using vector computing rules. Show that the two results are equal.

Plot a smoothed "hat" function. The "hat" function \(N(x)\) defined by (3.5) on page 109 has a discontinuity in the derivative at \(x=1\). Suppose we want to "round" this function such that it looks smooth around \(x=1\). To this end, replace the straight lines in the vicinity of \(x=1\) by a (small) cubic curve $$ y=a(x-1)^{3}+b(x-1)^{2}+c(x-1)+d $$ for \(x \in[1-\epsilon, 1+\epsilon]\), where \(a, b, c\), and \(d\) are parameters that must be adjusted in order for the cubic curve to match the value and the derivative of the function \(N(x)\). The new rounded functions has the specification $$ \tilde{N}(x)= \begin{cases}0, & x<0 \\ x, & 0 \leq x<1-\epsilon \\\ a_{1}(x-1)^{3}+b(x-1)+c(x-1)+d_{1}, & 1-\epsilon \leq x<1 \\\ a_{2}(x-1)^{3}+b(x-1)+c(x-1)+d_{2}, & 1 \leq x<1+\epsilon \\ 2-x, & 1+\epsilon \leq x<2 \\ 0, & x \geq 2\end{cases} $$ with \(a_{1}=\frac{1}{3} \epsilon^{-2}, a_{2}=-a_{1}, d_{1}=1-\epsilon+a_{1} \epsilon^{3}, d_{2}=1-\epsilon-a_{2} \epsilon^{3}\), and \(b=c=0 .\) Plot this function. (Hint: Be careful with the choice of \(x\) coordinates!) Name of program file: plot_hat.py.

Implement Lagrange's interpolation formula. Imagine we have \(n+1\) measurements of some quantity \(y\) that depends on \(x\) : \(\left(x_{0}, y_{0}\right),\left(x_{1}, y_{1}\right), \ldots,\left(x_{n}, y_{n}\right)\). We may think of \(y\) as a function of \(x\) and ask what \(y\) is at some arbitrary point \(x\) not coinciding with any of the \(x_{0}, \ldots, x_{n}\). This problem is known as interpolation. One way to solve this problem is to fit a continuous function that goes through all the \(n+1\) points and then evaluate this function for any desired \(x\). A candidate for such a function is the polynomial of degree \(n\) that goes through all the points. This polynomial can be written $$ p_{L}(x)=\sum_{k=0}^{n} y_{k} L_{k}(x) $$ where $$ L_{k}(x)=\prod_{i=0, i \neq k}^{n} \frac{x-x_{i}}{x_{k}-x_{j}} $$ The \(\Pi\) notation corresponds to \(\sum\), but the terms are multiplied, e.g., $$ \prod_{i=0, i \neq k}^{n} x_{i}=x_{0} x_{1} \cdots x_{k-1} x_{k+1} \cdots x_{n} $$ The polynomial \(p_{L}(x)\) is known as Lagrange's interpolation formula, and the points \(\left(x_{0}, y_{0}\right), \ldots,\left(x_{n}, y_{n}\right)\) are called interpolation points. Make a function Lagrange \(\left(x\right.\), points) that evaluates \(p_{L}\) at the point x, given \(n+1\) interpolation points as a two-dimensional array points, such that points \([i, 0]\) is the \(x\) coordinate of point number \(i\) and points \([i, 1]\) is the corresponding \(y\) coordinate. To verify the program, we observe that \(L_{k}\left(x_{k}\right)=1\) and that \(L_{k}\left(x_{i}\right)=0\) for \(i \neq k\), implying that \(p_{L}\left(x_{k}\right)=y_{k}\). Write a function verify (points) that computes \(\left|p_{L}\left(x_{k}\right)-y_{k}\right|\) at all the interpolation points and checks that the value is approximately zero. Call verify with 5 equally spaced points along the curve \(y=\sin (x)\) for \(x \in[0, \pi]\). Then evaluate \(p_{L}(x)\) for an \(x\) in the middle of two interpolation points and compare the value of \(p_{L}(x)\) with the exact one: \(\sin (x)\). Name of program file: Lagrange_polynomial1.py.

Plot a formula. Make a plot of the function \(y(t)=v_{0} t-0.5 g t^{2}\) for \(v_{0}=10, g=9.81\) and \(t \in\left[0,2 v_{0} / g\right] .\) The label on the \(x\) axis should be 'time (s)' and the label on the \(y\) axis should be 'height \((\mathrm{m}) '\). Name of program file: plot_ball1.py.

See all solutions

Recommended explanations on Computer Science 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.

Sign-up for free