Expontional Function in java and the number of vowels

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YellowJ
    New Member
    • Mar 2011
    • 10

    Expontional Function in java and the number of vowels

    Hii , every body .. how are you ?

    Code:
        public static double CalculateTheExp(double x , double n)
        {
            double sum = 0;
            x = CalculatPower(2 , 5 );
            n = CalculatFact(5);
            if(x==0)
                return 1 ;
            else if (x>0)
            {
                return x+(x/(CalculateTheExp(x,n)));
            }
            else
            System.out.println("WRONG CHOICE!");
            return -1;
        }
    
        // calculate factorial.
        public static double CalculatFact(double n )
        {
         if(n == 0)
            return 1;
        else
          return n * CalculatFact(n-1);
        }
    
        // calculate Power.
        public static double CalculatPower(double n , double x )
        {
                if(n==0) // base case
                return 1;
            else if(n>0) // recursive case
                return x*CalculatPower(n-1,x);
                    else
                        return -1;
        }

    I WANNA FOUND e^x when e^x = 1+(x/1!)+(x^2)/2!+ ... x^n/n!

    power and factorial methods worked ! but the exponential it does not work at all !!

    the error !

    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)
    at labassigmnetsix .Main.Calculate TheExp(Main.jav a:34)


    .
    ,
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    First off, these look like homework questions. So, if you have specific technique questions or whatever, then we'll answer them for you. We are not, however, in the business of doing folks homework. That's your job.

    So, in your mathematic recursion problem, the error is in lines 4 and 5 of the code you posted.

    As for your second problem, you have code that I'm not sure will even compile. Are you clear on what you're trying to achieve?

    Luck!
    Oralloy

    Comment

    • YellowJ
      New Member
      • Mar 2011
      • 10

      #3
      the second one i already solved by myself and it's worked ! so forget it and i'll removed now!

      but the first one even if it's homework , i did not
      say i need someone to solve it to me i almost solved it
      but .. i have problem with exp. function !!
      here where i got the error & and i know i made
      a mistake .. but i do not know what it is !!
      return x+(x/(CalculateTheEx p(x,n)));

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        YellowJ,

        The problem is that you are overloading x and n.

        Look very carefully at lines 4 and 5 for a moment.

        Then, if you still don't see it, change the function prototype to this:
        Code:
        public static double CalculateTheExp(final double x , final double n)
        I'm trying to help here, without throwing the answer at you.

        Remember that your calculation must promote the power series and factorial series in parallel. Ponder that, and write down what a few phases of the recursion should look like. Once you do that, I think you'll have a better idea of how to proceed.

        If need be, step very carefully through each step of the exponental recursion. I think you'll find the error very quickly on the first pass.

        Luck!

        Comment

        • YellowJ
          New Member
          • Mar 2011
          • 10

          #5
          yup! but i do not need to change any thing in fact & pow methods ? right !

          .

          exp function !
          basic case (if x = 0) --> return one and stop !
          but if (x+1) .. 1 .. , return (x-1)+(x^n/n!) ! am a right !! because each index will call the previous one
          to get the answer ! is that what do you mean !!

          do i have change my code to :

          return x-1+(x/(CalculateTheEx p(x,n)));

          I'll try it now ,,

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Good, you realized that you are clobbering the values of x and n. That alone renders your exponential algorithm an infinite loop.

            You recognize the recurance term, yes?

            Start simple, don't recurse. How is your exponental function initially called? What value should the first iteration of your function compute?

            Then, take it only one step deeper. How is the function called, and what should it return?

            Does that help?

            Comment

            • YellowJ
              New Member
              • Mar 2011
              • 10

              #7
              actually am still confuse !
              but if i think in that way .. e = 2.718
              so do i have say (should x be >1)!!

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                Sorry, my spelling error - it should be "recurrence term"

                What is the general term in the sum used to calculate the exponental?

                Start from there....

                Comment

                • YellowJ
                  New Member
                  • Mar 2011
                  • 10

                  #9
                  e^x=1+ x/1!+x^2/2!+ …….+x^n/n!
                  -----

                  THAT LAST THING MY MIND FOUND !
                  Code:
                  Sum = 0;
                  Num = 0;
                  if(x==0)
                  return one ; 
                  else if(x>0)
                  Num = CalculatPower(x-1,n)+CalculateTheExp(CalculatPower(x,n),(CalculatPower(x,n)/(CalculatFact(n))));
                  Sum = Sum + Num;
                  return sum ; 
                  else 
                  return 
                  -1;

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    Actually that's one direct approach to the problem, but the recursive term is wrong. I never considered propagating the one from the bottom of the recursion. That is clever. Kudos!

                    I'm not certain if you observe that:
                    1 = x^0/0!

                    Still, if you leave the first term out, or make it a special case, you have the general term in the series, the recurrence term, which is:
                    x^n/n!

                    Which, using your code, you can easily calculate, correct?
                    x^n/n! = CalcPow(x, n) / CalcFact(n)

                    The trick with recursion is to use each iteration to calculate one term (or other logical segment) of the problem.

                    Where this problem is possibly fooling you is that you have several conditions that you are trying to look at in the formula:
                    e^x = 1 + x/1! + x^2/2! + x^3/3! + ...

                    Breaking down,
                    term(0) = 1
                    and
                    term(1) = x/1!
                    and
                    term(n) = x^n/n!

                    Now, if you let
                    sum(n) = sum(n..infinity , term(n))
                    Which is to say
                    sum(n) = x^n/n! + x^(n+1)/(n+1)! + x^(n+2)/(n+2)! + ...
                    And, specifically,
                    sum(2) = x^2/2! + x^3/3! + x^4/4! + ...
                    Then
                    e^x = 1 + x/1! + sum(2)
                    And
                    e^x = 1 + x/1! + x^2/2! + sum(3)

                    I know it's a little long winded. Does that help?

                    Comment

                    • YellowJ
                      New Member
                      • Mar 2011
                      • 10

                      #11
                      Code:
                              if(x==0)
                                  return 1 ;
                              else if (x>0)
                                  return sum+=CalculateTheExp (CalculatPower(x,n),CalculatPower(x,n-1)/(CalculatFact(n-1)));
                              else
                              return sum;
                          }

                      I study the case :
                      tell me if am wrong !
                      base case is when x=0 , return 1 .

                      but look !
                      power has its own base case to stop ?
                      and factorial also it has its own base case to stop ?
                      how i can mange this

                      Comment

                      • YellowJ
                        New Member
                        • Mar 2011
                        • 10

                        #12
                        sorry i posted my last comment w/o reading your last post

                        Comment

                        • YellowJ
                          New Member
                          • Mar 2011
                          • 10

                          #13
                          yeah i understand now , because the n which is increase
                          one each step !

                          so i created method for add 1 for each n !
                          Code:
                                  public static double sum(double n)
                              {
                                   if(n==0)
                                       return 1;
                                   else if(n>0)
                                       return sum(n+1);
                                   else 
                                       return -1;
                              }
                          Code:
                              public static double CalculateTheExp(double x , double n)
                              {
                                  double sum=0;
                                  double num =0;
                                  if(x==0)
                                      return 1 ;
                                  else if (x>0)
                                      return num=CalculateTheExp (1,(CalculatPower(x,sum(n)))/(CalculatFact(sum(n))));
                                  else
                                  return sum = sum+num;
                              }
                          but wait if sum(n) = add all series together, so i think i don't have
                          to use it ,, why i can not +1 directly in the parameter.
                          I don't understand the error !! i think am gonna give up >.<"

                          Comment

                          • Oralloy
                            Recognized Expert Contributor
                            • Jun 2010
                            • 988

                            #14
                            YellowJ,

                            No worries.

                            As for the terminating condition, each term of the calculation is computed as follows:
                            term(n) = x^n/n!

                            Has your instructor done any analysis of the terms of the formula for the exponental?

                            In a nutshell, for any x, let
                            term(n) = f(n)/g(n)
                            where
                            f(n) = x^n
                            and
                            g(n) = n!

                            So, we want to know what happens to term(n) as n goes to infinity.

                            Let's start with f(n) for the easy case:
                            For -1<x<1, f(n) converges on zero as n aproaches infinity
                            next we'll take the positive one case:
                            For x=1, f(n)=1 for all n
                            and negative one:
                            For x=-1, f(n)=1 for n even, f(n)=-1 for n odd
                            and now the positive numbers greater than one:
                            for x>1, f(n)=x^n, increasing without bound as n approaches infinity
                            and now, the negative numbers less than negative-one:
                            for x<-1, f(n)=x^n, increasing without bound with alternating sign, as n approaches infinity

                            The trick is with g(n), which is simply analyzed as
                            g(n)=n!, increasing without bound as n approaches infinity

                            so, what happens to f(n)/g(n) as n goes to infinity? Well, it turns out that we can rearrange the equation for terms as follows:
                            term(n) = f(n)/g(n) = (x/1)*(x/2)*(x/3)*...*(x/n)
                            From which we can observe that the iteration number n will eventually swamp x and force term(n) to converge on zero.

                            Needless to say, once the term is below the floating point minimum of your machine, then it will be approximated by zero, and be inconsequential in the computation of the sum.

                            Luck!

                            Comment

                            • Oralloy
                              Recognized Expert Contributor
                              • Jun 2010
                              • 988

                              #15
                              removed due to giving an answer improperly. Self edit.
                              Last edited by Oralloy; Mar 30 '11, 03:52 PM. Reason: Posted answer in frustration, removed.

                              Comment

                              Working...