Prev: I PAY WHO CAN SOLVE MY Final Year Project until 24/03/2010. London Only please
Next: retrieving pointer data from a struct causes segfault
From: Brittany Morgante on 21 Mar 2010 20:13 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.
From: Jan Simon on 21 Mar 2010 20:34 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
From: Brittany Morgante on 21 Mar 2010 20:45 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <ho6dtr$koa$1(a)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
From: Walter Roberson on 21 Mar 2010 20:40 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.
From: Jan Simon on 21 Mar 2010 21:45
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 |