Pages: [1] 2   Go Down
 Author Topic: Manipulating the behavior of an exponential function (Math gurus needed!)  (Read 2232 times) 0 Members and 1 Guest are viewing this topic.
United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « on: July 22, 2012, 04:01:07 am » Bigger Smaller Reset

Hello,
I'm working with an exponential function, and I need a bit of help with manipulating its behavior. Basically, I have two points that I would like to maintain whilst "dragging" the curve of the function outwards in order to exaggerate the exponential behavior of the function. Here is the application: I have an input value (x) which I would like to scale from 1080 and 1750 to an output value (y) within 1500 and 2000. I do not want to use a linear equation, because I would like to create the effect where lower x values output y values incrementing at a much slower rate than output y values increment from higher x input values.

I am currently using this function to achieve my goal: y = (1500 / ((4/3)^(1/670))^1080) • ((4/3)^(1/670))

Unfortunately, the effect of the exponential behavior is very minor and unnoticeable in the application I am using it for in my program. How can I exaggerate the exponential behavior of the function whilst maintaining the two points (1080, 1500) and (1750, 2000)?
 Logged

fuzzball27 >>-->

Montreal
Offline
Edison Member
Karma: 16
Posts: 2209
 « Reply #1 on: July 22, 2012, 08:40:16 am » Bigger Smaller Reset

So, other thread didn't help much? http://arduino.cc/forum/index.php/topic,115296.0/topicseen.html
I'd suggest, before you try to program equation in C, you need clear representation of this equation in math with all coefficients defined, only after that you would be able to make a decision what "library" function is the best for task ( pow, exp, base 2, base 10, float vs integer etc).
The simple solution is Excell, or LibreOffice, where you play around with math, and see how it affects a  chart.  Only when you like what you see, you ready to transfer it in C.
 Logged

United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « Reply #2 on: July 22, 2012, 12:25:43 pm » Bigger Smaller Reset

So, other thread didn't help much?

You must be confused.
The other thread answered all of my questions. I got that function working just fine.

The simple solution is Excell, or LibreOffice, where you play around with math, and see how it affects a  chart. Only when you like what you see, you ready to transfer it in C.

I've been doing that, and I disagree with you. I have been doing research online and playing with graphs, and it would seem in this case the simple solution is to ask a forum with over 120,000 people who actually have an idea of what I'm talking about.
 Logged

fuzzball27 >>-->

Southern California
Offline
Full Member
Karma: 0
Posts: 105
 « Reply #3 on: July 23, 2012, 12:07:15 am » Bigger Smaller Reset

See if this has a noticeable effect:

y = 1500 + .004455(x-1080)*(x-1080)
 Logged

Netherlands
Online
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #4 on: July 23, 2012, 12:02:27 pm » Bigger Smaller Reset

Quote
I am currently using this function to achieve my goal: y = (1500 / ((4/3)^(1/670))^1080) • ((4/3)^(1/670))

Unfortunately, the effect of the exponential behavior is very minor and unnoticeable in the application I am using it for in my program. How can I exaggerate the exponential behavior of the function whilst maintaining the two points (1080, 1500) and (1750, 2000)?

Where is the x in the function?

How would you implement the "basic" function if it was between the points (0,0) and (1,1) ? If you can define that the rest is scaling.

Easiest option is to use - http://arduino.cc/playground/Main/MultiMap - which can mimic all non linear functions

 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « Reply #5 on: July 23, 2012, 12:25:13 pm » Bigger Smaller Reset

See if this has a noticeable effect:

y = 1500 + .004455(x-1080)*(x-1080)

Thank you, I'll give it a shot. How did you calculate it?

Where is the x in the function?

I apologize:  y = (1500 / ((4/3)^(1/670))^1080) • ((4/3)^(1/670))^x

How would you implement the "basic" function if it was between the points (0,0) and (1,1) ? If you can define that the rest is scaling.

Thank you! I believe you just answered my question. With a little trig and elbow grease I think that will work.

Easiest option is to use - http://arduino.cc/playground/Main/MultiMap - which can mimic all non linear functions

Yes, but he uses map() which uses long value types and I need a high-speed function, so I'm trying to stay away from 32 and 64 bit numbers. (although I have to use 32 bit because Arduino doesn't support 16 bit float values?)
 Logged

fuzzball27 >>-->

Ayer, Massachusetts, USA
Offline
Edison Member
Karma: 27
Posts: 1099
 « Reply #6 on: July 23, 2012, 01:11:26 pm » Bigger Smaller Reset

Yes, but he uses map() which uses long value types and I need a high-speed function, so I'm trying to stay away from 32 and 64 bit numbers. (although I have to use 32 bit because Arduino doesn't support 16 bit float values?)
Just remember, the ATmega chips are 8 bit chips, with no floating point.  This means every floating point calculation probably involves hundreds to thousands of operations (for add/subtract), and thousands to millions (for multiplies/division).  While 32-bit integer arithmetic also involves multiple instructions, it is likely 1-2 orders of magnitude faster than using floating point.  If you can recode your algorithm to not use floating point, it probably will execute faster.

If your code is floating point intensive, and you have time constraints on its execution, you probably should be thinking about using something else, like an ARM with hardware floating point.
 Logged

United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « Reply #7 on: July 23, 2012, 02:16:18 pm » Bigger Smaller Reset

See if this has a noticeable effect:

y = 1500 + .004455(x-1080)*(x-1080)

Wow. Genius. I should have thought of that earlier!
 Logged

fuzzball27 >>-->

United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « Reply #8 on: July 23, 2012, 02:43:38 pm » Bigger Smaller Reset

For the curious, here is the equation I am now using: y=((1/897.8 )*(x-1080)^2)+1500
 Logged

fuzzball27 >>-->

Southern California
Offline
Full Member
Karma: 0
Posts: 105
 « Reply #9 on: July 23, 2012, 06:03:50 pm » Bigger Smaller Reset

Quote
Quote from: JavaMan on July 22, 2012, 09:07:15 PM
See if this has a noticeable effect:

y = 1500 + .004455(x-1080)*(x-1080)

Thank you, I'll give it a shot. How did you calculate it?

The only number I calculated was .004455, and that was to make y = 2000 when x = 1750.  But I forgot about the 1500 and gave you the wrong number!  Should have been .001114.  Looks like you figured it out anyway.  Good job!
 Logged

Netherlands
Online
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #10 on: July 25, 2012, 10:42:37 am » Bigger Smaller Reset

Quote
For the curious, here is the equation I am now using: y=((1/897.8 )*(x-1080)^2)+1500

if x and y are of the type integer you can speed up this formula by removing the FP math

long t = x-1080;
t = t * t * 10;
y = t / 8978 + 1500;
 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « Reply #11 on: July 25, 2012, 12:54:09 pm » Bigger Smaller Reset

I'm using unsigned 16 bit integers (uint16_t) so I'd assume that will work? I'll give it a shot. @MichaelMeissner also suggested using this method of calculation (not in this thread).
 Logged

fuzzball27 >>-->

Netherlands
Online
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #12 on: July 29, 2012, 07:07:35 am » Bigger Smaller Reset

Quote
I'm using unsigned 16 bit integers (uint16_t) so I'd assume that will work?
No it will not.

your input values are in the range "1080 and 1750" squaring it gives values between 1 and 4 million, will not fit in an 16 bit.
OK the subtraction of 1080 makes it max 700^2 == 490.000 which still will not fit in a 16 bit int by a factor 7.

However if you persist to do only 16 bit math and you accept a (~1%) error you could apply the division in steps to keep the result under max unint16. The prices is less accuracy and some "staircasing" in the graph due to less distinct values. I used a spreadsheet to model the results below.

(original code, 2 long multiplies and one long division)
long t = x-1080;
t = t * t * 10;
y = t / 8978 + 1500;   (will have 445 distinct values)

Note: 8978 = 2x67x67
t = x-1080;    // t = 0..700
t = t/67;       // t = 0..11  (oops only 12 distinct values BAD)
t = t * t * 5;
y = t + 1500;   // still only 12 distinct values  TOO ROUGH

8978 is almost 8980 = 2.2.5.449

t = x-1080;    // t = 0..700
t1 = t/2;       // t1 = 0..350 (350 distinct values OK!)
t2 = t/5;       // t2 = 0..140 (140 disctinct values OK!)
note that using two distinct dividers give more distinct values in the end.

t = t1 * t2;    //  t = 0..44890
t = t / 89;     // (remaining factors result in => 2*449/10 = 89   (328 distinct values)
t = t + 1500;
if (t > 2000) t = 2000;   // max error is 4

Other
8978 is ~8982 = 2.3.3.499
===> using 2 and 3 will result in an overflow  (700^2 /6 >> 65535)
===> using 3 and 6 will give less distinct values than 2 and 5
===> using 1 and 9 is interesting as you need one division less! and it will map 1750 on 2000

t = x-1080;    // t = 0..700
t1 = t/9;       // t1 = 0..77
t = t1 * t;      //  t = 0..49580
t = t / 99;     // 280 distinct values
t = t + 1500;

So using these values produces slightly less distinct values but its codesize is also less (faster) AND still using only 16 bit math!.

another one to consider
8978 is ~8976 = 2.2.2.2.3.11.17
==> using 2 and 4 is interesting as two divisions would result in a shift (fast)
t = x-1080;
t1 = t/2;
t2 = t/4;
t = t1 * t2;      //  t = 0..55945
t = t / 111;     // => 262 distinct values (max error 6)
t = t + 1500;
if (t > 2000) t = 2000;

Epilog:
If small errors are acceptable one can improve the speed and the footprint of the algorithm quite a lot.

 « Last Edit: July 29, 2012, 07:10:26 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

SF Bay Area (USA)
Offline
Karma: 78
Posts: 5453
Strongly opinionated, but not official!
 « Reply #13 on: August 22, 2012, 04:50:00 am » Bigger Smaller Reset

Making me nervous...
Code:
for the curious, here is the equation I am now using: y=((1/897.8 )*(x-1080)^2)+1500
You know that "^" is NOT an exponentiation operation in actual c++, unless it got overloaded for floating point...
 Logged

United States
Offline
Jr. Member
Karma: 0
Posts: 99
Indubitably
 « Reply #14 on: August 22, 2012, 03:07:11 pm » Bigger Smaller Reset

Haha yes that was the "plain english" version. "^" is an operator isn't it?
 Logged

fuzzball27 >>-->

 Pages: [1] 2   Go Up