Click to See Complete Forum and Search --> : Loops to compute given formular
hugo84
September 8th, 2009, 10:19 AM
Hi does anyone have an idea how should i apply using loops to compute the following formular in my program? Thanks in advance
x-x^2/2+x^4/4 -x^6/6 + .......... -x^20/20
^ refers to Power of
eg: x^2 is x²
holestary
September 8th, 2009, 11:42 AM
hi,
maybe this can give u an idea..
for(double k=2;k<=20;k=k+2)
{
z=Math.pow(x,k)/k;
if(m%2!=0)
{
x=x-z;
m++;
}
else
{
x=x+z;
m++;
}
}
hugo84
September 8th, 2009, 12:36 PM
Hi holestary thanks for your quick reply again could you explain to me this part of the code? what am i supposed to declare for m?
if(m%2!=0)
{
x=x-z;
m++;
}
else
{
x=x+z;
m++;
}
holestary
September 8th, 2009, 12:55 PM
Hi holestary thanks for your quick reply again could you explain to me this part of the code? what am i supposed to declare for m?
if(m%2!=0)
{
x=x-z;
m++;
}
else
{
x=x+z;
m++;
}
while loop is working..first, it calculates z
then it decides..addition or subtraction..we control this by if-else..declare m=1 out of loop..
m%2!=0 ===>substraction
m%2==0 ===>addition
then m=m+1..
i am not good at english..i tell you as i can..
holestary
September 8th, 2009, 01:00 PM
if(m%2!=0)
x=x-z;
else
x=x+z;
m++;
it is the same..
jcaccia
September 8th, 2009, 04:53 PM
You change the value of x in every step of the loop, while it should remain constant. A better solution is:
double pow = 1.0;
double sum = x;
double sqr = x * x;
for (double k = 2.0; k <= 20.0; k += 2.0) {
pow = -pow * sqr;
sum += pow / k;
}
hugo84
September 9th, 2009, 03:41 AM
while loop is working..first, it calculates z
then it decides..addition or subtraction..we control this by if-else..declare m=1 out of loop..
m%2!=0 ===>substraction
m%2==0 ===>addition
then m=m+1..
i am not good at english..i tell you as i can..
Yeah basically i need to compute x- x^3/3 +x^5/5 -x^7/7 +x^9/9 -x^11/11 +x^13/13 -x^15/15 +x^17/17 -x^19/19 which would be = 0.729815 when x=0.9.
I am quite confused with the alternate subtraction and addition of the formular. I guess theres some thing wrong there, that's why i am not getting the correct result?
double z=0, x=0.9,m=1;
for(double k=3;k<=20;k=k+2)
{
z=Math.pow(x,k)/k;
// System.out.println(z);
if(m%2!=0)
{
x=x-z;
m++;
}
else
{
x=x+z;
m++;
}
}
System.out.println(x);
}}
jcaccia
September 9th, 2009, 04:57 AM
Hugo84, have you seen my post (#6). When you do this:
x=x+z;
// or
x=x-z;
you are actually changing the value of x, which should remain constant all the time. That is why you are getting the wrong results.
Now for this new formula with odd exponents the correct code is:
double pow = x;
double sum = x;
double sqr = x * x;
for (double k = 3.0; k <= 19.0; k += 2.0) {
pow = -pow * sqr;
sum += pow / k;
}
If you run this for x = 0.9 the result (sum) is 0.7298155.
hugo84
September 9th, 2009, 07:17 AM
Hugo84, have you seen my post (#6). When you do this:
x=x+z;
// or
x=x-z;
you are actually changing the value of x, which should remain constant all the time. That is why you are getting the wrong results.
Now for this new formula with odd exponents the correct code is:
double pow = x;
double sum = x;
double sqr = x * x;
for (double k = 3.0; k <= 19.0; k += 2.0) {
pow = -pow * sqr;
sum += pow / k;
}
If you run this for x = 0.9 the result (sum) is 0.7298155.
Ohhh. Sorry on missing out on that as i thought it would not be an issue. Thanks for the help and clarification.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.