 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ding Lei Guest
|
Posted: Thu Jun 24, 2004 9:23 am Post subject: Need help on math computation with java |
|
|
Hello java group,
I need to use Java to compute the following forumla:
p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 + y)^n)
p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Thank you very much!
Yours,
layman
--
Yours,
<<<::::: D i n g L e i ::::::>>
| Quote: | | ||
| Ext: 8106 ||
| Email: <dinglei [A] ipanel [O] cn> ||
| Dept. Of Technology/Engineering ||
| Embedded Internet Solutions Inc. ||
| ||
(((((( ===================== ))))) |
|
|
| Back to top |
|
 |
gajo Guest
|
Posted: Thu Jun 24, 2004 11:03 am Post subject: Re: Need help on math computation with java |
|
|
"Ding Lei" <dinglei (AT) ipanel (DOT) cn> wrote
| Quote: | Hello java group,
I need to use Java to compute the following forumla:
p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 +
y)^n)
p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
|
Of course you can do anything with a computer, but I'm just curious if n
goes to infinity or not?
|
|
| Back to top |
|
 |
gajo Guest
|
Posted: Thu Jun 24, 2004 12:04 pm Post subject: Re: Need help on math computation with java |
|
|
"gajo" <gajo (AT) eunet (DOT) yu> wrote
| Quote: | "Ding Lei" <dinglei (AT) ipanel (DOT) cn> wrote in message
news:87eko5nwbu.fsf (AT) dinglei (DOT) ipanel.com.cn...
Hello java group,
I need to use Java to compute the following forumla:
p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 +
y)^n)
p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Of course you can do anything with a computer, but I'm just curious if n
goes to infinity or not?
|
If it does not go to infinity then I think your solution could be the
following:
p = c(1/(1+y)+...+1/((1+y)^n)) /* (1+y)^n
p*(1+y)^n = c((1+y)^n-1 + (1+y)^n-2 + ... + 1)
Now let's make a funkction
f(n) = (1+y)^n
That would make the formula above
p*f(n) = c(f(n-1) + f(n-2) + ... + f(1) + f(0))
Obviously, f(0) = (1+y)^0 = 1
Let's make A = c/p
Then f(n) = A(f(n-1) + f(n-2) + ... + f(0))
f(n) = Af(n-1) + Af(n-2) + ... + Af(0)
And now consider that f(0) = 1 and you have the following:
f(0) = 1
f(1) = Af(0) = A
f(2) = Af(1) + Af(0) = A^2 + A
....
f(n) = A^n-1 + A^n-2 + ... + A^2 + A
OK, so we have that f(n) = (1+y)^n
That is, (1+y)^n = A^n-1 + A^n-2 + ... + A
So, 1+y = n_root(A^n-1 + A^n-2 + ... + A)
Finally,
y = n_root(A^n-1 + A^n-2 + ... + A) - 1
Replace A with c/p and you get the solution.
HTH, Gajo
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Thu Jun 24, 2004 6:33 pm Post subject: Re: Need help on math computation with java |
|
|
On Thu, 24 Jun 2004 17:23:33 +0800, Ding Lei <dinglei (AT) ipanel (DOT) cn> wrote
or quoted :
| Quote: | Hello java group,
I need to use Java to compute the following forumla:
p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 + y)^n)
p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Thank you very much!
|
off the top of my head the code would go something like this:
double p = 0;
double yfactor = 1+y;
double ypow = yfactor;
for (int i=1; i
{
p += c/ypow;
ypow *= yfactor;
}
return p;
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Thu Jun 24, 2004 7:22 pm Post subject: Re: Need help on math computation with java |
|
|
Roedy Green wrote:
| Quote: | On Thu, 24 Jun 2004 17:23:33 +0800, Ding Lei <dinglei (AT) ipanel (DOT) cn> wrote
or quoted :
Hello java group,
I need to use Java to compute the following forumla:
p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 + y)^n)
p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Thank you very much!
off the top of my head the code would go something like this:
double p = 0;
double yfactor = 1+y;
double ypow = yfactor;
for (int i=1; i
{
p += c/ypow;
ypow *= yfactor;
}
return p;
|
Or if y is known to be non-zero:
p = c / (1. + y) * (Math.pow(1. + y, n) - 1.) / y;
Math.pow() may appear slow, but this may be preferable if
n is large (for a typical 30-year mortgage, n == 360).
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
gajo Guest
|
Posted: Thu Jun 24, 2004 8:01 pm Post subject: Re: Need help on math computation with java |
|
|
"Eric Sosman" <Eric.Sosman (AT) sun (DOT) com> wrote
| Quote: |
Or if y is known to be non-zero:
p = c / (1. + y) * (Math.pow(1. + y, n) - 1.) / y;
Math.pow() may appear slow, but this may be preferable if
n is large (for a typical 30-year mortgage, n == 360).
|
But he said he needs to calculate y, not p
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Thu Jun 24, 2004 9:02 pm Post subject: Re: Need help on math computation with java |
|
|
On Thu, 24 Jun 2004 22:01:25 +0200, "gajo" <gajo (AT) eunet (DOT) yu> wrote or
quoted :
| Quote: | But he said he needs to calculate y, not p
|
In that case he could do a binary search on p or use a numerical
method such as Newton Raphson to home in on a solution using the ways
of computing p from y. See
http://mindprod.com/jgloss/binarysearch.html
Java's binary search is not designed for numerical analysis, but it
gives you the idea.
This is third year university numerical analysis. You probably would
tackle it with Maple or Wolfram Mathematica today, or look in some
financial package for a fully canned solution.
The text I used back then was A First Course in Numerical Analysis
Anthony Ralston, Philip Rabinowitz
http://www.amazon.com/exec/obidos/ASIN/048641454X/canadianmindprod
From a practical point of view, the easiest way to solve it is to go
to your nearest drugstore and find a financial calculator that can do
it.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
gajo Guest
|
Posted: Thu Jun 24, 2004 9:34 pm Post subject: Re: Need help on math computation with java |
|
|
"Roedy Green" <look-on (AT) mindprod (DOT) com.invalid> wrote
| Quote: | From a practical point of view, the easiest way to solve it is to go
to your nearest drugstore and find a financial calculator that can do
it.
|
The formula is somehow familiar, perhaps when n goes to infinity the result
(p) will be something like c*sin(x) or something like that. I never like
calculus that much to memorize these kind of things. But if n doesn't go to
infinity then y can be calculated using simple combinatorics, as I explained
in some other post. I actually took a pen and paper and solved (I think
correctly) the problem, so in the end
y = n_root(A^n-1 + A^n-2 + ... + A) - 1, where A = c/p
This is so simple I'm wondering why did it even get posted on a Java forum,
when it has nothing to do with Java. This is pure maths...
Gajo
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Thu Jun 24, 2004 10:21 pm Post subject: Re: Need help on math computation with java |
|
|
gajo wrote:
| Quote: | "Eric Sosman" <Eric.Sosman (AT) sun (DOT) com> wrote in message
news:40DB2A04.6040802 (AT) sun (DOT) com...
Or if y is known to be non-zero:
p = c / (1. + y) * (Math.pow(1. + y, n) - 1.) / y;
Math.pow() may appear slow, but this may be preferable if
n is large (for a typical 30-year mortgage, n == 360).
But he said he needs to calculate y, not p
|
Woops! So he did, yes indeedy ...
It's easy to solve for p or c or n given the other
three values, but I can't see any way to solve for y
in closed form. Last time I worked this out (I was
buying a house and wanted to understand how the mortgage
calculations *really* worked), I recall trying Newton's
method and seeing it blow up rather badly; the derivative
got very small in the vicinity of the root. A lower-
order iteration was slow but got results; I think it
may have been
y1 = (1 - 1 / (y0 + 1)^n) * c / p
y2 = (1 - 1 / (y1 + 1)^n) * c / p
...
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Fri Jun 25, 2004 3:00 pm Post subject: Re: Need help on math computation with java |
|
|
Eric Sosman wrote:
| Quote: | gajo wrote:
"Eric Sosman" <Eric.Sosman (AT) sun (DOT) com> wrote in message
news:40DB2A04.6040802 (AT) sun (DOT) com...
Or if y is known to be non-zero:
p = c / (1. + y) * (Math.pow(1. + y, n) - 1.) / y;
Math.pow() may appear slow, but this may be preferable if
n is large (for a typical 30-year mortgage, n == 360).
|
Oh, fudge. You'd never know it from the above, but
I actually received passing marks in tenth-grade algebra.
No doubt my teacher's ghost is now petitioning the Celestial
Bookkeeper to have my marks stricken from the Record ...
I garbled the summation of the geometric series, somehow
mixing up bits and pieces of a series with ratio (1+y) and
another with ratio 1/(1+y) ... The formula should have been
p = c * (1. - Math.pow(1. + y, -n) / y;
| Quote: | But he said he needs to calculate y, not p
Woops! So he did, yes indeedy ...
It's easy to solve for p or c or n given the other
three values, but I can't see any way to solve for y
in closed form. Last time I worked this out (I was
buying a house and wanted to understand how the mortgage
calculations *really* worked), I recall trying Newton's
method and seeing it blow up rather badly; the derivative
got very small in the vicinity of the root. A lower-
order iteration was slow but got results; I think it
may have been
y1 = (1 - 1 / (y0 + 1)^n) * c / p
y2 = (1 - 1 / (y1 + 1)^n) * c / p
...
|
This, at least, seems to work. Lacking faith in my
obviously fading algebraic skill, I tested it by choosing
y and n and calculating the corresponding c/p, then starting
over with c/p and n known and running the iteration to get
the y value back.
FWIW, I also tried gajo's formula
| Quote: | y = n_root(A^n-1 + A^n-2 + ... + A) - 1, where A = c/p
|
.... on the same c/p and n, but got a nonsensical answer.
Suspecting a typo, I tried a few variations, adding the
A^n and/or A^0 terms to the sum, but didn't get anywhere.
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
gajo Guest
|
Posted: Fri Jun 25, 2004 11:18 pm Post subject: Re: Need help on math computation with java |
|
|
| Quote: | y1 = (1 - 1 / (y0 + 1)^n) * c / p
y2 = (1 - 1 / (y1 + 1)^n) * c / p
|
Well, since I still haven't passed all my algebra lectures I might be
making a fool out of myself by
asking these questions, but how are you supposed to calculate y from
these formulas? I tries the
following:
p = 4, c = 16, n = 2, and I guessed y0 = 1
Then I got
y1 = 16/4 * (1 - 1/(1+1)^2) =
= 4 * (1 - 1/4) = 3
Does that mean y = 3 ? Because I then placed it into the original
formula,
p = c * (1/(1+y) + 1/(1+y)^2)
And got that
4 = 16 * (1/4 + 1/16) = 4 + 1 = 5, which is wrong.
| Quote: | FWIW, I also tried gajo's formula
on the same c/p and n, but got a nonsensical answer.
|
Yes, funny how I overlooked a small detail, which turned out to change
the whole thing. I have modified
the formula now, but unfortunately the mistake is still around 9%, and
I can't tell why. Would anyone care
to look at how I solved this problem? Don't be afraid of the ugly
format where I'm forced to explain And the reason why it's long
because I split it into many lines to be more easily readable!
At the end I'll post a Java program that does all this.
So, we have the original formula:
p = c(1/1+y + ... + 1/(1+y)^n)
If we multiply this equation with (1+y)^n we get
(1+y)^n * p = c( (1+y)^n-1 + (1+y)^n-2 + ... + (1+y) + 1)
Let's throw p on the other side and make c/p = A
(1+y)^n = A( (1+y)^n-1 + ... + (1+y) + 1)
We can make a function f(n) = (1+y)^n
If we place f(n) into the equation we'll get
f(n) = A(f(n-1) + f(n-2) + ... + f(1) + f(0))
From here we can see that f(0) = 1
That means f(1) = A*f(0) = A
f(2) = A*f(1) + A*f(0) = A(A + 1)
f(3) = A*f(2) + A*f(1) + a*f(0) = A(A^2 + 2A + 1)
f(4) = A(A^3 + 3A^2 + 3A + 1)
f(5) = A(A^4 + 4A^3 + 6A^2 + 4A + 1)
As we go by, the pattern becomes obvious - the formula is equal to
that of (x+y)^n
That is how we get f(n) = A * (1+A)^(n-1)
We replace f(n) with (1+y)^n and we get
(1+y)^n = A * (1+A)^(n-1)
We need to get rid of that ^n. First we make it so that the right
side, (1+A) has the same power, that is
we multimply it with (1+A)/(1+A) and we get:
(1+y)^n = A * (1+A)^n / (1+A)
We divide this equation with (1+A)^n
((1+y)/(1+A))^n = A/(1+A)
We take the n-root out of it and get
(1+y)/(1+A) = n_root( A/(1+A) )
Throw 1+A on the left to the right
1+y = (1+A) * n_root( A/(1+A) )
Throw 1 on the right side and finally we get THE FORMULA:
y = (1+A) * n_root((A/(1+A)) - 1
As I've said, the formula makes mistakes. When I start with p=4, I get
y=3.47, and after placing this y
into the original formula and try to calculate p, I get that p=4.37
I guess there is a problem with the n_root not approximating the value
closely enough, but it could be the
formula that is wrong. If someone is willing to see where I'm doing
wrong, it would be great to finally
solve this man's problem (although he hasn't checked in again to let
us know what he thinks).
Finally, here's a code sample to test the formula:
public static double n_root(double number, int n) {
// can you fucking believe that there is no n_root
// function in Java!!?
if (n == 2) {
return Math.sqrt(number);
} else {
return Math.pow(Math.E,Math.log(number)/n);
}
}
public static double mainFormula(int p, int c, int n) {
double A = c/p;
return (1+A) * n_root(A/(1+A), n) - 1;
}
public static double testFormula(int c, int n, double y) {
double tmp = 0;
for (int k=1; k<=n; k++) {
tmp += 1/Math.pow(1+y, k);
}
return c*tmp;
}
public static void main(String[] args) {
System.out.println("p=4, c=16, n=2 -->
y="+mainFormula(7,16,15));
System.out.println("Test, p =
"+testFormula(16,15,mainFormula(7,16,15)));
}
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Fri Jun 25, 2004 11:32 pm Post subject: Re: Need help on math computation with java |
|
|
On 25 Jun 2004 16:18:15 -0700, [email]gcsaba2 (AT) freemail (DOT) hu[/email] (gajo) wrote or
quoted :
| Quote: | // can you fucking believe that there is no n_root
// function in Java!!?
|
Yes there is, just a tiny bit disguised. Math.pow( x, p );
use Math.pow( x, 1/p );
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Mon Jun 28, 2004 3:23 pm Post subject: Re: Need help on math computation with java |
|
|
gajo wrote:
| Quote: | y1 = (1 - 1 / (y0 + 1)^n) * c / p
y2 = (1 - 1 / (y1 + 1)^n) * c / p
Well, since I still haven't passed all my algebra lectures I might be
making a fool out of myself by
asking these questions, but how are you supposed to calculate y from
these formulas? I tries the
following:
p = 4, c = 16, n = 2, and I guessed y0 = 1
Then I got
y1 = 16/4 * (1 - 1/(1+1)^2) =
= 4 * (1 - 1/4) = 3
Does that mean y = 3 ? [...]
|
No: It means that y1 should be closer to the true
answer than the original guess y0 -- and y2 (obtained
by plugging y1) into the same formula should be closer
still. You keep on repeating, getting y2,y3,y4,...
until you're satisfied with the accuracy of the result.
y2 = 4 * (1 - 1/(y1+1)^2) = 3.75
y3 = 4 * (1 - 1/(y2+1)^2) = 3.822715 (approx)
y4 = 4 * (1 - 1/(y3+1)^2) = 3.828020
y5 = 4 * (1 - 1/(y4+1)^2) = 3.828398
y6 = 4 * (1 - 1/(y5+1)^2) = 3.828425
y7 = 4 * (1 - 1/(y6+1)^2) = 3.828427
y8 = 4 * (1 - 1/(y7+1)^2) = 3.828427
.... and that looks like a good place to stop: The seventh
significant digit is no longer changing, so we can be
pretty confident of the first six. We can check the
solution by plugging it into the original equation
16 * (1/(1+y + 1/(1+y ^2) = 4.00000012+
Of course, it's silly to solve the case n=2 iteratively,
since it's just a quadratic in x = 1/(1+y):
16 * (x + x^2) = 4
4*x^2 + 4*x - 1 = 0
x = (-4 +- sqrt(16 + 16)) / 8 = (-1 +- sqrt(2))/2
= (sqrt(2) - 1)/ 2 [choosing the positive root]
x = 1/(1 + y)
y = 1/x - 1 = 2/(sqrt(2) - 1) - 1 = 3.82842713-
It's possible to get direct solutions for n=3 and n=4
also, but I know of no way to do it for n>=5.
We're straying somewhat from Java here (we were never all
*that* close to begin with), so I'll just mention that the
study of techniques for solving non-linear equations is part
of a branch of applied mathematics called "Numerical Analysis."
Much of the topic is deep beyond my understanding, but pretty
much everyone who calls himself a programmer ought to be able
to swim in the shallower waters. Iterative methods like the
one above are found in the tidal pools, not in mid-ocean.
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Tue Jun 29, 2004 2:23 pm Post subject: [OT] Re: Need help on math computation with java |
|
|
Daniel Sjöblom wrote:
| Quote: | Eric Sosman wrote:
Of course, it's silly to solve the case n=2 iteratively,
since it's just a quadratic in x = 1/(1+y):
16 * (x + x^2) = 4
4*x^2 + 4*x - 1 = 0
x = (-4 +- sqrt(16 + 16)) / 8 = (-1 +- sqrt(2))/2
= (sqrt(2) - 1)/ 2 [choosing the positive root]
x = 1/(1 + y)
y = 1/x - 1 = 2/(sqrt(2) - 1) - 1 = 3.82842713-
It's possible to get direct solutions for n=3 and n=4
also, but I know of no way to do it for n>=5.
There cannot be a general formula for solving 5th grade equations by
taking n-th roots. It is possible to prove this, but I will not attempt
to do so here
|
Yes, yes: Abel proved that the general quintic equation
can't be solved by taking roots. (He was lots smarter than
I am -- why, when he was my age, he'd been dead twenty-nine
years!)
But Abel's proof does not state that *all* quintic
equations are insoluble; for example, consider `x^5 = 32'
or `x^5 - 28*x^4 + 288*x^3 - 1358*x^2 + 2927*x = 2310'
(which may look formidable, but trust me: it's a prime
example of an easily-solved quintic).
The O.P.'s equation has a very regular form, and it's
conceivable that there might be a way to exploit that
regularity to derive a closed-form solution. I don't know
how to do so, but I also don't want to assert without proof
that it can't be done.
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|