Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a MATLAB function to evaluate exp(x) using the Taylor seriese exp (x) =

ID: 2993857 • Letter: 1

Question

1) Write a MATLAB function to evaluate exp(x) using the Taylor seriese                

                    exp (x) = sigma n=0 to infinity X^n /n!                

                    The inputs are the value of x and the desired absolute accuracy of exp (x). be sure to have a test for non convergence. If the series fails to converges tell                    the user the value returned has not converged and is not accurate (has not converged to the desired accuracy .) your messaage in the case of non convergence                    should inform the user why non convergence occured.                

                    2) Develop the summation expression and write a second MATLAB function similarto above sin(x) using the Tylor series expansion                

                    sin(x) = x- x^3/3! + x^5/5! -x^7/7!...........                

                    Th

Just answer second part of questin

Explanation / Answer

1) Try the following MATLAB code to calculate exp(x):


%function to evaluate exp(x) using Taylor series
function [value] = exp_x(x,n)
fact(1) = 1;
value = 0;
i = 0;
err = 1;
while err>n
    if i~=0
        f = 1;
        for j = 1 : i
            f = f * j;
        end
        fact(i+1) = f; %stores the factorial
    end
    value = value + (x^i)/fact(i+1);
    err = abs(exp(x)-value);
    i = i + 1;
    if(i > 1000 | value == Inf)
        fprintf('The series has failed to converge to the desired accuracy. ');
        break;
    end
end
fprintf('exp(x) = %f ',value);

-----------------------------------------------------

%calling the function
clc; clear all;
n = input('Desired accuracy (total number of terms in summation) = ');
x = input('Value of x = ');
[value] = exp_x(x,n);



2) For calculating sin(x) using Taylor Series:


%function to evaluate sin(x) using Taylor series
function [value2] = sin_x(x,n)
value2 = 0;
t = 1;
i = 1;
err = 1;
while err>n
   f = 1;
    for j = 1 : i   %calculates factorial
        f = f * j;
    end
    value2 = value2 + (-1)^(t+1)*(x^i)/f;
    t = t + 1;
    i = i + 2;
    err = abs(sin(x)-value2);
    if(i > 2000 | abs(value2) == Inf)
        fprintf('The series has failed to converge to the desired accuracy. ');
        break;
    end
end
fprintf('sin(x) = %f ',value2);

----------------------------------------------------------

%calling the function
clc; clear all;
n = input('Desired accuracy (total number of terms in summation) = ');
x = input('Value of x = ');
[value2] = sin_x(x,n);