Thread Subject: Cos infinite series

Subject: Cos infinite series

From: Brittany Morgante

Date: 22 Mar, 2010 00:13:04

Message: 1 of 8

I'm having trouble trying to write a matlab function for the cos infinite series in matlab terms
cos(x)=1-x^2/2!+x^4/4!-x^6/6!+...

I googled the cos infinite function and got ((-1^n)(x^2*n))/(2n)!
I plugged it into matlab, it seemed to work. but when i plugged the values into both of the equations to check the math i got 2 different answers. theoretically the two functions should be the same, but all depending on what value is chosen for x in the second function.

Basically what im ask for is if someone has found a way to write the cos(x) infinite series in a for loop that matlab can understand.

Subject: Cos infinite series

From: Jan Simon

Date: 22 Mar, 2010 00:34:03

Message: 2 of 8

Dear Brittany!

> I'm having trouble trying to write a matlab function for the cos infinite series in matlab terms
> cos(x)=1-x^2/2!+x^4/4!-x^6/6!+...
>
> I googled the cos infinite function and got ((-1^n)(x^2*n))/(2n)!
> I plugged it into matlab, it seemed to work. but when i plugged the values into both of the equations to check the math i got 2 different answers.

Obviously both formulas are identical. If you have "plugged it into Matlab", and got 2 different answers, just show us what you've written and we find the bug.
 
> Basically what im ask for is if someone has found a way to write the cos(x) infinite series in a for loop that matlab can understand.

Yes, Brittany, *you* have found 2 ways already! One of the implementations seems to have a small bug, but this can be fixed.

Kind regards, Jan

Subject: Cos infinite series

From: Brittany Morgante

Date: 22 Mar, 2010 00:45:05

Message: 3 of 8

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <ho6dtr$koa$1@fred.mathworks.com>...
> Dear Brittany!
>
> > I'm having trouble trying to write a matlab function for the cos infinite series in matlab terms
> > cos(x)=1-x^2/2!+x^4/4!-x^6/6!+...
> >
> > I googled the cos infinite function and got ((-1^n)(x^2*n))/(2n)!
> > I plugged it into matlab, it seemed to work. but when i plugged the values into both of the equations to check the math i got 2 different answers.
>
> Obviously both formulas are identical. If you have "plugged it into Matlab", and got 2 different answers, just show us what you've written and we find the bug.
>
> > Basically what im ask for is if someone has found a way to write the cos(x) infinite series in a for loop that matlab can understand.
>
> Yes, Brittany, *you* have found 2 ways already! One of the implementations seems to have a small bug, but this can be fixed.
>
> Kind regards, Jan

n=1;
% x=1
cosval=1;
for i=1:1:n
    cosval=((-1^n)*x^(2*n))/(factorial(2*n)) %formula found through google
% cosval=cosval-(-1^(i+1)/factorial(2*n)) %formula made up using original formula on hw paper
end

Subject: Cos infinite series

From: Walter Roberson

Date: 22 Mar, 2010 00:40:22

Message: 4 of 8

Brittany Morgante wrote:
> I'm having trouble trying to write a matlab function for the cos
> infinite series in matlab terms
> cos(x)=1-x^2/2!+x^4/4!-x^6/6!+...
>
> I googled the cos infinite function and got ((-1^n)(x^2*n))/(2n)!
> I plugged it into matlab, it seemed to work. but when i plugged the
> values into both of the equations to check the math i got 2 different
> answers. theoretically the two functions should be the same, but all
> depending on what value is chosen for x in the second function.

You are very likely hitting floating point round off errors. (Provided,
that is, that you missed out a summation in stating the second version
of the expression.)

You can increase the accuracy of the summation by running the loop in
reverse, from highest n to lowest n. Calculating the correct n to start
from might be a bit tricky through.

Subject: Cos infinite series

From: Jan Simon

Date: 22 Mar, 2010 01:45:07

Message: 5 of 8

Dear Brittany!

> > > cos(x)=1-x^2/2!+x^4/4!-x^6/6!+...
> > >
> > > I googled the cos infinite function and got ((-1^n)(x^2*n))/(2n)!

> n=1;
> % x=1
> cosval=1;
> for i=1:1:n
> cosval=((-1^n)*x^(2*n))/(factorial(2*n)) %formula found through google
> % cosval=cosval-(-1^(i+1)/factorial(2*n)) %formula made up using original formula on hw paper
> end

The idea of an infinite sum is definitely different from calling a FOR loop over only one single number.
Please look at the two *equal* definitions of the formula and insert it in your loop. You've confused the loop index "i" and "n". Use just one of them inside your loop! Finally run the loop for a lot of itereations.
And look again to the formulas you've googled correctly:
  ((-1^n)(x^2*n))/(2n)!
and
  1 - x^2/2! + x^4/4! - x^6/6! + ...
must give identical results. Therefore the "n" (or call them "i") must start at 0. Try it!

Good luck, Jan

Subject: Cos infinite series

From: Walter Roberson

Date: 22 Mar, 2010 01:55:39

Message: 6 of 8

Brittany Morgante wrote:

> n=1;
> % x=1
> cosval=1;
> for i=1:1:n
> cosval=((-1^n)*x^(2*n))/(factorial(2*n)) %formula found through google
> % cosval=cosval-(-1^(i+1)/factorial(2*n)) %formula made up using
> original formula on hw paper
> end

Biggest mistake in the new version: you are not summing the values. Each
loop iteration is going to overwrite the value from the previous loop.

This is in addition to the other poster who pointed out that your
confusion about i and n.

Subject: Cos infinite series

From: Brittany Morgante

Date: 22 Mar, 2010 02:21:04

Message: 7 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <ho6imr$3n1$1@canopus.cc.umanitoba.ca>...
> Brittany Morgante wrote:
>
> > n=1;
> > % x=1
> > cosval=1;
> > for i=1:1:n
> > cosval=((-1^n)*x^(2*n))/(factorial(2*n)) %formula found through google
> > % cosval=cosval-(-1^(i+1)/factorial(2*n)) %formula made up using
> > original formula on hw paper
> > end
>
> Biggest mistake in the new version: you are not summing the values. Each
> loop iteration is going to overwrite the value from the previous loop.
>
> This is in addition to the other poster who pointed out that your
> confusion about i and n.

with using only one of the formulas, how do you tell matlab to sum the values instead of only getting individual values for each term?
i havent been using the "newest" version of matlab, but the r2008a version because that is what the school that i am going to provides on all the computers.
 x=1;
cosval=1;
 for i=1:1:n
cosval=((-1^i)*x^(2*i))/(factorial(2*i))
 end

Subject: Cos infinite series

From: Walter Roberson

Date: 22 Mar, 2010 02:47:43

Message: 8 of 8

Brittany Morgante wrote:

> with using only one of the formulas, how do you tell matlab to sum the
> values instead of only getting individual values for each term?

total = 0
for IndexName = 1:FinalIndex
   ThisValue = SomeExpressionInvolving_IndexName
   total = total + ThisValue
end

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
homework Brittany Morgante 21 Mar, 2010 20:14:08
rssFeed for this Thread

Contact us at files@mathworks.com