problem in my algorithm...

G

[G.P]Georgy™

Hey guys !
I have to solve this following question:

Determine the value of e(x), where:
e(x) = 1 + x/1! + x²/2! + x³/3! + …
Consider the terms of the sum until the difference between the values
obtained
with addiction of two consecutive terms be inferior to 0,0001.


What I succeed was this code:

public class Factorial {

static int fact(int n){
int f = 1, i = 1;
while (i < n){
i = i + 1;
f = f * i;
}
return f;
}

public static void main (String args[]){

float difference = 1;

//this variable will acumulate the sum until (difference <
0.0001).
//starts with 1, the first term of the sum.
float e = 1;

int x = 8;

while(difference < 0.0001){
//(????)
//I don't know how can I calculate this difference; what
terms I use to do this difference?
}

}
}


any idea for me??

thank you !
 
F

Fred

Hey guys !
I have to solve this following question:

Determine the value of e(x), where:
e(x) = 1 + x/1! + x²/2! + x³/3! + …
Consider the terms of the sum until the difference between the values
obtained
with addiction of two consecutive terms be inferior to 0,0001.

What I succeed was this code:

public class Factorial {

    static int fact(int n){
        int f = 1, i = 1;
        while (i < n){
            i = i + 1;
            f = f * i;
        }
        return f;
    }

    public static void main (String args[]){

        float difference = 1;

        //this variable will acumulate the sum until (difference <
0.0001).
        //starts with 1, the first term of the sum.
        float e = 1;

        int x = 8;

        while(difference < 0.0001){
            //(????)
            //I don't know how can I calculate this difference; what
terms I use to do this difference?
        }

    }

}

any idea for me??

thank you !

You don't want to keep computing factorials.
You want something like this:

sum=1.;
n=1;
b=x;
a = 0;
while ( fabs(a-b) > tolerance ) {
a = b;
sum += a;
b = (a * x) / ++n;
}
 
G

[G.P]Georgy™

Hey Fred and Rossum!
I think I got what you are explaining to me:

/** Calculates factorial */
static int fact(int n) {
int result = 1;
while (n > 1) {
result *= n--;
}
return result;
}

/** Calculates e(x) */
static double e(int x) {
final double delta = 0.0001;
double result = 1.0;
double a = 1.0;
double b = 1.0;
double difference = 1.0;
int i = 1;

while (difference > delta) {
b = Math.pow(x, i)/fact(i);
result += b;

difference = b - a;

a = b;
i++;
}

return result;
}

I think it's solved.
But...
I'm still confused in the following part of question: "with addiction
of two consecutive terms".
where is this addiction???


Thanks for replies!
Greetings from Brazil.
 
M

Mark Space

[G.P]Georgy™ said:
I'm still confused in the following part of question: "with addiction
of two consecutive terms".
where is this addiction???

"Determine the value of e(x), where:
e(x) = 1 + x/1! + x²/2! + x³/3! + …
Consider the terms of the sum until the difference between the values
obtained..."

See the plus signs up there? That's the addition. Each item involving
an X there is called a "term" in algebra.
 
L

Lew

[G.P]Georgyâ„¢ said:
Hey Fred and Rossum!
I think I got what you are explaining to me:

/** Calculates factorial */
static int fact(int n) {
int result = 1;
while (n > 1) {
result *= n--;
}
return result;
}

/** Calculates e(x) */
static double e(int x) {
final double delta = 0.0001;
double result = 1.0;
double a = 1.0;
double b = 1.0;
double difference = 1.0;
int i = 1;

while (difference > delta) {
b = Math.pow(x, i)/fact(i);
result += b;

difference = b - a;

a = b;
i++;
}

return result;
}

I think it's solved.

Fred's version will be much faster and kinder to the stack.

Note that Fred omitted type declarations from his snippet:
You want something like this:

I'd go with
<snippet>
private static final double TOLERANCE = 0.0001;

public final double ePower( double x )
{
return ePower( x, TOLERANCE );
}

public final double ePower( double x, double tolerance )
{
if ( tolerance <= Double.MIN_NORMAL )
{
tolerance = TOLERANCE;
}
double sum = 1.;
long n = 1L;
for ( double a = 0.0, b = x; Math.abs( a - b ) > tolerance; )
{
a = b;
sum += a;
b = (a * x) / ++n;
}
return sum;
}
I'm still confused in the following part of question: "with addiction
of two consecutive terms".
where is this addiction???

"Addition" - see Mark Space's answer.
 
G

[G.P]Georgy™

Ok now!!
Everything understood about the "addition",
and the best performance of the program...

Thanks a lot to who helped me!

--
Georgy Passos

[G.P]Georgy™ said:
Hey Fred and Rossum!
I think I got what you are explaining to me:
        /** Calculates factorial */
   static int fact(int n) {
       int result = 1;
       while (n > 1) {
           result *= n--;
       }
       return result;
   }
     /** Calculates e(x) */
     static double e(int x) {
       final double delta = 0.0001;
       double result = 1.0;
       double a = 1.0;
            double b = 1.0;
       double difference = 1.0;
       int i = 1;
       while (difference > delta) {
           b = Math.pow(x, i)/fact(i);
           result += b;
           difference = b - a;
           a = b;
           i++;
       }
       return result;
     }
I think it's solved.

Fred's version will be much faster and kinder to the stack.

Note that Fred omitted type declarations from his snippet:
You want something like this:

I'd go with
<snippet>
  private static final double TOLERANCE = 0.0001;

  public final double ePower( double x )
  {
    return ePower( x, TOLERANCE );
  }

  public final double ePower( double x, double tolerance )
  {
   if ( tolerance <= Double.MIN_NORMAL )
   {
    tolerance = TOLERANCE;
   }
   double sum = 1.;
   long n = 1L;
   for ( double a = 0.0, b = x; Math.abs( a - b ) > tolerance; )
   {
     a = b;
     sum += a;
     b = (a * x) / ++n;
   }
   return sum;
  }
I'm still confused in the following part of question: "with addiction
of two consecutive terms".
where is this addiction???

"Addition" - see Mark Space's answer.
 
R

Roedy Green

Everything understood about the "addition",
and the best performance of the program...

One more hint to problem of this sort. When you have a large number of
things to add up, the result will be more accurate if you start with
the smallest and work toward the biggest.
--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
V

vj

I see a problem in freds algorithm for x < 0.

As evident that for x<0 terms will alternate between +ve and -ve
values. So its quite quite easy to see two terms T(i),T(i+1) such
that T(i)+T(i+1) < Tolerence but FABS(T(i)-T(i+1)) >Tolerence.

Since the basic requirement is that iteration should continue untill
SUM of TWO Consicutive terms (T + T[i+1]) is inferior(less) then
0.0001 hence freds algorithm would continue for some extra iterations
before concluding.

So i suggest that the loop predicate "fabs(a-b) > tolerance" be
changed to "a+b>tolerance".

Kindly correct me if i am wrong,

VJ
 
J

John B. Matthews

vj said:
I see a problem in freds algorithm for x < 0.

Also for positive x that result in a == b.
As evident that for x<0 terms will alternate between +ve and -ve
values.

Interesting. For x > 0, the sum advances smoothly toward the correct
value from below; for x < 0, the increment alternates between plus and
minus as the new term is even or odd, respectively. There's an animated
GIF showing this effect, here:

So its quite quite easy to see two terms T(i),T(i+1) such
that T(i)+T(i+1) < Tolerence but FABS(T(i)-T(i+1)) >Tolerence.

Since the basic requirement is that iteration should continue untill
SUM of TWO Consicutive terms (T + T[i+1]) is inferior(less) then
0.0001 hence freds algorithm would continue for some extra iterations
before concluding.

So i suggest that the loop predicate "fabs(a-b) > tolerance" be
changed to "a+b>tolerance".

Kindly correct me if i am wrong,


I'm not sure. The series converges, so I'd use Math.abs(b) > tolerance:

<http://en.wikipedia.org/wiki/Characterizations_of_the_exponential_functi
on>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top