Question

In: Computer Science

Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...

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.

Solutions

Expert Solution

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:


Related Solutions

Use Maclaurin series to the first three terms in of arctan (x)
Use Maclaurin series to the first three terms in of arctan (x)
Find the Maclaurin series for tan x and using that series, derive the Maclaurin series for...
Find the Maclaurin series for tan x and using that series, derive the Maclaurin series for sec^2(x)
Develop a well-structured MATLAB function to compute the Maclaurin series expansion for the cosine function and...
Develop a well-structured MATLAB function to compute the Maclaurin series expansion for the cosine function and name the function cosMac. The function should have the following features: Iterate until the relative error (variable name “relErr” ) falls below a stopping criterion OR exceeds a maximum number of iterations (variable name“maxIter”) for a given value of x. Include a default value of relErr = 1e-6 if the user does not enter the value (use nargin function). Include a default value for...
Find the Maclaurin series using the definition of a Maclaurin series as well as the interval...
Find the Maclaurin series using the definition of a Maclaurin series as well as the interval of convergence. a) f(x) = 1/((1-x/3)^2) b) f(x) = sin(2x)
What is the Maclaurin series for the function f(x)=arcsinx. Find the radius of convergence of the...
What is the Maclaurin series for the function f(x)=arcsinx. Find the radius of convergence of the series.
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to 3π/2 with increments equal to Δx,where Δx and n(the number ofterms used in the series approximation) should be input arguments of the function andshould be specified by the user.
Fourier Series Approximation Matlab HW1:     You are given a finite step function   x(t)=-1, 0<t<4...
Fourier Series Approximation Matlab HW1:     You are given a finite step function   x(t)=-1, 0<t<4          1, 4<t<8 .           Hand calculate the FS coefficients of x(t) by assuming half- range expansion,  for each case below and modify the code. Approximate x(t) by cosine series only (This is even-half range expansion). Modify the below code and plot the approximation showing its steps changing by included number of FS terms in the approximation. Approximate x(t) by sine series only (This...
Find the Maclaurin series of the function f(x)=(3x2)e^−4x (f(x)=∑n=0∞cnxn)
Find the Maclaurin series of the function f(x)=(3x2)e^−4x (f(x)=∑n=0∞cnxn)
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
Write a code to approximate the derivative of a function f(x) using forward finite difference quotient...
Write a code to approximate the derivative of a function f(x) using forward finite difference quotient f( x + h ) - f( x ) f'(x) ≈ ------------------- (for small h). h For the function f(x) = sin(x), at x=1 , compute the FD quotients for h = 1/2k, k=5,...,N, with N=30 and compare with the exact derivative cos(x). Output k , h , error. Where SHOULD the error tend as h → 0 ? 1. Look at the numbers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT