Hii , every body .. how are you ? -
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.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)
.
,
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
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
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)));
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: - 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!
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 ,,
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?
actually am still confuse !
but if i think in that way .. e = 2.718
so do i have say (should x be >1)!!
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....
e^x= 1+ x/1!+x^2/2!+ …….+x^n/n!
-----
THAT LAST THING MY MIND FOUND ! -
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;
-
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?
-
-
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
sorry i posted my last comment w/o reading your last post
yeah i understand now , because the n which is increase
one each step !
so i created method for add 1 for each n ! -
public static double sum(double n)
-
{
-
if(n==0)
-
return 1;
-
else if(n>0)
-
return sum(n+1);
-
else
-
return -1;
-
}
-
-
-
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 >.<"
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!
removed due to giving an answer improperly. Self edit.
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 !!
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 ^^"
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |