Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 12:10 PM   PM User | #1
mosquitobite
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 2
Thanked 1 Time in 1 Post
mosquitobite is an unknown quantity at this point
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; 01-24-2013 at 12:18 PM..
mosquitobite is offline   Reply With Quote
Old 01-24-2013, 02:05 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
}
As g occurs only in one line I do not see how it can be wrong if 3.73 is correct when g = 1.

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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mosquitobite (01-25-2013)
Old 01-24-2013, 03:01 PM   PM User | #3
mosquitobite
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 2
Thanked 1 Time in 1 Post
mosquitobite is an unknown quantity at this point
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.
mosquitobite is offline   Reply With Quote
Old 01-24-2013, 08:31 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,217
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
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.
*CLEARLY* if 3.73, is correct for G=1, then the answer should be 37.3 for G=10.

After all, to transform
Code:
F / G: = 1 / N - N / (1 + I)^N - 1
to solve for F, all you do is multiply both sides by G:
Code:
G * ( F / G ) = G * ( 1 / N - N / (1 + I)^N - 1 )
-->>
F = G * ( 1 / N - N / (1 + I)^N - 1 )
which means the answer for G=10 should be 10 times the answer for G=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=1, I=10, N=10 indeed I get 3.73
And for G=10, I=10, N=10 indeed I get 37.25
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-24-2013, 08:34 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,217
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quite frankly, I don't understand how this:
Code:
F = G * ( 1 / N - N / (1 + I)^N - 1 )
works at all, when *CLEARLY* the formula starts with 1/I and *NOT* 1/N.

Also, you are missing the *REQUIRED* parentheses around the sub-expression ( (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!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
mosquitobite (01-25-2013)
Old 01-25-2013, 12:32 PM   PM User | #6
mosquitobite
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 2
Thanked 1 Time in 1 Post
mosquitobite is an unknown quantity at this point
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.
mosquitobite is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 PM.


Advertisement
Log in to turn off these ads.