Question

In: Advanced Math

Develop a well-structured MATLAB function to compute the Maclaurin series expansion for the cosine function and...

  1. 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:

    1. 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.

    2. Include a default value of relErr = 1e-6 if the user does not enter the value (use nargin function).

    3. Include a default value for maxIter = 100 if the user does not enter the value (use nargin function).

    4. Return the estimate of cos(x), the approximate relative error, the number of iterations, and the true relative error (that you can calculate based on the built- in cosine function).

Solutions

Expert Solution


%Matlab code for Maclaurin series
clear all
close all

%example of cosMac function
x=pi;
[val,tr_er,rel_err,iter]=cosMac(x);
fprintf('For x=%f, cos(x)=%f with relative error=%e and iteration=%d\n',x,val,rel_err,iter)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function for Maclaurin series for cos(x)
function [val,tr_er,rel_err,iter]=cosMac(x,maxIter,relErr)

    if nargin < 3
        relErr = 10^-6;
    end
  
    if nargin < 2
        maxIter = 100;
    end
    err=1;k=1;vv(1)=1; val=1;
  
    while err>=relErr
        k=k+1;
        if mod(k,2)==1
            vv(k)=((x^(2*(k-1)))/(factorial(2*(k-1))));
        else
            vv(k)=-((x^(2*(k-1)))/(factorial(2*(k-1))));
        end
        val=val+vv(k);
        err=abs(abs(vv(k-1))-abs(vv(k)));
        if k>=maxIter
            break
        end
      
    end
  
  
    tr_er=(cos(x)-val);
  
    rel_err=err;
    iter=k;
end

%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%


Related Solutions

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)
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1...
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1 + ? + ? 2 2! + ? 3 3! + ? 4 4! + ⋯ + ? ? ?! This natural exponential function uses the irrational number ? as a base. You can find the number ? in Java as ???ℎ. ?, as well as, the exponential method as ???ℎ. ???(). You will implement your own exponential method following these criteria: 1. Determine...
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1...
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1 + ? + ? 2 2! + ? 3 3! + ? 4 4! + ⋯ + ? ? ?! This natural exponential function uses the irrational number ? as a base. You can find the number ? in Java as ???ℎ. ?, as well as, the exponential method as ???ℎ. ???(). You will implement your own exponential method following these criteria: 1. Determine...
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1...
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1 + ? + ? 2 2! + ? 3 3! + ? 4 4! + ⋯ + ? ? ?! This natural exponential function uses the irrational number ? as a base. You can find the number ? in Java as ???ℎ. ?, as well as, the exponential method as ???ℎ. ???(). You will implement your own exponential method following these criteria: 1. Determine...
3. Find the first three nonzero terms of the Maclaurin series expansion for the following function....
3. Find the first three nonzero terms of the Maclaurin series expansion for the following function. (10 Points) ?(?) = ?^cosx
3. Find the first three nonzero terms of the Maclaurin series expansion for the following function....
3. Find the first three nonzero terms of the Maclaurin series expansion for the following function. (10 Points) ?(?) = ?^cosx
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...
how to create a function to compute PACF of a time series in MATLAB without using...
how to create a function to compute PACF of a time series in MATLAB without using built-in function ''parcorr''?
Find the first 3 nonzero terms of the Maclaurin series for the function and the values...
Find the first 3 nonzero terms of the Maclaurin series for the function and the values for which the series converges absolutely. f(x)= x^4 * e^x^2
2. Do the Maclaurin series expansion of 1 √(1−?) out to 3 terms 3. Find the...
2. Do the Maclaurin series expansion of 1 √(1−?) out to 3 terms 3. Find the Taylor series for the two functions: i. ?(?) = ? −6? about ? = −4 ii. ?(?) = 7 ? 4 about ? = −3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT