Some extra math functions

About advanced scenes, and the Thyme scripting language used in Algodoo.

Re: Some extra math functions

Postby gradyfitz » Sat Nov 07, 2009 8:13 am

Ivanlul wrote:I need help doing inverse trig fuctions(arcsin, arccos, arctan)

Math.asin(x) - Arcsine (Math.sin–1) of x.
Math.acos(x) - Arccosine (Math.cos-1) of x.
Math.atan(x) - Arctangent (Math.tan-1) of x.

Hopefully these are what you want (they are built into Algodoo and Phun).
Mechanisms: 18 Mechanisms.
Thyme: Tutorial - Variables/Commands List.
Thymechanic
gradyfitz
 
Posts: 174
Joined: Tue Sep 01, 2009 8:33 am
Location: Victoria, Australia

Re: Some extra math functions

Postby Ivanlul » Mon Nov 09, 2009 2:22 am

Thanks!
Registered: 2008-10-16
Image
User avatar
Ivanlul
 
Posts: 763
Joined: Mon Aug 31, 2009 11:19 pm
Location: US

Re: Some extra math functions

Postby KarateBrot » Tue Dec 01, 2009 1:54 pm

Lambert W function
The Lambert W function is the inverse function of f(x) = xe^x. It's needed if you know f(x) and you want to calculate x. It can't be solved analytic so you have to iterate the solution. This can be done with this code.
Strictly speaking the Lambert W function is a special case of the iteration.

Code: Select all
scene.my.LambertW = (x) =>
{
    y := (x <= 1 ? {0} : {(x ^ 0.001 - 1) / 0.001});
    e := 2.718281828;
    for(20, (i)=>{ y = y - (y * e ^ y - x) / (e ^ y * (y + 1)) })
};



Example:

1)
We want to know the solution of 3 = xe^x

2)
In the console we type:
scene.my.LambertW(3)

3)
The solution will be 1.0499089. Now we know: 3 = 1.0499089*e^1.0499089
Last edited by KarateBrot on Wed Dec 02, 2009 8:52 pm, edited 1 time in total.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby KarateBrot » Wed Dec 02, 2009 8:50 pm

Iteration (Bisection)

Code: Select all
scene.my.bisection = (f, x1, x2) =>
{
    left := math.tofloat(x1);
    right := math.tofloat(x2);
    mid := (left + right) / 2;
    f(mid) == 0 ? {} : {
        for(30, (i)=>{
            mid = (left + right) / 2;
            (f(left) * f(mid)) < 0 ? {right = mid} : {left = mid}
        })
    };
    mid = (left + right) / 2
}



scene.my.bisection(f, x1, x2)
f: function
x1: value on the left side of the root
x2: value on the right side of the root


Example:

1)
As always we want to calculate the root of the function f(x) = cos(x) - x
We also need two proper values which we know a root is between them. Lets take 0 for the small value and 2 for the big value.

2)
In the console we type:
scene.my.bisection( (x)=>{math.cos(x)-x}, 0, 2 )

3)
The solution will be 0.7390852 which is our root
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby standardtoaster » Tue Dec 08, 2009 5:50 pm

Endpoint Formula

Code: Select all
Scene.my.EndPoint=(P, L, A)=>{[P(0) + (L * math.cos(A)), P(1) + (L * math.sin(A))]}

P is the point that you know of. L is length of the "line" between P and your unknown point. A is the angle, in radians. Scene.my.EndPoint([0, 0], 4, math.pi/4) will give you [2.828427, 2.828427]. That is your unknown second point.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Some extra math functions

Postby RA2lover » Thu Dec 10, 2009 1:46 am

that's a polar projection, already included in link0007's starting post
Jrv wrote:
TC42 wrote:Quite honestly, I didn't think anyone on 4chan has that good a use of grammar, spelling, usage, mechanics, ect.
But I've never been there, so I may be wrong.


GTFO newfgt
User avatar
RA2lover
 
Posts: 607
Joined: Mon Aug 31, 2009 8:43 pm
Location: Brazil

Re: Some extra math functions

Postby standardtoaster » Thu Dec 10, 2009 2:06 am

Oh. I didn't know that. I haven't really learned about polar projection yet. I had originally made that code for my softbody spawner. I didn't know it had already been done.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Some extra math functions

Postby KarateBrot » Thu Dec 10, 2009 9:54 pm

Random number

Code: Select all
scene.my.memory = 17;

scene.my.random = (min, max) =>
{
      x := scene.my.memory;
      m := 1249 * 373;
      x = x ^ 2 % m;
      x == 0 ? {scene.my.memory = x + 1} : {scene.my.memory = x};
      x * (max - min) / (1249 * 373.0) + min
}


- - - - -

min: Minimum random number
max: Maximum random number

Your random number then will be between min and max



Improved code
The old one had a period at about 1200 numbers. Now there's no period anymore if the simulation is running.

Code: Select all
scene.my.memory = 17;

scene.my.random = (min, max) =>
{
      x := scene.my.memory;
      m := 1249 * 373;
      x = x ^ 2 % m;
      scene.my.memory = x + sim.time;
      x <= 1 ? {scene.my.memory = scene.my.memory + 17} : { };
      x * (max - min) / (1249 * 373.0) + min
}
Last edited by KarateBrot on Wed Feb 10, 2010 4:52 am, edited 1 time in total.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby KarateBrot » Sat Dec 12, 2009 6:22 pm

Integral
It's not the exact integral because algodoo can't calculate it. It's a numerical integral based on simpson's rule and pretty precise.

Code: Select all
scene.my.integral = (f, a, b)=>
{
m1 := 0; m2 := 0; (b - a) / (3.0 * 40) * (0.5 * f(a) + for(39, (k)=>{m1 = m1 + f(a + (k + 1) * (b - a) / 40.0)}) + 2 * for(40, (k)=>{m2 = m2 + f(((a + k * (b - a) / 40.0) + (a + (k + 1) * (b - a) / 40.0)) / 2.0)}) + 0.5 * f(b))
}


scene.my.integral(f, a, b)
f: function
a: lower limit
b: upper limit


Example:

1)
We want to calculate the integral of the function f(x) = cos(x) - x between a = 0 and b = 2

2)
In the console we type:
scene.my.integral( (x)=>{math.cos(x)-x}, 0, 2 )

3)
The solution will be -1.0907027.
Note: It's only a deviation of 1.1920929*10^(-7) from the real value
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby Rexy » Mon Dec 21, 2009 10:44 pm

Whats all this? :?
Rexy
 
Posts: 14
Joined: Mon Dec 14, 2009 9:38 pm

Re: Some extra math functions

Postby standardtoaster » Mon Dec 21, 2009 11:11 pm

These are user-created math functions. They are made because quite a few math functions that are handy/required for certain formulas are not native to Algodoo.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Some extra math functions

Postby Rexy » Thu Dec 24, 2009 3:28 pm

Oh ^^, So what are they usefull for, in Algodoo/phun?
Rexy
 
Posts: 14
Joined: Mon Dec 14, 2009 9:38 pm

Re: Some extra math functions

Postby KarateBrot » Thu Dec 24, 2009 4:52 pm

depends on what you want to make. for example if you want to calculate the distance of a lot of points to each other you can use the distance function. otherwise it's just too much work to write the formula for each point over and over again.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby blahblah200 » Fri Jan 08, 2010 6:58 pm

How and where do you enter the functions and how do you edit the different points - P1 and P2 - in the distance function -
Code: Select all
scene.my.dist = (P1, P2) =>
{
     math.pow(math.pow(P2(0) - P1(0), 2) + math.pow(P2(1) - P1(1), 2), 0.5);
};
because I have read Grady's tutorial and I still haven't got a clue... :? :oops:
Image<-- Made with GIMP
User avatar
blahblah200
 
Posts: 61
Joined: Tue Sep 01, 2009 11:33 am
Location: Somewhere over the rainbow...

Re: Some extra math functions

Postby standardtoaster » Fri Jan 08, 2010 7:23 pm

You paste that into the console. You can open the console with ~ or F11. Paste it in and press return. The distance formula is a function. It takes two inputs. Use like this: Scene.my.dist(scene.my.p1, scene.my.p2) That will return the distance between scene.my.p1 and scene.my.p2. It doesn't need be those exact variables. P1 and P2 are just there for easy readability. He could have made blerp and glockenspiel if he wanted to. :D
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Some extra math functions

Postby blahblah200 » Sat Jan 09, 2010 2:26 pm

Sorry for being a noob, but it didn't work and I did these steps;
  1. Typed in the console
    Code: Select all
    scene.my.dist = (P1, P2) => {math.pow(math.pow(P2(0) - P1(0), 2) + math.pow(P2(1) - P1(1), 2), 0.5);};
    scene.my.P1 = [0, 0];
    scene.my.P2 = [0, 0];
  2. Made a scene like this
    Code: Select all
    o   o
    |-----|
  3. Put in the left circle in the OnCollide menu
    Code: Select all
    (e)=>{scene.my.P1 = e.pos;  scene.my.dist(scene.my.P1, scene.my.P2)}
  4. Put in the right circle in the OnCollide menu
    Code: Select all
    (e)=>{scene.my.P1 = e.pos;  scene.my.dist(scene.my.P1, scene.my.P2)}
  5. Put in the Text script menu
    Code: Select all
    { "" + scene.my.dist}

But at the end of all this, when the circles collided with the box, the text still read
Code: Select all
scene.my.dist = (P1, P2) => {math.pow(math.pow(P2(0) - P1(0), 2) + math.pow(P2(1) - P1(1), 2), 0.5);};


Do you know what went wrong??
Image<-- Made with GIMP
User avatar
blahblah200
 
Posts: 61
Joined: Tue Sep 01, 2009 11:33 am
Location: Somewhere over the rainbow...

Re: Some extra math functions

Postby KarateBrot » Sat Jan 09, 2010 4:08 pm

scene.my.dist is just the funtion. if you put the two points into the function it calculates the distance and that's a number.

so for the text script menu you have to type:
Code: Select all
{ "" + scene.my.dist(scene.my.P1, scene.my.P2) }


and you can delete "scene.my.dist(scene.my.P1, scene.my.P2)" from onCollide because you don't need it there. you just need to get the two points in OnCollide.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby blahblah200 » Fri Jan 15, 2010 8:39 pm

Thank you SOO much!! This is going to help me in so many future projects!!
thx!!
Image<-- Made with GIMP
User avatar
blahblah200
 
Posts: 61
Joined: Tue Sep 01, 2009 11:33 am
Location: Somewhere over the rainbow...

Re: Some extra math functions

Postby KarateBrot » Tue Feb 02, 2010 7:49 pm

Unit vector
With this function you can shorten every vector to the length of 1 unit.
You can also do this with vector / scene.my.dist([0,0], vector) but for long vectors I find scene.my.unitvector(vector) a bit more handy.

Code: Select all
scene.my.unitvector = (v) => { v / (v(0)^2 + v(1)^2)^0.5 }
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Some extra math functions

Postby kilebantick » Wed Feb 03, 2010 6:06 am

Can someone make a "Random" function based off of hingecolor? scene.addhingecolor, or something along the lines of that? I'm an ammature, don't know what's possible, and what is :)
Image
User avatar
kilebantick
 
Posts: 1267
Joined: Tue Sep 01, 2009 9:50 am

Re: Some extra math functions

Postby standardtoaster » Wed Feb 03, 2010 7:02 am

Code: Select all
Scene.my.random={(Scene.addHinge{}).color(0)}

That will produce a random number between 0 and 1. However, the console will print "!! ERROR: Constraint connected to unknown geometry".
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Some extra math functions

Postby kilebantick » Fri Feb 05, 2010 2:14 am

standardtoaster wrote:
Code: Select all
Scene.my.random={(Scene.addHinge{}).color(0)}

That will produce a random number between 0 and 1. However, the console will print "!! ERROR: Constraint connected to unknown geometry".

That's fine for me, i don't really care much about the console :)
Image
User avatar
kilebantick
 
Posts: 1267
Joined: Tue Sep 01, 2009 9:50 am

Re: Some extra math functions

Postby Mystery » Fri Feb 05, 2010 3:05 pm

Code: Select all
Scene.my.random={(Scene.addbox{density := 0}).color(0)}

I like my code better. Doesn't clog up the console :)
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: Some extra math functions

Postby standardtoaster » Fri Feb 05, 2010 4:27 pm

Any way works as long as it spawns something.
Code: Select all
Scene.my.random={(Scene.add*{}).color(0)
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Some extra math functions

Postby canadian joe » Mon Feb 15, 2010 7:13 pm

HOW DO PEOPLE COME UP WITH THESE KINDS OF SCRIPTS! Compared to this script i have the skills of a cheesecake..............a dead cheesecake. I probably just don't know scripting language that well :(
canadian joe wrote:im just writing this so i can sig it


RicH wrote:Phun's Army.
User avatar
canadian joe
 
Posts: 115
Joined: Wed Feb 10, 2010 9:42 pm

PreviousNext

Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest

cron