Planetary Movement

Prompt

Solution

Analytical

The steps to solve this problem with an analytical solution are fairly straightforward. All we need to do is plug x = pi/2 into the provided equation for f(x). This evaluates to ( pi/2) - 1.5 which is around 0.0708. We use this answer to compare with our numerical approach and to determine how many terms we need on the Taylor series to drop below our maximum allowed error.



Pseudocode for Numerical Approach

%Variable Declaration / Initialization a = pi/2; %base pointh = 0.01; %step sizex_range = (0:h:pi)'; %vector of x-coordinates % Function (y_0) and derivativesy_0 = (x_range) - 1 - 0.5*sin(x_range);...y_7 = 0.5*cos(x_range);
%Calculate function values @ 'a' for use in Taylor series y_a0 = (a) - 1 - 0.5*sin(a); %Function at 'a'...y_a7 = 0.5*cos(a); %Seventh Derivative at a % Taylor series approximationsT0 = y_a0; %Zero-th orderT1 = T0 + ((y_a1)*((x_range - a).^1/factorial(1))); ...T7 = T6 + ((y_a7)*((x_range - a).^7/factorial(7)));
%%ErrorError_Numerical_0 = abs(T0 - y_0);...Error_Numerical_7 = abs(T7 - y_0); %Plot f(x) with first, 3rd, 5th, and 7th taylor approximations%Plot Various Errors to show increasing accuracy with higher taylor series%Plot 4th order error

Numerical

Our objective with the numerical approach is to construct a Taylor series expansion of the original function f(x) with base a = pi/2. We then observe the behavior of the Taylor series approximations along the interval ( 0 < x < pi). Adding additional terms to the Taylor series approximation adjusts the new function to more closely fit the original function f(x), thus reducing our error. Our goal is to determine how many additional terms are required on the Taylor series in order to reduce the maximum error along the specified interval to below 0.015. See below for a visual representation of this as well as example calculations and pseudocode (for implementation in MATLAB).

Discussion

Figure 1: Depicts the function with 1st, 3rd, 5th, and 7th order Taylor Approximations

Results

As we increase the amount of terms we add to the taylor series the approximations become significantly more accurate. Notice in figure 1 how the 7th-order approximation hugs the function on the given interval, almost exactly estimating its change. As we will see in the error analysis, a fourth-order taylor approximation is sufficient to minimze error along the given interval of x below 0.015.

Error Analysis

Figure 2: Depicts the error for the fourth-order Taylor approx.

Notice how the error spikes on both ends of the interval, and is minimal at around x = 1.57, which is our base point "a = pi/2". The maximum error for the fourth-order approximation on this interval is 0.010, which is below our target of 0.015. The third-order error does not meet this criterion.

Figure 3: Various errors for higher-order Taylor approx.

Notice how the 2nd order has a signifcantly larger error compared to the 6th order, which practically hugs the y-intercept. This is because adding more terms causes a Taylor approximation to "behave" more like the original function.

Appendix - Matlab code

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Author: Bryan Bennett%Date: 2/18/2020%Problem: The purpose of this code is to investigate how additional terms%in a Taylor Series approximation increase the accuracy of that approximation%and reduce the corresponding error.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; %% Set up, function and variable declaration a = pi/2; %base pointh = 0.01; %step size (in x)x_range = (0:h:pi)'; %vector of x-coordinates % Function (y_0) and derivativesy_0 = (x_range) - 1 - 0.5*sin(x_range); y_1 = 1 - 0.5*cos(x_range); %First Derivativey_2 = 0.5*sin(x_range); %...y_3 = 0.5*cos(x_range);y_4 = -0.5*sin(x_range);y_5 = -0.5*cos(x_range);y_6 = 0.5*sin(x_range);y_7 = 0.5*cos(x_range); %Seventh Derivative %% Plugging in 'a', solving for Taylor Series, and Error %Calculate function & derivative values @ point 'a' for use in Taylor series y_a0 = (a) - 1 - 0.5*sin(a); %Function at ay_a1 = 1 - 0.5*cos(a); %First derivative at ay_a2 = 0.5*sin(a); %...y_a3 = 0.5*cos(a);y_a4 = -0.5*sin(a);y_a5 = -0.5*cos(a);y_a6 = 0.5*sin(a);y_a7 = 0.5*cos(a); %Seventh Derivative at a % Taylor series approximationsT0 = y_a0; %Zero-th orderT1 = T0 + ((y_a1)*((x_range - a).^1/factorial(1))); %First order. Uses previous term's value (T0) T2 = T1 + ((y_a2)*((x_range - a).^2/factorial(2))); %Second order. Uses previous term's value (T1)T3 = T2 + ((y_a3)*((x_range - a).^3/factorial(3))); %...T4 = T3 + ((y_a4)*((x_range - a).^4/factorial(4)));T5 = T4 + ((y_a5)*((x_range - a).^5/factorial(5)));T6 = T5 + ((y_a6)*((x_range - a).^6/factorial(6)));T7 = T6 + ((y_a7)*((x_range - a).^7/factorial(7))); %Seventh order taylor approximation %%ErrorError_Numerical_0 = abs(T0 - y_0); %Error for zero-order Taylor approx.Error_Numerical_1 = abs(T1 - y_0);Error_Numerical_2 = abs(T2 - y_0);Error_Numerical_3 = abs(T3 - y_0);Error_Numerical_4 = abs(T4 - y_0);Error_Numerical_5 = abs(T5 - y_0);Error_Numerical_6 = abs(T6 - y_0);Error_Numerical_7 = abs(T7 - y_0); %Error for seventh-order Taylor approx. %% Plots %Plot f(x) with first, 3rd, 5th, and 7th taylor approximationsfigure(1)hold all;plot(x_range,y_0,'-.', 'LineWidth',5,'Markersize',6,'Color',[17 17 17]/255);plot(x_range,T1,'-o', 'LineWidth',3,'Markersize',6,'Color','r');plot(x_range,T3,'-.', 'LineWidth',3,'Markersize',6,'Color','b');plot(x_range,T5,'-o', 'LineWidth',1,'Markersize',6,'Color','y');plot(x_range,T7,'-.', 'LineWidth',2,'Markersize',6,'Color','g');xlabel('x-distance');ylabel('y-value');title('Function with various taylor approximations overlaid');legend('Function','First Order','Third Order','Fifth Order','Seventh Order','location','northwest')box on; grid on; %Plot Various Errors to show increasing accuracy with higher taylor seriesfigure(2)hold all;plot(x_range,Error_Numerical_2,'-o', 'LineWidth',1,'Markersize',6,'Color','r');plot(x_range,Error_Numerical_3,'-.', 'LineWidth',1,'Markersize',6,'Color','g');plot(x_range,Error_Numerical_4,'-o', 'LineWidth',1,'Markersize',6,'Color','b');plot(x_range,Error_Numerical_5,'-.', 'LineWidth',1,'Markersize',6,'Color','r');plot(x_range,Error_Numerical_6,'-o', 'LineWidth',1,'Markersize',6,'Color',[17 17 17]/255);xlabel('x-coordinate');ylabel('y-coordinate');title('Errors for various taylor approximations');legend('2nd','3rd','4th','5th','6th')box on; grid on; %Plot 4th order error. THIS IS WHAT I OBSERVED TO BE THE ONE THAT MATTERSfigure(3)hold all;plot(x_range,Error_Numerical_4,'-o', 'LineWidth',3,'Markersize',6,'Color','r');xlabel('x-coordinate');ylabel('Error');title('Error for 4th-order taylor approximation');legend('Numerical error for 4th-order')box on; grid on;