Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6

01242013, 12:10 PM #1
 Join Date
 Jan 2013
 Posts
 4
 Thanks
 2
 Thanked 1 Time in 1 Post
Translating math formula JavaScript Help
Greetings
I am currently in the process of learning javascript and been working on working on this small simple project that calculates economic formula instantly. I managed to get 7 out of 8 formula to work but got stuck on the last one. I was wondering if some could please show me what I did wrong or how to fix my errors. I will post the formula below.
Formula is Finding Future Value (F) given Gradient Value (G),
Interest (I), and year (N).
F / G: = 1 / N  N / (1 + I)^N  1
My Problem is when I added value G = 10, I = 10, N =10
I get a result of 93.73 which is wrong. Where i calculate 37.31 as correct.
When I do G =1, I = 10, N = 10.
I get a result of 3.73, which is correct.
Regardless of what I do I can't seem to fix it. Any help and advice is very appreciated. Thank you very much.
Code:function fvg () { power = document.form.gradient.value * (1 / (document.form.rate.value / 100))  (document.form.year.value) / (Math.pow((1 + (document.form.rate.value / 100)), document.form.year.value)  1); document.form.amount.value = Math.round(power * 100) / 100; document.form.interest.value = Math.round((document.form.amount.value  document.form.gradient. value) * 100) / 100; }
Last edited by mosquitobite; 01242013 at 12:18 PM.

01242013, 02:05 PM #2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 19,377
 Thanks
 217
 Thanked 2,696 Times in 2,672 Posts
Are you quite sure that the answer is not in fact 93.73? And that your formula is truly correct?
Simplified:
Code:function fvg() { var p = 0; var g = 10; var i = 10; var n = 10; p = g * (1 / (i /100)); p = p  (n / (Math.pow((1 + (i/100)), n)  1)); p = Math.round(p*100)/100; alert (p); // 93.73 }
Sampson was a strong man who let himself be led astray by a Jezebel, like Delilah.  Pupil's answer to Catholic Elementary School test.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Users who have thanked Philip M for this post:
mosquitobite (01252013)

01242013, 03:01 PM #3
 Join Date
 Jan 2013
 Posts
 4
 Thanks
 2
 Thanked 1 Time in 1 Post
Hi Phillip
Thank you very much for your response and for your script verification. Now thats an interesting result, it is showing up the same as my scripts. It could very well be possible that this may be an accurate answer. I rechecked my formula in my econ text book. I did made a mistake in my earlier post for it should be A / G instead of F / G. My apologies for this misrepresentation.
it should be this formula, which is identical to my Econ book. So, theoretically it should be correct :/.
I can't say I am confident on my math skills but this what I got when I calculated manually. So base on my result I was expecting something similar for my script. Unless my calculation and order of operation is flawed.
Variables
G = 10 I = 10 / 100 = .10 N = 10
Formula
G = (1/.10)  (10 / ((1 + .10)^10)  1)
My manual calculation and results
A) 1/.10 = 10 Formula  (1/I)
B) 1 + .10 = 1.10 => (1.10)^10 = 2.59 => 2.59  1 = 1.59 Formula (1+I)^10  1)
C) 10 / 1.59 = 6.28 Formula  10 / (1+i)^N 1
D) 10  6.28 = 3.71 Formula (1/i)  (N / (1 + i)^N 1
E) 10 * 3.71 = 37.10 Formula G *1/i)  (N / (1 + i)^N 1
So I came to my final as 37.10 ~ 37.30 depending on the calculator I use. I was trying to replicate this with my javascript assuming my manual calculation is close of being correct. Would you happen something I did wrong? Thank you very much.

01242013, 08:31 PM #4F / G: = 1 / N  N / (1 + I)^N  1
My Problem is when I added value G = 10, I = 10, N =10
I get a result of 93.73 which is wrong. Where i calculate 37.31 as correct.
When I do G =1, I = 10, N = 10.
I get a result of 3.73, which is correct.
After all, to transformCode:F / G: = 1 / N  N / (1 + I)^N  1
Code:G * ( F / G ) = G * ( 1 / N  N / (1 + I)^N  1 ) >> F = G * ( 1 / N  N / (1 + I)^N  1 )
Let's just translate that pictured formula, exactly, with no simplifications:
Code:<!DOCTYPE html> <html> <body> <form id="theForm"> Gradient: <input name="gradient"/><br/> Rate: <input name="rate"/><br/> Years: <input name="year"/><br> <input type="button" value="calculate" onclick="fvg()"/> <hr/> Amount: <input name="amount" readonly /><br/> Interest: <input name="interest" readonly /> </form> <script type="text/javascript"> // UGUS = 1/i  n/( (1+i)^n 1 ) function UGUS( G, i, n ) { // UGUS formula save that we multiply by G return G * ( 1/i  ( n / ( Math.pow(1+i,n)  1 ) ) ); } function fvg () { var form = document.getElementById("theForm"); var gradient = Number( form.gradient.value ); var interest = form.rate.value / 100; var periods = Number( form.year.value ); var power = UGUS( gradient, interest, periods ); form.amount.value = power.toFixed(2); // forget Math.round! form.interest.value = ( power  gradient ).toFixed(2); } </script> </body> </html>
And for G=10, I=10, N=10 indeed I get 37.25Be yourself. No one else is as qualified.

01242013, 08:34 PM #5
Quite frankly, I don't understand how this:
Code:F = G * ( 1 / N  N / (1 + I)^N  1 )
Also, you are missing the *REQUIRED* parentheses around the subexpression ( (1+I)^N  1)
I think the only reason it *SEEMED* to work was because you used the *SAME NUMBER* (10) for both I and N!!!
PURE LUCK!Be yourself. No one else is as qualified.

Users who have thanked Old Pedant for this post:
mosquitobite (01252013)

01252013, 12:32 PM #6
 Join Date
 Jan 2013
 Posts
 4
 Thanks
 2
 Thanked 1 Time in 1 Post
Wow! Old Pendant, thank your script, this works out beautifully! I spent a couple hours on this, tweak with numerous amount of variation and still couldn't come up with a solution.
What I posted somehow worked mostly randomly moving chunks around. I think indeed it is PURE LUCK! Or maybe beginner's luck since I just started learning how to write javascripts .
A little more tweaking I have to do on my codes and see if I can really understand how this works based on your model.
Once again my appreciation to Old Pendant and Phillip M for your help and feedback. Thank you.