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 203088 articles. 524 followers. Post

7 Replies
601 Views

Similar Articles

[PageSpeed] 0


  • 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 fit with an infinite serie function ?
I would like to fit non-linear data points I have collected from FRAP experiments. The function to be fitted is an infinite serie : f = Somme (((-b)^n/factorial(n))/(1+n*(1+2*t/tauD))) with n=1 to 100 I need to find the best value for my parameters b and tauD. I have tried to use the curve fitting tool but the function was not accepet as custom equation. I'm totally novice in matlab so I would be pleased if someone could help me. "Aurelie Chabaud" <aurelie.billon@univ-nantes.fr> wrote in message <i16rvl$hmf$1@fred.mathworks.com>... > I would like to f...

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}] ...

A Series of Convolution of Infinite Sequences
I have a set of $M$ infinite data sequences, and I hope to perform a serie of convolutions such that SEQ_result = SEQ_1 * SEQ_2 * SEQ_3 * ... SEQ_M I am only interested at the first few terms of SEQ_result. Right now, I can only think of one method, which is to use fas convolution (truncation all to finite sequences, FFT, pointwise multiply iFFT). Is there other efficient way to implement such system? Thanks for input. CJ On Nov 28, 11:21 am, "cjlam" <jethro...@gmail.com> wrote: > I have a set of $M$ infinite data sequences, and I hope to perform a series > of ...

Infinite Series Error
Thanks to everyone who responded (not sure why I cannot see the responses at the newsgroup). Here is something peculiar: Sum[q^(-6 + 4*n)/(1 - q^(-5 + 4*n)), {n, 0, Infinity}] /. {q -> 0.1} gives an error, but (Sum[q^(-6 + 4*n)/(1 - q^(-5 + 4*n)), {n, 0, 1}] /. {q -> 0.1}) + (Sum[ q^(-6 + 4*n)/(1 - q^(-5 + 4*n)), {n, 2, Infinity}] /. {q -> 0.1}) gives the correct answer -21.1012 ...

Infinite Series Error
Can anyone explain why evaluating the following expression gives an error? Sum[q^(-6 + 4*n)/(1 - q^(-5 + 4*n)), {n, 0, Infinity}] /. {q -> 0.1} None of the denominators are 0 (would only happen if -5 + 4*n=0, which is impossible for an integer n, yet I get an error message "Infinite expression 1/0 encountered" (several times in fact). Any help on a work around greatly appreciated. (Please reply to my personal e-mail also). Thanks, Jimmy Hello Jimmy, Thank you for reporting the error with the infinite sum considered by you. The unexpected Power::infy...

problems with hypergeom and infinite series
Dear all, I have encountered the following problems with MatLab R2010b. 1. The following instruction >> hypergeom(62,[64,26],-400 cannot get the answer and I dont know why. 2. The following m-file cannot give the plot. In fact it stops at one point and I found the mistake maybe the hypergeom instruction, but I have no idea how to fix it. Could anyone help me in overcoming this problem? Many thanks in advance! Kindest regards, Dale Guo clc clear all close all r=3.; lam=5.32e-4; k=2*pi/lam; n=25; f=500; alpha=9.9; rho=linspace(0.,1,100); len=length(rho)...

Infinite loop calculating a series value
I'm trying to calculate the value of this expresion: ABS(\sum(K=1, \infty, ((1+i)/2)^k)) the result should be 1 but my calculator enter in a infinite loop. How can I fix it? Thank you, bye. On Mon, 10 Mar 2008 09:26:58 -0500, mambro wrote: > I'm trying to calculate the value of this expression: > ABS(sum(K=3D1,infinity,((1+i)/2)^k)) > the result should be 1 > my calculator enters an infinite loop. An explicit sum of an infinite number of terms will tend to do that. You could replace "infinity" by 100, say, or else use a limit: First do command CASCFG, the...

polynomial division which gives a infinite series
hi is there any code or built in function in matlab to perform polynomial division betwee two finite polynomials to get an infinite series ,,, pls dont suggest me to use deconv function this gives zero as quotient for division of lower order polynomial with the higher order one,,, please post in the answer as soon as possible thanx in advance.................. "sree vidyanikethan engineering college Morusu" <madan_morusu@gmail.com> wrote in message <gn917l$l0v$1@fred.mathworks.com>... > hi > > is there any code or built in function in matlab to perfo...

Print value of p from the infinite series
Hi, new here. I'm trying to print a table that shows how many terms needed to accomplished 3.14, 3.141, 3.1415, 3.14159. But, dead end with weird logic-error (at least for me, right now). Here is how, suppose the output should something like: <code> full: p: flag: p in int: counter: 4.000000 4.000 true 4000 1 2.666667 2.667 false 2666 2 3.466667 3.467 true 3466 3 .... <snipped> .... 3.142186 3.142 true 3142 1685 3.141000 3.141 f...

calculate the value of pi from the infinite series
does anyone know how to calculate the value of pi from the infinite series pi=4-4/3+4/5-4/7+4/9-4/11+... then how to shows the value of pi approximated by one term of this series, by two terms, by three terms,etc. How many terms of this series do you have to use before you first get a)3.14 b)3.141 c)3.1415 d)3.14159 "atiqa eiqa" <atiqa_eiqa@yahoo.com.my> wrote in message <gr2me5$2h8$1@fred.mathworks.com>... > does anyone know how to calculate the value of pi from the infinite series > > pi=4-4/3+4/5-4/7+4/9-4/11+... > > then how to shows the value of...

Re: Mathematica and infinite series #2
No, this sort of thing certainly won't work. Unless Mathematica can find a closed form of the sum it will not be able to compute a general series coefficient from an infinite series. However, it will work if you truncate the series than you can get particular series coefficients, like this: f == Sum[BesselJZero[0, n^2] Sin[n^2] Log[ Sin[Cos[n]]] Log[n]/(n^2 Factorial[n]) x^n, {n, 1, 10}] + O[x]^11; SeriesCoefficient[f, 4] and so on. Andrzej On 17 Oct 2010, at 21:44, Sam Takoy wrote: > Hi, > > Thanks for the response! > > I don't have Ma...

this is my solution of calculate pi using infinite series
%include <iostream> %include <cmath> %include <iomanip> using namespace std; int main() [ // Declare and initalise variables int decPlaces; double fraction=0, pi=0; cout << "Program to calculate pi using the sum of an infinite series" << endl; cout << "Please input the number of decimal places to display: " << endl; cin >> decPlaces; cout << end1; // calculate the value for pi using a 100 step loop for ( int n = 1; n <= 100; n++ ) [ fraction = (4*pow(-1.0, n+1))/(2*n...

compute cos(x) using taylor series
I need to compute using the taylor series but i must also name the function, i have looked around and from what i can understand this is how i thought it needs to be done: %x- the argument %N &#8211; the initial number of terms in the expansion function[x N]=ty_cos(x,N) for x=1:N y=feval(taylor(cos(x))) end end either my understanding of this is very wrong or my coding is wrong seeign as it doesnt work, all i need to be able to do, is create the taylor series expansion of cos(x) for the amount of terms N Dear Kevin! > I need to compute using the taylor series but i must...

this is my solution of calculate pi using infinite series #2
%include <iostream> %include <cmath> %include <iomanip> using namespace std; int main() [ // Declare and initalise variables int decPlaces; double fraction=0, pi=0; cout << "Program to calculate pi using the sum of an infinite series" << endl; cout << "Please input the number of decimal places to display: " << endl; cin >> decPlaces; cout << end1; // calculate the value for pi using a 100 step loop for ( int n = 1; n <= 100; n++ ) [ fraction = (4*pow(-1.0, n+1))/(2*n...

Command stand for Series(Sigma from n=1 to infinite
Which command can be used as inf =A1=C6(function) n=3D1 ...

Which Command stands for Series(Sigma from n=1 to infinite)??? ThankU...
Which command can be used as inf =A1=C6(function) n=3D1 ...