%define the value of x
x = 2;
%define an array of values for n (number of terms in the Taylor Series)
n_values = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
% Initialize arrays to store n and %Error values
n_array = [];
error_array = [];
%calculate %Error for different values of n
for n = n_values
%calculate the Taylor Series sum for the given n
TaylorSeriesSum = 1; % Initialize with the first term (n = 0)
factorial = 1;
power_x = 1;
for i = 1:n
factorial = factorial * i;
power_x = power_x * x;
TaylorSeriesSum = TaylorSeriesSum + power_x / factorial;
end
%calculate the OriginalValue using the exp(x) function
OriginalValue = exp(x);
%calculate %Error
error = ((OriginalValue - TaylorSeriesSum) / OriginalValue) * 100;
% Append n and %Error to the respective arrays
n_array = [n_array, n];
error_array = [error_array, error];
end
% Create a plot to visualize the effect of increasing n on %Error
figure;
plot(n_array, error_array, 'k-', 'LineWidth', 1.5, 'DisplayName', 'Error');
title('Effect of Number of Terms on Accuracy of Taylor Series Approximation');
xlabel('Number of Terms (n)');
ylabel('% Error');
legend('Location', 'Northwest');
grid on;