Cos infinite series

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply Brittany 3/22/2010 12:13:04 AM

See related articles to this posting

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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply Walter 3/22/2010 2:47:43 AM
comp.soft-sys.matlab 201256 articles. 513 followers. Post

7 Replies
501 Views

Similar Articles

[PageSpeed] 24

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Infinite series
Hi: We have the following identity: \sum_{m=1}^{infinity} (-1)^m/((2m-3)^2*(2m-1)*(2m+1)^2)=-Pi/32. When we type the command, In[1]:=Sum[(-1)^m/((2*m-3)^2*(2*m-1)*(2*m+1)^2),{m,Infinity}] we get 2 1 1 -16 Pi + 2 Pi - HurwitzZeta[2, -(-)] - Zeta[2, -] 4 4 Out[1]= -------------------------------------------------- 512 The command Simplify[%] does not simplify it further. I am sure the above expression must be equal to -Pi/32, ...

Infinite series
Is anyone out there familiar with using MATLAB to manipulate infinite series calculations and where do I go to find?...cmw. ...

Re: Infinite series
On 12/15/09 at 7:27 AM, jogc@mecheng.iisc.ernet.in (Dr. C. S. Jog) wrote: >We have the following identity: >\sum_{m=1}^{infinity} (-1)^m/((2m-3)^2*(2m-1)*(2m+1)^2)=-Pi/32. >When we type the command, >In[1]:=Sum[(-1)^m/((2*m-3)^2*(2*m-1)*(2*m+1)^2),{m,Infinity}] >The command Simplify[%] does not simplify it further. >I am sure the above expression must be equal to -Pi/32, but a user >would prefer this answer than the above one. There are a variety of reasons Simplify often does not achieve what you are looking for. But the obvious thing to try when...

Mathematica and infinite series
Hi, I am about to embark on a project that operates heavily in infinite series, so I started figuring out Mathematica's basis capabilities. I found them very impressive, but I came across this: f[x_] := Sum[Log[n]/(n^2 Factorial[n]) x^n, {n, 1, Infinity}] Assuming[n > 0, SeriesCoefficient[f[x], {x, 0, 4}]] Answer: SeriesCoefficient[\!\( \*UnderoverscriptBox[\(\[Sum]\), \(n = 1\), \(\[Infinity]\)] \*FractionBox[\( \*SuperscriptBox[\(x\), \(n\)]\ Log[n]\), \( \*SuperscriptBox[\(n\), \(2\)]\ \(n!\)\)]\), {x, 0, 4}] Why doesn't Mathematica produce Log[n]/(n...

How to solve this infinite series...
The cosine function can be evaluated by the following infinite series: cos x = 1 &#8211; [x^2 / 21] + [x^4 / 41] &#8211; [x^6 / 61] &#8230;&#8230; Write a Matlab function to implement this formula so that it computes and prints out the values of cos(x) as each term in the series is added. In other words, compute and print in sequence the values for: cos x = 1 cos x = 1 - [x^2 / 21] cos x = 1 - [x^2 / 21] + [x^4 / 41] In article <fnlmg7$9g7$1@fred.mathworks.com>, Melvin <melvin1974@mathwoks.com> ...

infinite series #2
New to MATLAB. Trying to write code to determine the covergence value of an infinite series. I also need to display to 5 sig. fig. Any help would be appreciated. Thanks, Danny Danny Little wrote: > > > New to MATLAB. Trying to write code to determine the covergence > value > of an infinite series. I also need to display to 5 sig. fig. Any > help > would be appreciated. > > Thanks, Danny Take a look at the function symsum, from the Symbolic Math toolbox. Nilton Quoirin wrote: > > > Danny Little wrote: >> >> >> New to MATLAB. Trying to w...

How to solve this infinite series... #2
The cosine function can be evaluated by thefollowing infinite series: cos x = 1 &#8211; [x^2 / 21] + [x^4 / 41] &#8211; [x^6 / 61] &#8230;&#8230; Write a Matlab function to implement this formula so that it computes and prints out the values of cos(x) as each term in the series is added. In other words, compute and print in sequence the values for: cos x = 1 cos x = 1 - [x^2 / 21] cos x = 1 - [x^2 / 21] + [x^4 / 41] In article <fnqlf2$lmp$1@fred.mathworks.com>, Melvin <melvin1974@mathwoks.com> w...

A model problem for infinite series
Hi, As a follow up to my previous post and to give a little bit more information, I would like to briefly describe a mathematically nonsensical problem which has some of the elements that I need. To solve Laplace's equation for u(r, alpha) on the unit circle subject to Dirichlet boundary conditions U(alpha), one needs to decompose U as a Fourier series, multiply each term by r^|n| and add them back up. The model problem is this. Starting with a boundary condition U0, solve for u and let U1(alpha) = du/dr(evaluated at r=1)*f[alpha], where f[alpha] is relatively simple a...

Can Mathematica do this (infinite series)?
Hi, I'm working on a project involving infinite series and I don't know how to do it or even ask a sensible question about it. So I cooked up a question the answer to which might give me ideas. f[x_]:=Sum[c[n]x^n, {n, 1, Infinity}] What's the infinite series for f[x]^2 + Sin[x]f[x] in terms of c[n]? What's the simplest way that Mathematica can answer this question for general c[n]? The pipe dream is this: f[c_][x_] := Sum[c[n] x^n, {n, 1, Infinity}] g[c_][x_] := f[c][x]^2 + Sin[x] f[c][x] d[c_][n_] := SeriesCoefficient[g[c][x], {x, 0, n}] ...