To calculate a planetβs space coordinates, we have to solve the function π(π₯) = π₯ β 1 β 0.5 sin π₯
Let the base point be π = π₯i = π/2 on the interval [0, π]. Determine the highest-order Taylor series expansion resulting in a maximum error of 0.015 on the specified interval. The error is equal to the absolute value of the difference between the given function and the specific Taylor series expansion.
1) Establish parameters
2) Find true value
3) Create a loop for the Taylor Series approximation
4) Find true error after each iteration
5) If stopping criteria is met, break loop
6) Calculate error between each iteration and true value
*It is important to note that this method was attempted, but then the code was written without a loop. Each iteration was done by hand and inserted into the next Taylor Series Approximation.
The analytical solution is equivalent to the true value that the Taylor Series is approximating. The analytical solution is found by plugging in the points in the x interval [0,pi] into the equation for f(x). Here, I decided to estimate the planet's space coordinates at x1=pi as a sample calculation.
The numerical solution represents the Taylor Series approximation of the planet's space coordinates at point x1. Before approximating this value, the following intermediate calculations were performed.
Next, a Taylor Series was used to approximate the coordinates at x1=pi with a absolute true error stopping criteria of 0.015. This stopping criteria indicates when the Taylor Series approximated coordinates close enough to the true coordinates of the planet. The 0 order Taylor Series was performed first and the corresponding absolute true error was 2.0708. Since this error is greater than the stopping criteria, the Taylor Series was iterated again, this time to the first order. This process was repeated until the fourth order, where the stopping criteria was met. The Taylor Series approximations and the absolute true error of them is shown below.
Despite the code for my plot including axis labels, a title, and a legend, it would not include these into the graph of the results. Nothing I tried seemed to work and this was the only graph that would display from the code. The Taylor Series approximation was closer to the true value using higher order derivatives. The results of the Taylor Series approximations are shown below.
As a higher order derivative was used in the Taylor Series approximation, the error between the true and approximate value decreased. It only took four iterations for the error to be less than the stopping criteria of 0.015. It is important to note that the The absolute error of the Taylor Series approximations are shown below.
The absolute true error of the Taylor Series approximations will never be exactly zero. When the order of the Taylor Series is smaller, truncation error is the cause of most of the overall error. As the order increases, the truncation error decreases but the round-off error increases. There is a point in the series where the truncation and round-off error are equal. Then, for successive orders of the Taylor Series approximation, round-off error dominates the overall error.
The approximation was more accurate with each successive order of the Taylor Series. In this experiment, it only took 4 orders to reach the error stopping criteria. If an infinite number of terms was calculated, the Taylor Series would give an exact approximation. Overall, Taylor Series is an effective method for approximating the value of a function when used properly.
Consider the function π(π₯) = π₯^3 β 2π₯ + 4 on the interval [β2,2] with β = 0.25. Use the forward, backward, and centered finite difference approximations for the first and second derivatives to illustrate graphically which approximation is most accurate. Graph all three first derivative finite difference approximations along with the theoretical, and do the same for the second derivative as well.
1) Establish parameters h and x
2) Find the first derivative and second derivative analytical solutions:
For loop
equation for f'(x)
equation for f''(x)
end
3) Find the first derivative and second derivative numerical solutions for forward, backward, and center approximation methods:
For loop
Forward first derivative approx. equation
Backward first derivative approx. equation
Centered first derivative approx. equation
Forward second derivative approx. equation
Backward second derivative approx. equation
Centerd second derivative approx. equation
end
4) Plot first derivative approximations with analytical value
plot(x, forward first derivative)
plot(x, backward first derivative)
plot(x, centered first derivative)
plot(x, analytical first derivative)
Include legend, xlabel, ylabel, title
5) Plot second derivative approximations with analytical value
plot(x, forward second derivative)
plot(x, backward second derivative)
plot(x, centered second derivative)
plot(x, analytical second derivative)
Include legend, xlabel, ylabel, title
6) Calculate error between each iteration and true value:
abs((true-approx)/true)*100% for each first and second derivative approximation
7) Plot error of the first derivative approximation
plot(x, forward first derivative error)
plot(x, backward first derivative error)
plot(x, centered first derivative error)
plot(x, analytical first derivative error)
Include legend, xlabel, ylabel, title
8) Plot error of the second derivative approximation
plot(x, forward second derivative error)
plot(x, backward second derivative error)
plot(x, centered second derivative error)
plot(x, analytical second derivative error)
Include legend, xlabel, ylabel, title
The analytical solution represents the true values of the first and second derivatives at each step over the interval. The analytical solution for the first derivative is found by taking the derivative of f(x) and inserting every value between the interval [-2,2] that is a multiple of the step size (0.25). The general analytical solution for the second derivative was found by taking the derivative of f'(x) and inserting the same x-values into it.
The function and its first and second derivatives is shown below.
A sample calculation when x=1 is shown below. This solution represents one of the range of 17 solutions.
First, the derivative of the function was estimated using forward, backward, and centered approximations at each step over the interval. The equations of these formulas can be seen in the MATLAB code below. Next, the second derivative of the function was approximated using all three methods as well at each x-value over the interval. Then, each array was compared to the true value found in the analytical solution using a true error calculation.
The three first derivative approximations are parabolic over the interval. All approximations and the analytical value are the same at the point x=0. However, as the x-values stray from zero, the forward and backward approximations differentiate from the analytical value. The forward approximation estimates the first derivative to be higher than the true derivative for x-values greater than zero and lower for x-values less than zero. The backward approximation estimates the first derivative to be lower than the true derivative for x-values greater than zero and greater than for x-values less than zero. The centered approximation technique predicted the analytical solution perfectly for each x-value.
The second derivative approximations correctly resemble lines. The graphs should be linear because the original function is a cubic function and the second derivative of a cubic function is a linear function. Similar to the first derivative approximation, the centered approximation method estimates the exact second derivative. The forward approximation over-estimates the second derivative, while the backward approximation under-estimates the second derivative. The difference between the forward and backward methods and the true value remains the same because the slope of the lines is the same.
The percent relative error was calculated between the approximations and the analytical solutions. The formula for percent relative error is:
The error between the first and second derivative approximations is shown in the two graphs below.
In the first derivative percent relative error graph, the error is smallest for the centered finite difference approximation. The error for the forward and backward approximations is mirrored and the average error along the interval is the same for both approximation methods.
In the second derivative percent relative error graph, the error is zero for the centered finite difference approximation. The error for the forward and backward approximations is the same, which is shown be the overlap of every point along the x-interval. Additionally, the error for the forward and backward approximations is highest at x=0, or at the middle of the interval. As the x-value moves from zero, the error decreases.
The centered finite difference approximation correctly predicted the analytical value for the first and second derivatives. In most cases, the centered approximation will give the true derivative. Even so, the centered approximation method is more effective than the forward and backward approximation methods in most situations. This is because the centered approximation method uses both the values of the function one step size above and below the approximated value. In contrast, the forward approximation method just uses the value of the function one step size above the value being approximated. Lastly, the backward approximation method uses the analytical value of the function at a point one step size below the value being approximated.
I learned how to solve these problems most by reviewing my partner, Max Album's code. For the first submission, I tried to solve the second problem using a for loop multiple times. I could not solve it, and I had to resort to plugging in every value by hand. Max's approach, however, allowed me to see how to implement a for loop with the proper syntax. The one step that I was missing in my original attempt was the function callback at the bottom of the code. This command sets the function of the problem as a constant throughout so that it never has to be stated again. This function command made indexing the function with the interval easier for me to solve.
I made several small mistakes that Max pointed out in the code review process. In several instances, I made syntax mistakes such as not including quotes around my plot parameters. I also had several missing parentheses that Max highlighted. Max also gave me helpful encouragement about what was correct about my code versus what needed to be changed. This made it much easier to make small changes rather than reworking the entire problems.