Some extra math functions
60 posts • Page 2 of 3 • 1, 2, 3
Re: Some extra math functions
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).
- gradyfitz
- Posts: 174
- Joined: Tue Sep 01, 2009 8:33 am
- Location: Victoria, Australia
Re: Some extra math functions
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.
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
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
Iteration (Bisection)
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
- 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
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
Endpoint Formula
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.
- 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.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Some extra math functions
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
-
RA2lover - Posts: 607
- Joined: Mon Aug 31, 2009 8:43 pm
- Location: Brazil
Re: Some extra math functions
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.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Some extra math functions
Random number
- - - - -
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;
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
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.
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
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
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
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.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Some extra math functions
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
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
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);
};
<-- Made with GIMP
-
blahblah200 - Posts: 61
- Joined: Tue Sep 01, 2009 11:33 am
- Location: Somewhere over the rainbow...
Re: Some extra math functions
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.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Some extra math functions
Sorry for being a noob, but it didn't work and I did these steps;
But at the end of all this, when the circles collided with the box, the text still read
Do you know what went wrong??
- 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];
- Made a scene like this
- Code: Select all
o o
|-----|
- 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)}
- 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)}
- 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??
<-- Made with GIMP
-
blahblah200 - Posts: 61
- Joined: Tue Sep 01, 2009 11:33 am
- Location: Somewhere over the rainbow...
Re: Some extra math functions
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:
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.
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
Thank you SOO much!! This is going to help me in so many future projects!!
thx!!
thx!!
<-- Made with GIMP
-
blahblah200 - Posts: 61
- Joined: Tue Sep 01, 2009 11:33 am
- Location: Somewhere over the rainbow...
Re: Some extra math functions
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.
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 }
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Some extra math functions
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
-
kilebantick - Posts: 1267
- Joined: Tue Sep 01, 2009 9:50 am
Re: Some extra math functions
- 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".
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Some extra math functions
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
-
kilebantick - Posts: 1267
- Joined: Tue Sep 01, 2009 9:50 am
Re: Some extra math functions
- Code: Select all
Scene.my.random={(Scene.addbox{density := 0}).color(0)}
I like my code better. Doesn't clog up the console
-
Mystery - Posts: 2802
- Joined: Thu Sep 03, 2009 1:16 pm
- Location: Southern Australia
Re: Some extra math functions
Any way works as long as it spawns something.
- Code: Select all
Scene.my.random={(Scene.add*{}).color(0)
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Some extra math functions
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.
-
canadian joe - Posts: 115
- Joined: Wed Feb 10, 2010 9:42 pm
60 posts • Page 2 of 3 • 1, 2, 3
Who is online
Users browsing this forum: No registered users and 1 guest