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 207146 articles. 0 followers. lunamoonmoon (258) is leader. Post Follow

7 Replies
793 Views

Similar Articles

[PageSpeed] 21
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:
Similar Artilces:

Creating a series of "vectors" using x-y chart
Hello Excel Gurus: Here's one that has me stumped. I would like to use an Excel chart to produce figure with a series of "vectors". In my lab, we do cycling biomechanics and I would like be able to print out a graphic for the cyclists who take part in our studies. They all want to see how they exert force on the pedal. So I have created an x-y chart that represents the location of the pedal and the magnitude and direction of the force at several discrete crank angles. That part is easy enough, I use the x-y coordinates for the pedal spindle at several points of the ...

the distance between two time series
I'd like a measure that passed some simple tests. Here are a few. The distance should be zero if any of the following are true: 1) Yi = Xi + a 2) Yi = a*Xi 3) ?? -- I don't know what other conditions should be added. And the distance should be one if the following are true: 1) ?? 2) ?? Any ideas? "Robert pisani" <r.pisani@mac.com> wrote in message <hr4apk$bar$1@fred.mathworks.com>... > I'd like a measure that passed some simple tests. Here are a few. > > The distance should be zero if any of the following are true: > 1) Yi = ...

neural network for forecasting the time series data
hello friends i am doing forecasting of time series data. I did it using AR method. i wish to use "newnarxsp". Anyone can help me for the code or hint for me. Please urgent.... Jamuna ...

sin and cos
Is it possibe to obtain algebraic formulas for fractions of pi? How do I get Maple to write out cos(Pi/5) for example? Try convert(cos(Pi/5),radical); Preben Alsholm boi wrote: > Is it possibe to obtain algebraic formulas for fractions of pi? > How do I get Maple to write out cos(Pi/5) for example? On Wed, 24 Sep 2003, boi wrote: > Is it possibe to obtain algebraic formulas for fractions of pi? > How do I get Maple to write out cos(Pi/5) for example? Some very simple cases like cos(Pi/5) can be handled by convert(..., radical). For example, convert(cos(Pi/...

how to do this : sin(x)*cos(y)+sin(y)*cos(x)= sin(x+y)
i'm using Maple Toolbox For Matlab, using command : > syms x y >simple(sin(x)*cos(y)+sin(y)*cos(x)) but do not show the result : sin(x+y) how to do this ? "Huy " <huyconfid@yahoo.com> wrote in message <gnltg7$pdb$1@fred.mathworks.com>... > i'm using Maple Toolbox For Matlab, > using command : > > syms x y > >simple(sin(x)*cos(y)+sin(y)*cos(x)) > but do not show the result : sin(x+y) > how to do this ? Hi, It is pretty simple. The solution to your problem is maple('combine','sin(x)*cos(y)+sin(y)*cos(x)','trig&...

Stopped because of infinite likelihood
Dear all, I am using a proc mixed and I am getting the error " Stopped because of infinite likelihood" This is related with dthe independent variable because I have changed the dependent one and the problem persists. I have seen some people saying that this is due to repeated entries..but I am affraid I have doubts about the meaning of this. Is it a problem if I have repeated values on the independent variable? I am using as random effects a covariance matrix. And two different models with the same matrix were different, one ran fine and the other gave this problem. ...

How to handle ID's in series (myID01-myID50, otherID01-otherID50)
Hi, very new here. I had some help getting this far so I don't fully understand the code below (I understand it as I read it, but not quite enough that I could write it from scratch). Thanks to Lasse in this group for helping me get this far. Ok, here's what I have: --- // Create an array of our tabs var divArray = []; function cacheDivs() { var divs = document.getElementsByTagName("div"); for (var i=0;i<divs.length;i++) { if (/^(tab\_overview|tab\_resources)/.test(divs[i].id)) { divArray[divArray.length]=divs[i]; } } for (var i in divA...

Convert many Sinc and Cos to one Cos ?
I have say A1 cos(x+a1) + A2 cos(x+a2) + ... + B1 sin(x+b1) + ... I want to express the answer in the form to A cos (x + P ) Thanks. In article <c35ec8e4-e35f-45c8-b7f7-241c336a8e5b@f63g2000hsf.googlegroups.com>, mike <rotchm@gmail.com> wrote: >I have say A1 cos(x+a1) + A2 cos(x+a2) + ... + B1 sin(x+b1) + ... >I want to express the answer in the form to A cos (x + P ) The general case only has the trivial solution, A = anything non-zero P = -x + arccos(the left hand side / A) Are there restrictions on the variables such that you have reason to e...

cos fuction expression convert
Hello there. I would like to convert the expression: x(t)=a cos^2 (2 pi b t) into this xt = a*cos^2(2*pi*b*t) but every time i do this i get an error "Error: Unbalanced or unexpected parenthesis or bracket." Is something that is wrong in the expression.. Many thanx C. Mysirlidis Dear Charalampos, > xt = a*cos^2(2*pi*b*t) > but every time i do this i get an error "Error: Unbalanced or unexpected parenthesis or bracket." > > Is something that is wrong in the expression.. Yes. Of course. Just read the error message. xt ...

Finite $_POST loops infinitely
I have no idea why this is happening and I need someone to explain this to me at the simplest level absolutely possible (pretend I'm a 10-year old and explain it that way, please!) This class method: PHP Code: /** * Perform an array scan * * @access private * @param array $array * @see vname */ function &array_scan(&$array) { if (is_array($array) && @sizeof($array) > 0) { print_r("sizeof(" . vname($array) . ") = " . sizeof($array) . "<P>"); $index = 1; foreach ($arr...

series of commands by visa
Hi all.&nbsp; I am trying to control a Corvus microscope stage controller and have had&nbsp;luck in sending individual commands to do what ever I want the controller to do.&nbsp; I want the controller however, &nbsp;to move the stage in a scanning motion and to perform this I have to send multiple commands in the proper order.&nbsp; I have set up my VI a couple of different ways and have got the same results.&nbsp; The run starts out fine but then the controller must not be able to keep up with the commands from the computer as the stage acts erratically and stops.&...

On Designing a Feedback Time-Series Neural Network for Operational Deployment
% To obtain a time-series feedback net for operational deployment, % save % % 1. The CLOSELOOP net % 2. The final contents of the delay buffer Xf, Af obtained from % either openloop training % [ net tr Ys Es Xsf Asf ] = train(net, Xs,Ts,Xi,Ai); % or closeloop training % [ netc tr Ycs Ecs Xcsf Acsf ] = train(netc, Xcs,Tcs,Xci,Aci); % Further advice % % 1. NN design is a trial and error process because, % a. default trn/val/tst data division is random % b. default weight/bias initialization is random % c. default input-delay, feedback-delay a...

solutions manual, test bank for PHP Programming with MySQL The Web Technologies Series 2e 2/E 2nd Edition by Don Gosselin Diana Kokoska Robert Easterbrooks contact directly at studentshelp(at)hotmail(
PHP Programming with MySQL The Web Technologies Series 2e 2/E 2nd Edition by Don Gosselin Diana Kokoska Robert Easterbrooks solutions manual and test bank available at best price contact at studentshelp(at)hotmail(dot)com PHP Programming with MySQL The Web Technologies Series 2e 2/E 2nd Edition by Don Gosselin Diana Kokoska Robert Easterbrooks ...

Free Hobowars Infinite Wake Mod.
Hobowars infinite wake mod, This mod gives you an infinite supply of wake, to where you will never get tired. This is a very rare mod, once in a lifetime, undetectable, and untraceable. You can train for hours without ever stopping. Password and Username is needed to install the mod into your account. NOTE: It may take some time to get the mod installed, I have to do it manually. Email me at GodModder@gmail.com Hobo Information: Name:XXXXX (0000000) Gang: None Gender: Male Level: 1 Exp: 0/100 Life: 20/20 Dollars: $156,449,059 Cans: 0 Food: 0/10 - Check out food list Married to: Nobody [Propos...

Computer Control over Leviton Green Series Components
Has anyone experience of controlling devices in the Leviton Green series? This home uses wall switches such as dimming wall switch controllers HXC7S-1TW with dimming wall switch receivers HXM06-1TW to set scenes. Monitoring the power line X10 signals using an ACT monitor I see power line signals such as: K[1]012636 for scene 1 and K[1]01BE36 for OFF and when I use ACT's free Logger Interface software with one of their computer interface controller I can enter those same strings to correctly generate the appropriate scene commands. I have two problems: first I would like to control the i...

Problem with sin/cos of complex signal
First, a disclaimer: 1: Yes, I'm a student 2: Yes, this is homework 3: Yes, I've been struggling with this for a long time, and could use an extra pair of eyes 3.5: Yes, I'm sure the mistake is something stupid I've done, feel free to tell me so. The assignment is not hard. Take a complex signal, graph the real and imaginary portions, and then calculate a few sin's and cosine's to show that the real signal is actually the cosine and the imaginary is really the sin Here's my code (at the end of the post). You'll see if you run it that the signal appears to gra...

Forecasting time series with AR model
Hi everyone! I am new to matlab and time series processing... Could somebody help me with the questions below? I am using an AR model to forecast some data in a time series. First I fit the model with AR() function, and then, I try to forecast the next value (1-step forecast) with Predict() function. Having 100 data, I use first half to fit and second to forecast. The points is that Predict() should show one more data, like data number 101, but it end at 100, so where is the 1-step forecast? (I think that for the same reason, using 4-steps forecast, Predict function should show four more...

Asics Gel series and Asics Mexico series #3
<u><a href=http://www.asicstigersgels.com>Asics</a></u> placed among the ranks of the set's fifth-lagest sporting brand. <a href=http://www.asicstigersgels.com/products_all.html>Asics Tigers</a> became the customary brand nearby professional athletes and sports enthusiasts about the humankind in 2002.All these honors of Asics match shoes are because of <a href=http://www.asicstigersgels.com/specials.html>Asics shoes</a>'super technology and perfect design. The most people like Asics Gel series and Asics Mexico series, Asics Kayano ...

Get selected Time Series from a FINTS Object
Hello, usually you can receive a time series from a FINTS object bei name e.g. timeseries.series1 Is there a solution to select more than one series? I would like to have a possibility to do something like this: selectedSeries = [2 5 10 19] timeseries.{selectedSeries} Instead of numbers I could als use the names: selectedSeries = {'series1' 'series5' 'series10' 'series19'} I have not found a solution yet. I hope you can help me :) Thank you! Bye Moritz ...

How do I append a series of numbers from...to?
I have a table which holds certification numbers that come in batches. The numbers will run from (for example) 456 to 496. I have to enter these numbers individually. Is there a code I could use to append from 456 to 496 to the table? I'd like a pop up form that prompts for the number from and the number to. Robert Waggoner wrote: > I have a table which holds certification numbers that come in batches. The > numbers will run from (for example) 456 to 496. I have to enter these > numbers individually. Is there a code I could use to append from 456 to 496 > to the table?...

COS and indirect references
Hi. I am not very experienced in the PDF file format, so please bear with me, if it is a stupid question. I am a bit confused about the COS level and indirect references. I just want to know what type of COS object an indirect reference is, since the syntax of a such reference(for instance: Parent 4 0 R) does not comply(or does it?) to any of the COS level types as defined in the PDF specification. AFAIK, an indirect reference is the only exception where the syntax does not comply to any of the data types defined; boolean, number, string, name, array, dictionary, stream and null. It is ...

[JPL+semweb] any news on the infinite recursive loops ?
Hello Paul (and jan), I was wondering if you had had the time to explore this problem on which I've worked for days and whose solution seems, nows, pretty ugly ? Cheers and for all the reader, happy new year (this is the last day for that, no ? Djam� On 2005-01-31, Djam Seddah <djame@jamais-de-la-vie.com> wrote: > Hello Paul (and jan), > I was wondering if you had had the time to explore this problem on which > I've worked for days and whose solution seems, nows, pretty ugly ? Sofar I do not know about a problem, but I'd suggest using mail for discussing th...

Double integration on infinite rectangles
Hi all, is there any way to calculate a double integral on an infinite rectangle? As an example, I would like to calculate something like integrand = @(x,y)exp(-x.*y); quad2d(integrand,0,Inf,1,2) whose solution is log(2), but quad2d only accepts finite arguments and I have to use an approximation like integrand = @(x,y)exp(-x.*y); quad2d(integrand,0,1000,1,2) I also tried to use quadgk f=@(y)(quadgk(@(x)exp(-y.*x),0,Inf)); quadgk(@(y)f(y),1,2) but it doesn't work. Any suggestion? Thanks in advance. "antonio di cesare >" <dicesare@vodafone...

Mapping a series of Dates to an array of Number
Hello. Got a problem here. Ive got a set of points tht id be plotting. Those points would contain the date on which the work was done against its frequency. Supposedly if i did something on the 28th of March one of the points would be (28, respective freq). The next time i did my work on the 1st of April. So my next point would (1,freq). Now how could i map all the dates to another array so that when im plotting them i can plot the points as (day 1, freq), (day2,freq), (day4 , freq) and so on rather than having points like (28, freq), (29,freq), (1,freq) and blah blah blah. Id be needing s...

Cos[a x - b] displayed as Cos[b
Dear all, I have an expression which I wish to put it in the form of Cos[a x - b]. However when I input it to Mathematica, it automatically converted to Cos[b - a x]. Similarly, for Sin[a x - b] it would become -Sin[b - ax]. I understand that both are equivalent, but I just wish to have the form of Cos[a x - b]. Is there any way to do this? Thank you very much. Best regards, Ho HO JEE HOU wrote: > Dear all, > > I have an expression which I wish to put it in the form of Cos[a x - b]. > However when I input it to Mathematica, it automatically converted to &g...