Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Forked from 140bytes/LICENSE.txt
Created May 26, 2011 11:28
Show Gist options
  • Save jfhbrook/992957 to your computer and use it in GitHub Desktop.
Save jfhbrook/992957 to your computer and use it in GitHub Desktop.
Univariate Euler's Method in <140 bytes. Pretty easy tbh.

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

The 140byt.es site hasn't launched yet, but for now follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt. It was inspired by work from Thomas Fuchs and Dustin Diaz.

// This function implements Euler's method, an explicit numerical method for solving
// ordinary differential equations, in one dimension. That is, position is a scalar.
// What did you expect? It's under 140 bytes. ;)
// For more, see http://en.wikipedia.org/wiki/Euler_method
function(a,b,c,d,e) {
// a: A function of the form `velocity = function(time,position)`
// I also wrote an example use that's <140 characters, including a relatively
// non-trivial ODE.
// b: Initial position, usually called `y_0` or "y-not" in the literature
// c: timespan (think seconds)
// d: step size (think seconds)
// e: placeholder for the result, which is a series of positions corresponding
// to t = [0, c, 2*c, ... Math.ceil(c/d)]
e = [b]; //The first "slot" of the result should by y_not.
//time is equal to i*d
// The method itself is actually simply defined, in terms of a recursive
// relationship. All it does is take the current velocity and predict the next
// position by extrapolating.
for(var i=0; i < -~(c/d); i++){
e.push( e[i] + d*a(d*i,e[i]))
}
return e;
}
(function(a,b,c,d,e,i){e=[b];for(i=0;i<-~(c/d);i++){e.push(e[i]+d*a(d*i,e[i]))}return e})(function(t,y){return y+Math.sin(t);},0,10,1)
function(a,b,c,d,e,i){e=[b];for(i=0;i<-~(c/d);i++){e.push(e[i]+d*a(d*i,e[i]))}return e}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Joshua Holbrook <http://jesusabdullah.github.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "euler",
"description": "Implements the Euler Method for solving ODEs for one dimension.",
"keywords": [
"140bytes",
"numerical"
]
}
@Kambfhase
Copy link

Nice, I see some room for improvements though:

  1. i is not declared.
  2. if c/d is always positve Math.ceil(c/d) can be shortened to -~(c/d).
  3. if you omit the block of the for loop and simply use a ; you can save another byte.
  4. you might be able to integrate the i++ into the loop body.

mfG Hase

@jfhbrook
Copy link
Author

Ooh, the "i is not declared" might actually disqualify this one until I fix it. >_< I like that bithack though. I always forget the bitwise tricks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment