math help. exponents

Hi, I looked at basic commands for math and I didn't see the answer.

I am getting a value from 0-70 in b3 and I need b10 to be a value from 0-255 where the closer b3 gets to 70, the more it affects b10 (so on a graph it would be a curve rather than a line.)

So I want to do b10 = b3^1.3042

how would I code this?
 

westaust55

Moderator
Try a google search for exponents and logarithms.
I have posted code here to in effect do the reverse and calculate a logarithm of a number from 1 to 65535 to 2 decimal places which you should find with a search.
Sorry do not have time to give more advise and type a long response by iPhone.
 

hippy

Ex-Staff (retired)
The quick and easy solution is to pre-calculate b3^1.3042 for b3=0 to 70, put those values into EEPROM then use a READ command.

0 1 2 4 6 8 10 13 15 18 20 23 26 28 31 34 37 40 43 47 50 53 56 60 63 67 70 74 77 81 84 88 92 96 99 103 107 111 115 119 123 127 131 135 139 143 147 152 156 160 164 169 173 177 182 186 191 195 199 204 208 213 218 222 227 231 236 241 245 250 255
 
Last edited:

LeonR

Member
I think hippy may be correct, I was hoping to find a way by multiplying itself in a loop but the decimal makes it hard so i've given up for the moment! :confused:
 
Hey Hippy, this looks like the easiest solution for me right now. Is there a good tutorial on how to use eeprom that you know of? Also how much memory will this use up? because I am very tight for space.
 

g6ejd

Senior Member
No program space just EEPROM.

READ location-address, result

e.g. READ 10, B0 'places whatever value is in address 10 into the byte / variable B0

WRITE 10,22
READ 10,B0
B0= 22
 

boriz

Senior Member
Hmm. Never seen Aztec before. Like the site. Like the ease of ordering (no need to type address and stuff). Just ordered some kit. Let's see how it goes.
 

MartinM57

Moderator
This is what is called an affine transformation.

A great tutorial on exactly how to do this with a picAXE is on the Aztec MCU website: http://www.aztecmcu.com/id75.html

good luck!
Welcome to the forum :)

I don't think the requirement is for an affine transformation - aren't they just for straight lines (well, the quoted reference page is for straight lines only)

I am getting a value from 0-70 in b3 and I need b10 to be a value from 0-255 where the closer b3 gets to 70, the more it affects b10 (so on a graph it would be a curve rather than a line.)
 

Domonicb

New Member
Welcome to the forum :)

I don't think the requirement is for an affine transformation - aren't they just for straight lines (well, the quoted reference page is for straight lines only)
Thanks for the welcome; yes you are correct - spoke too soon perhaps on that one :-D

For those that have never seen Aztec MCU - I have ordered from them several times and find them to be reasonably priced, quick to ship, and Bill the owner is a Gem when comes to customer service or help with a project.
 

DirtBiker

New Member
An affine transform can be applied to any data set or function.

If you could live with a quadratic curve (x^2 is a little bit more aggressive than x^1.3042) and apply an affine transform to it, then convert to PICAXE basic you would get something like:

b10 = 13 * b3 * b3 / 249

That will give back 0-255 for an input range of 0-70 starting very slow and increasing. Try it and see if it suits your needs.

BTW, I have also used Aztec MCU several times. I agree on all points mentioned. They are great to deal with.
 

westaust55

Moderator
Consider that
x^(a/b) = bth root of (x^a).

So if you can find a fraction that matches the power part of the calculation then you can possibly solve the calculation.

So with the power as 1.3042 this equates to 3.9 / 3.0 or as an approximation is 4 / 3
X^4 is simple as x*x*x*x
And I have previously posted a subroutine for the cube root calculation (to two decimal places).
But as we have used an approximation the values are roughly 12% high so to keep within the 0 – 255 result range you would need to adjust by something like (+0.15 * 0.88)

A check in Excel indicate if you use multipliers to keep say 2 decimal places then the error will range from about 2% low to around 6% high but keep in mind we took an approximation with 1.3042 being 4/3.

As values for a and b become larger the math will take longer. For example:
If b were to become 5 then you need a square root and a cube root calculation.
(I have also posted a square root to 2 decimal places routine).


However at the end of the day, where there are a smallish number of values to be used, an EEPROM or TABLE memory based look up table as suggested by hippy will require less space and time to achieve a result.
 
Last edited:

westaust55

Moderator
Another way by again using a lot of program code and power is:
consider that:
1. x^y = 10^(y * log10(x)) - but you still need to calculate 10^x
or
2. x^y = e^(y * ln(x))

now I have posted a routine for log10 to two decimal places on this forum before and ln(x) =log(x) * 2.30258 (etc)
and e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + x^5/5! + x^6/6! + x^7/7! +x^8/8! . . . . . (well you get the idea and you would have to go to about x^8/8! to have reasonable accuracy.

(where x! is the factorial of x - eg 5! = 5*4*3*2*1)

So it could be done on a PICAXE but will still take up a lot of code which is far from warranted unless you want calculations of numbers like x^y with x at increments of say 0.1 (1, 1.1, 1.2, 1.3, ...)
and . . . I have not yet sat down to work up a subroutine for e^x - thought about it a few times but time seems to be too short at the moment . . . .
 

DirtBiker

New Member
Your approach is correct and is very much appreciated. I've programmed transcendental functions in PICAXE basic before, and while it is sometimes a strict requirement, it does take a lot of variable space and processing. But sometimes it just can't be avoided. Time and technology will relieve this dilemma, not doubt, when the chips PICAXE are based on have sufficient speed and resources to give us floating point math and transcendental functions.

But, perhaps the OP does not need exactly X^1.304. b10 = 13 * b3 * b3 / 249 gives a curve of the type he is talking about and might just work. it's simple, quick and uses minimal resources. Just a suggestion.
 

DirtBiker

New Member
Another way to look a ln(x) is like this:

ln(x)=2y(1+(1/3)y^2+(1/5)y^4+(1/7)y^6+...) where y=(x-1)/(x+1).

I have used this before. You can actually get better than 2 digits of accuracy by truncating after 4 or 5 terms and adjusting the constant of the last term, depending on the range of values for x you need to deal with. It removes the requirement to store or do all the factorial calculations and y and y^2 can both be calculated just once per call which makes it fairly simple to implement.

But I still think b10 = 13 * b3 * b3 / 249 is worth a try.
 
Top