Javascript assignment multiplication operators in a loop as a Math function

Mircea

I am doing the following calculations in a loop:

https://jsfiddle.net/0xLvxjar/

var i = 0;
var times = 200;
var friction = 0.9925;
var velocity = 10;
var position = 100;

for (; i < times; i += 1) {
    velocity *= friction;
    position += velocity;
}

console.log(velocity) // 2.218723008169958 
console.log(position) // 1129.722321918849

I would like to rewrite that as a Math function and eliminate the loop if possible.

Looking trough the JavaScript Math object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math I am not seeing something to handle this.

Is there a better way of getting to the final result using a Math formula?

Hauke P.

Solve it with math:

// v_1 = v_0 * f
// p_1 = p_0 + v_1
// 
// p_2 = p_1 + v_1 * f
//     = p_0 + v_1 + v_1 * f
//     = p_0 + v_0 * f + v_0 * f * f
//     = p_0 + v_0 * (f + f^2)

At this point you might see that p_n = p_0 + v_0 * (f + f^2 + f^3 + ... + f^n).

From your formulary you might know that this is true:

Formula

Therefore p_n = p_0 + v_0 * f * (1 - f^n) / (1 - f).

Transferred to JavaScript you get this:

var n = 200;
var friction = 0.9925;
var velocity_0 = 10.0;
var position_0 = 100.0;

var velocity_n = velocity_0 * Math.pow(friction, n);
var position_n = position_0 + velocity_0 * friction * (1 - Math.pow(friction, n)) / (1 - friction);


alert(velocity_n);
alert(position_n);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript assignment multiplication operators in a loop as a Math function

From Dev

Python for() loop with math operators

From Dev

Javascript logical operators and assignment?

From Dev

While loop and outputting math operators

From Dev

Chained assignment of variables with operators in JavaScript

From Dev

Simple substitute of assignment operators of logical ones in JavaScript?

From Dev

Javascript: parseInt issue when it comes to math operators

From Dev

Javascript math function resolver

From Dev

Javascript for loop doing bad math

From Dev

Explanation of JavaScript bit operators in this function

From Dev

Javascript function assignment using eval

From Dev

Javascript this undefined after function assignment

From Dev

Javascript this undefined after function assignment

From Dev

About JavaScript and PHP assignment operators: Why the different results?

From Dev

Replicating the function of a for loop using only bitwise operators

From Dev

Generate a random math Equation using Random numbers and operators in Javascript

From Dev

clojure assignment of variables in a loop function that returns a vector

From Dev

clojure assignment of variables in a loop function that returns a vector

From Dev

Arrays Random Multiplication Math

From Dev

Variable assignment with ">>" and "&" operators

From Dev

Assignment operators in R: '<-' and '<<-'

From Dev

Assignment operators in R: '<-' and '<<-'

From Dev

Assignment operators in if statement

From Dev

Recursive functions with math operators

From Dev

for loop inside for loop function in javascript

From Dev

for loop inside for loop function in javascript

From Dev

javascript TypeError: Math.random is not a function on chrome

From Dev

Math.random() javascript function *undefined* on Chrome

From Dev

Hypotenuse using function & Math.sqrt() javascript

Related Related

  1. 1

    Javascript assignment multiplication operators in a loop as a Math function

  2. 2

    Python for() loop with math operators

  3. 3

    Javascript logical operators and assignment?

  4. 4

    While loop and outputting math operators

  5. 5

    Chained assignment of variables with operators in JavaScript

  6. 6

    Simple substitute of assignment operators of logical ones in JavaScript?

  7. 7

    Javascript: parseInt issue when it comes to math operators

  8. 8

    Javascript math function resolver

  9. 9

    Javascript for loop doing bad math

  10. 10

    Explanation of JavaScript bit operators in this function

  11. 11

    Javascript function assignment using eval

  12. 12

    Javascript this undefined after function assignment

  13. 13

    Javascript this undefined after function assignment

  14. 14

    About JavaScript and PHP assignment operators: Why the different results?

  15. 15

    Replicating the function of a for loop using only bitwise operators

  16. 16

    Generate a random math Equation using Random numbers and operators in Javascript

  17. 17

    clojure assignment of variables in a loop function that returns a vector

  18. 18

    clojure assignment of variables in a loop function that returns a vector

  19. 19

    Arrays Random Multiplication Math

  20. 20

    Variable assignment with ">>" and "&" operators

  21. 21

    Assignment operators in R: '<-' and '<<-'

  22. 22

    Assignment operators in R: '<-' and '<<-'

  23. 23

    Assignment operators in if statement

  24. 24

    Recursive functions with math operators

  25. 25

    for loop inside for loop function in javascript

  26. 26

    for loop inside for loop function in javascript

  27. 27

    javascript TypeError: Math.random is not a function on chrome

  28. 28

    Math.random() javascript function *undefined* on Chrome

  29. 29

    Hypotenuse using function & Math.sqrt() javascript

HotTag

Archive