f



Cos infinite series

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.
0
Brittany
3/22/2010 12:13:04 AM
comp.soft-sys.matlab 210980 articles. 13 followers. lunamoonmoon (258) is leader. Post Follow

7 Replies
1076 Views

Similar Articles

[PageSpeed] 56

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
0
Jan
3/22/2010 12:34:03 AM
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.
0
Walter
3/22/2010 12:40:22 AM
"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
0
Brittany
3/22/2010 12:45:05 AM
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
0
Jan
3/22/2010 1:45:07 AM
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.
0
Walter
3/22/2010 1:55:39 AM
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
0
Brittany
3/22/2010 2:21:04 AM
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
0
Walter
3/22/2010 2:47:43 AM
Reply: