473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Expontional Function in java and the number of vowels

10
Hii , every body .. how are you ?

Expand|Select|Wrap|Line Numbers
  1.     public static double CalculateTheExp(double x , double n)
  2.     {
  3.         double sum = 0;
  4.         x = CalculatPower(2 , 5 );
  5.         n = CalculatFact(5);
  6.         if(x==0)
  7.             return 1 ;
  8.         else if (x>0)
  9.         {
  10.             return x+(x/(CalculateTheExp(x,n)));
  11.         }
  12.         else
  13.         System.out.println("WRONG CHOICE!");
  14.         return -1;
  15.     }
  16.  
  17.     // calculate factorial.
  18.     public static double CalculatFact(double n )
  19.     {
  20.      if(n == 0)
  21.         return 1;
  22.     else
  23.       return n * CalculatFact(n-1);
  24.     }
  25.  
  26.     // calculate Power.
  27.     public static double CalculatPower(double n , double x )
  28.     {
  29.             if(n==0) // base case
  30.             return 1;
  31.         else if(n>0) // recursive case
  32.             return x*CalculatPower(n-1,x);
  33.                 else
  34.                     return -1;
  35.     }
  36.  
  37.  
  38.  
  39.  

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.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)
at labassigmnetsix.Main.CalculateTheExp(Main.java:34)


.
,
Mar 29 '11 #1

✓ answered by Oralloy

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!

16 2049
Oralloy
985 Expert 512MB
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
Mar 29 '11 #2
YellowJ
10
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/(CalculateTheExp(x,n)));
Mar 29 '11 #3
Oralloy
985 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. 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!
Mar 29 '11 #4
YellowJ
10
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/(CalculateTheExp(x,n)));

I'll try it now ,,
Mar 29 '11 #5
Oralloy
985 Expert 512MB
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?
Mar 29 '11 #6
YellowJ
10
actually am still confuse !
but if i think in that way .. e = 2.718
so do i have say (should x be >1)!!
Mar 29 '11 #7
Oralloy
985 Expert 512MB
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....
Mar 29 '11 #8
YellowJ
10
e^x=1+ x/1!+x^2/2!+ …….+x^n/n!
-----

THAT LAST THING MY MIND FOUND !
Expand|Select|Wrap|Line Numbers
  1. Sum = 0;
  2. Num = 0;
  3. if(x==0)
  4. return one ; 
  5. else if(x>0)
  6. Num = CalculatPower(x-1,n)+CalculateTheExp(CalculatPower(x,n),(CalculatPower(x,n)/(CalculatFact(n))));
  7. Sum = Sum + Num;
  8. return sum ; 
  9. else 
  10. return 
  11. -1; 
  12.  
Mar 30 '11 #9
Oralloy
985 Expert 512MB
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?
Mar 30 '11 #10
YellowJ
10
Expand|Select|Wrap|Line Numbers
  1.  
  2.         if(x==0)
  3.             return 1 ;
  4.         else if (x>0)
  5.             return sum+=CalculateTheExp (CalculatPower(x,n),CalculatPower(x,n-1)/(CalculatFact(n-1)));
  6.         else
  7.         return sum;
  8.     }
  9.  
  10.  

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
Mar 30 '11 #11
YellowJ
10
sorry i posted my last comment w/o reading your last post
Mar 30 '11 #12
YellowJ
10
yeah i understand now , because the n which is increase
one each step !

so i created method for add 1 for each n !
Expand|Select|Wrap|Line Numbers
  1.         public static double sum(double n)
  2.     {
  3.          if(n==0)
  4.              return 1;
  5.          else if(n>0)
  6.              return sum(n+1);
  7.          else 
  8.              return -1;
  9.     }
  10.  
  11.  
Expand|Select|Wrap|Line Numbers
  1.     public static double CalculateTheExp(double x , double n)
  2.     {
  3.         double sum=0;
  4.         double num =0;
  5.         if(x==0)
  6.             return 1 ;
  7.         else if (x>0)
  8.             return num=CalculateTheExp (1,(CalculatPower(x,sum(n)))/(CalculatFact(sum(n))));
  9.         else
  10.         return sum = sum+num;
  11.     }
  12.  
  13.  
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 >.<"
Mar 30 '11 #13
Oralloy
985 Expert 512MB
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!
Mar 30 '11 #14
Oralloy
985 Expert 512MB
removed due to giving an answer improperly. Self edit.
Mar 30 '11 #15
YellowJ
10


i think i got the idea from the graph !
so i have write a method for n is 1 the x will reach
the base case to stop which is x==0! and then i do the exponential term.
I'll try what i understand ..

i tried to post this comment five times !! but it gave me an error !!
Mar 30 '11 #16
YellowJ
10
Wow ! impossible! it's worked !! i think I'll love java soon ^^

thaaaaaannnk you very much Oralloy you are best adviser
thank you to help me out ^^"
Mar 30 '11 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lawrence | last post by:
Right now, over at www.monkeyclaus.org, the following script is getting to the 9th run through and dying after the line: echo "..."; You'll admit that is a strange place to die. I've hit...
1
by: edward hage | last post by:
Hello, I have a Linux machine and I use Netscape 7.1 which I upgraded from Mozilla. I downloaded j2sdj-1.4.2_02 and I have installed the plugins as followes: in .mozilla/plugins- directory a...
7
by: Paul Taylor | last post by:
I want to show a figure based on an numeric entry that more than 20 (or 21and higher). If Number value is 21 or higher then the amount returned is Number value * 120 (£) * 10 (%) to give a...
7
by: Melissa | last post by:
I'm trying to create a function that I can put in a query field that will consecutively number the records returned by the query starting at 1 and will start at 1 each time the query is run. So far...
2
by: jr | last post by:
I have a niggle with the Switch function I have a querey which has a column with 3 digit values of which there are about 20 which are unique. These are meaningless to the user and so using...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
5
by: glegipon | last post by:
As Tom Cahill would require, my challenge is (according to the "three R's") Reproducible: if run on OS 9.x, the monthly payment does not show up in the text box: Recognizable: I believe the...
1
by: marty.gagnon | last post by:
I have a xsl file that references a xml file using the document() function. I'm having trouble specifying the URI in the document() function of a xls stylesheet. I'm using java to transform the...
1
by: PAF | last post by:
Hi, I'm trying to evaluate a sum expression trougth .NET 2 System.Xml.XPath.XPathNavigator object. My code is : Dim document As New System.Xml.XmlDocument() document.LoadXml("<?xml...
4
by: Yonih | last post by:
So I am trying to get this Calculator to work. It needs to take in a vaule, and select a shipping Everythin works great except the shipping part. I need it to take the shipping value and add it to...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.