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

The value of sin(x) at a given x using n terms can be approximated using the Tay

ID: 1857095 • Letter: T

Question


The value of sin(x) at a given x using n terms can be approximated using the Taylor series expansion
of the sine function (Image 1)


and is read as n factorial. (Note: 0! = 1.) For example, the sine of x = using the rst n = 5
terms of Eqn. 1 is approximately (Image 2)


Part A:
Create two dierent MATLAB functions to calculate n factorial (n!). One will use a for loop
and the other will use a while loop. Compare your functions to the built-in MATLAB function
factorial(n). Remember to choose function names that are not already taken by some variable
or function. For each function, write a owchart.


Part B:
Write a function le, sineseries.m, to compute the value of sin(x) at a given x using n terms of
the series expansion of the sine function. Your function will take in two inputs:
1. the number of terms to be used in the series, n
2. the angle, x
and outputs the nterm approximation to the sine function at the angle x.
Follow these steps:
1. First, query MATLAB to see if the name sineseries is already taken by some variable or
function.

2. Begin your function with the function line and a header comment line that describes the
purpose of the script le.
3. Use one of your MATLAB functions from Part A to compute n!.
4. Use the n term series approximation of sine to calculate the sin x where the inputs to the
function are the angle in radians, x, and the integer n.
5. Save the le. Type help sineseries to see if MATLAB can access your script le. Now
compute the sine series with x = =6 and n = 1; 5; and 10. Tabulate your results and
compare with the built-in function value for sin (=6).
6. Perform the same series of calculations for x = 2=3 and tabulate your results.
7. Using your function, ll in the missing values in the following table (Image 3)

Calculate the series for (x = =3 , n = 3) by hand. Compare this with the value calculated
by your user-dened function routine in order to verify that your function is working.


Part C:
Write a script le to plot the sine series approximation using your function created in Part C.
Follow these steps:
1. First, query MATLAB to see if the name you choose is already taken by some variable or
function.
2. Construct a owchart for an algorithm that will compute the sine series approximation, y, for
0 x 2 using n terms in the series.
3. Use your MATLAB function from Part B to compute the sine series approximation, y, for
0 x 2 using 3 terms in the series. (Note: x and y should be one-dimensional arrays with
the same number of elements).
4. Use the plot command to plot y versus x.
5. Use your MATLAB function from Part B to compute the sine series approximation, y, for
0 x 2 using 25 terms in the series. (Note: x and y should be one-dimensional arrays
with the same number of elements).
6. Use the plot command to plot y versus x.
Note: You can either run the le you created two times, changing the number of terms in the series,
or you can include two separate plot commands in one le. If you choose the latter option, insert the
command figure on a separate line between plot commands.This will open a new plotting window,
so the prior graph is not overwritten.


This should be one cohesive report that includes Parts A, B and C.
Description of exercise
The work must include
the owcharts
the M-scripts
the command-window results
the two graphs from Part C





Explanation / Answer

follow this

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsok1b$ruu$1@fred.mathworks.com>...
> "Sean " <el_sean@yahoo.com> wrote in message <gsohrh$27i$1@fred.mathworks.com>...
> > I'm trying to write a script to calculate the series expansion of sine with the inputs x (argument) and n. Here's what I've thrown together so far:
> >
> > % SINESERIES: computes sin(x) from series expansion.
> > % A script to evaluate the series expansion of sine with the formula:
> > % sin(x) = x-(x^3/3!)+(x^5/5!)-...
> > x = input('Enter the argument x: ');
> > n = input('Enter the interval n: ');
> > N = 1:1:n; % Creates row vector with n elements, 1 at a time.
> > k = 2*N-1; % For convenience with prod() function
> > j = N-1; % For cleanup.
> > sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))
> > sum(sinseries)

I should have pointed out that there are more efficient ways of carrying out that computation. Suppose n = 5. You could do the computation this way:

s = x*(1-x^2/(2*3)*(1-x^2/(4*5)*(1-x^2/(6*7)*(1-x^2/(8*9)))));

Moreover you can easily devise a for-loop that accomplishes this calculation in the same sequence of operations for a general n. You would have to work from the high end and go backwards. It obviously involves far fewer multiplications and divisions and is more accurate numerically in the bargain. In spite of the for-loop overhead, this method is bound to be faster for sufficiently large n, since it is an order n algorithm whereas your original one is order n^2.

Another observation: Don't try this with values of x substantially larger than 1 unless you have set n very high. High values of x will cause the series to converge very slowly at first and only when the ratio x/k grows substantially smaller than 1 does it begin to converge at a decent rate. In the actual computation of the sine function, things are done in a very different manner than this.

Roger Stafford

Subject:Script for series expansion of sine

From:Derek O'Connor

Date:23 Apr, 2009 04:26:01

Message:4 of 6

"Sean " <el_sean@yahoo.com> wrote in message <gsohrh$27i$1@fred.mathworks.com>...
> Hello folks,
>
> To begin with, I browsed through the archives looking for relevant posts to my question and found some related topics. However, the posts were ill-structured and, appropriately, weren't answered. Hopefully I can be a little more specific.
>
> I'm trying to write a script to calculate the series expansion of sine with the inputs x (argument) and n. Here's what I've thrown together so far:
>
> % SINESERIES: computes sin(x) from series expansion.
> % A script to evaluate the series expansion of sine with the formula:
> % sin(x) = x-(x^3/3!)+(x^5/5!)-...
> x = input('Enter the argument x: ');
> n = input('Enter the interval n: ');
> N = 1:1:n; % Creates row vector with n elements, 1 at a time.
> k = 2*N-1; % For convenience with prod() function
> j = N-1; % For cleanup.
> sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))
> sum(sinseries)
>
> My calculations aren't turning out at all correctly, short of x = 1, n = 1. I've been toying with the equation to see if I translated it wrong, but I think the problem lies in how I'm expressing the series. If anybody has some advice, I'd greatly appreciate it. Thanks.

Sean,

The notes herehttp://www.derekroconnor.net/NA/LE/LE-2006-2.pdfexplain why your code gives the wrong answers.

I hope you read and digest all 14 pages of this homework solution.

Regards,

Derek O'Connor.

Subject:Script for series expansion of sine

From:Roger Stafford

Date:23 Apr, 2009 07:14:02

Message:5 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsok1b$ruu$1@fred.mathworks.com>...
> At first glance I would say the culprit here is prod(k). You are expecting its results to be a vector consisting of all the products from 1 up to a number that increases by 2 each time. Instead you are getting a single scalar answer consisting all the products in k. It would only work for n - 1. What you need is cumprod instead.

Sean, I erred in telling you that just cumprod(k) would correct the error in your code. The prod(k) divisor is still certainly wrong, as I mentioned before. However, what you would need to rescue that code is this:

N = 1:n;
k = 2*N-1;
j = N-1;
p = cumprod(1:2*n-1);
sinex = sum((-1).^j.*x.^k./p(k));

To make up for that lapse I'll spell out in detail the for-loop method I mentioned.

x2 = x^2/2;
s = 1;
for k = n-1:-1:1
s = 1-x2/((2*k+1)*k)*s;
end
s = x*s;

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsok1b$ruu$1@fred.mathworks.com>...
> "Sean " <el_sean@yahoo.com> wrote in message <gsohrh$27i$1@fred.mathworks.com>...
> > I'm trying to write a script to calculate the series expansion of sine with the inputs x (argument) and n. Here's what I've thrown together so far:
> >
> > % SINESERIES: computes sin(x) from series expansion.
> > % A script to evaluate the series expansion of sine with the formula:
> > % sin(x) = x-(x^3/3!)+(x^5/5!)-...
> > x = input('Enter the argument x: ');
> > n = input('Enter the interval n: ');
> > N = 1:1:n; % Creates row vector with n elements, 1 at a time.
> > k = 2*N-1; % For convenience with prod() function
> > j = N-1; % For cleanup.
> > sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))
> > sum(sinseries)

I should have pointed out that there are more efficient ways of carrying out that computation. Suppose n = 5. You could do the computation this way:

s = x*(1-x^2/(2*3)*(1-x^2/(4*5)*(1-x^2/(6*7)*(1-x^2/(8*9)))));

Moreover you can easily devise a for-loop that accomplishes this calculation in the same sequence of operations for a general n. You would have to work from the high end and go backwards. It obviously involves far fewer multiplications and divisions and is more accurate numerically in the bargain. In spite of the for-loop overhead, this method is bound to be faster for sufficiently large n, since it is an order n algorithm whereas your original one is order n^2.

Another observation: Don't try this with values of x substantially larger than 1 unless you have set n very high. High values of x will cause the series to converge very slowly at first and only when the ratio x/k grows substantially smaller than 1 does it begin to converge at a decent rate. In the actual computation of the sine function, things are done in a very different manner than this.

Roger Stafford