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

MATLAB needed Taylor Series Objective: In the following program the student is t

ID: 3817286 • Letter: M

Question

MATLAB needed

Taylor Series

Objective:

          In the following program the student is to demonstrate knowledge and use of convergence using while. For the Taylor Series concept see “Taylor_Series” document.

To compute any Taylor Series, realize that the series is an infinite series; therefore we must find a way to end the series.   Since this series has the factorial as a denominator in the item called Term therefore that fraction will eventually get extremely small because factorial grows very fast and very large. Focus on the item called Term in the theory discussion.   Term is the actual equation for a single term of the series; the answer is the summation, via a while loop, of all of the terms. How do we terminate this loop since the number of loops in not deterministic? Since Term eventually becomes very small we will choose to compare each Term to epsilon, when the Term is less than epsilon we stop the loop.   The answer then is accurate to within epsilon. NOTE, that when the Term has alternating signs, the Term must be in an Absolute Value form when compared to epsilon.

NOTE: These Trigonometry functions using Taylor Series assume angle is in radians.

Requirements:

1. Write a Function Main and 3 embedded functions. Remember for embedded functions, there are no scripts.

2. This program is to cycle repeatedly until the user chooses to stop.

3. The user will choose to calculate sine, cosine, or sinh. It is important that all of the inputs and outputs to and from the user are done in the main function not in the working functions.

4. The user should input their choice of the function, the angle and epsilon from the theory equations.

5. Write 3 functions that calculate the Sine, Cosine, and Sinh respectively. The parameters for these functions are the angle, epsilon, and i.   The function’s output is the answer.

6. When the function returns the answer, you are to output the answer and i after the function runs.   This value of i will be the number of loops required to reach epsilon. Functions should be very narrow in what they are doing BUT also all inclusive of the singular task, using the parameters so that the function can be reused.

7. In the Main Script check your results by using the respective corresponding standard Matlab functions.

8. Submit a table showing your results compared to the Matlab functions using 3 different epsilons, 10^-3, 10^-6, and 10^-12.

Regarding factorial, as the i gets larger factorial may fail, force i to be double and it should work.   The problem is that the factorial can exceed the integer size in the computer.

Matlab has the built in function factorial below is a function I wrote that solves factorial in a recursive way. You can use either one.

function [F ]= FACT(x)

if (x <= 1)

    F = 1

else

    F = x * FACT(x - 1)

end

end

Explanation / Answer

Matlab code:

driver.m

while(1)
fprintf('Enter 1 to calculate sine, 2 for cos, 3 for sinh, 4 for exit! ');
c = input('');
if(c == 1)
fprintf('Enter value of angle ');
x = input('');
fprintf('Enter value of epsilon ');
e = input('');
[out,i] = mysin(x,e)
elseif(c == 2)
fprintf('Enter value of angle ');
x = input('');
fprintf('Enter value of epsilon ');
e = input('');
[out,i] = mycos(x,e)   
elseif(c==3)
fprintf('Enter value of angle ');
x = input('');
fprintf('Enter value of epsilon ');
e = input('');
[out,i] = mysinh(x,e)   
else
break;
end
end

mysin.m

function [out,i] = mysin(x,e)
out = 0.0;
i = 1;
term = ((-1)^(i-1))*(x^(2*i - 1)/factorial(2*i - 1));
while( abs(term) > e)
out = out + term;
i = i + 1;
term = ((-1)^(i-1))*(x^(2*i - 1)/factorial(2*i - 1));
end
i = i - 1;
end

mycos.m

function [out,i] = mycos(x,e)
out = 0.0;
i = 0;
term = ((-1)^(i))*(x^(2*i)/factorial(2*i));
while( abs(term) > e)
out = out + term;
i = i + 1;
term = ((-1)^(i))*(x^(2*i)/factorial(2*i));
end
end

mysinh.m

function [out,i] = mysinh(x,e)
out = 0.0;
i = 0;
term = ((x^(2*i + 1)/factorial(2*i + 1)));
while( abs(term) > e)
out = out + term;
i = i + 1;
term = ((x^(2*i + 1)/factorial(2*i + 1)));
end
end

Sample Output:

Enter 1 to calculate sine, 2 for cos, 3 for sinh, 4 for exit!
1
Enter value of angle
pi/2
Enter value of epsilon
10^-3

out =

0.9998


i =

4

Enter 1 to calculate sine, 2 for cos, 3 for sinh, 4 for exit!
1
Enter value of angle
pi/2
Enter value of epsilon
10^-6

out =

1.0000


i =

6

Enter 1 to calculate sine, 2 for cos, 3 for sinh, 4 for exit!

1
Enter value of angle
pi/2
Enter value of epsilon
10^-12

out =

1.0000


i =

9

Output Table:

Epsilon e = 10^-3 e = 10^-6 e = 10^-12

Output sin(pi/2) Value 0.9998 1.000 1.000

Output cos(pi/2) Value -8.9452e-04 -4.6477e-07 5.2606e-13

Output sinh(pi/2) Value 2.3011 2.3013   2.3013