In: Computer Science
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of iterations it took.
function [approx, final_e_a, num_iterations] = cal_arctan(x, n,
max_iterations)
% Matlab function to calculate the approximate value of arctan(x)
using the Maclaurin series approximation
% number of iterations
num_iterations = 1;
e_s = (0.5*(10^(2-n)))/100; % calculate the value of e_s for
approximation
ptan = 0; % previous value of arctan
ctan = x; % current value of arctan
final_e_a = abs(ptan-ctan); % absolute difference between current
and previous value of calculated arctan
sf = -1;
% loop continues till final_e_a >= e_s and number of
% iterations < maximum number of iterations
while(final_e_a >= e_s && num_iterations <
max_iterations)
ptan = ctan; % set current value of arctan to previous value of
arctan
% add the next term of series to current value of arctan
ctan = ctan + (sf
*((x^(2*num_iterations+1))/(2*num_iterations+1)));
num_iterations = num_iterations+1; % increment the number of
iterations
sf = -sf; % alternate the sign
final_e_a = abs(ptan-ctan); % absolute difference between current
and previous value of calculated arctan
end
approx = ctan;
end
Output: