# How do I map a nonlinear equation

Hi Im trying to map a nonlinear function but do not have the slightest idea of how to do it. I plugged the data below in excel and plotted the graph and came up with not the best function but something like in the ball park

y = -31.84ln(x) + 267.47

The left column is temp and the right column is resistance. If you take a look you will notice the resistance is not linear.

-40 100865 -35 72437 -30 52594 -25 38583 -20 28582 -15 21371 -10 16120 -5 12261 0 9399 5 7263 10 5658 15 4441 20 3511 25 2795 30 2240 35 1806 40 1465 45 1195 50 980 55 809 60 671 65 559 70 469 75 395 80 334 85 283 90 241.8 95 207.1 100 178 105 153.6 110 133.1 115 115.7 120 110.9 125 88.3 130 77.5 135 68.3 140 60.3 145 53.4 150 47.5

I think your best bet is to get the input sorted out. Which temperature sensor are you using? Got a schematic for how it's connected? I presume it's a simple analogRead you're using?

Hi, that approx equation has a problem, you cannot take natural log of negative number. I tried it for the positives ones and it is nowhere approximate. Edit.. I did it back to front, approx does work but not exactly in step, not sure how close you want it to be.

You could make two arrays, one for x and the other for y values that you have. When you are given a x value you scan the x array and find the two values in the array either side of the given x, then use map function to do a linear approximation for y.

Just a suggestion, not sure how quick it would take to function, or what you want to do with the values.

If the sensor you are using is a NTC resistor, you should be able to find the spec or some info on equivalent equations.

Tom...... :relaxed:

Try here, they give an equation, I think yours is a 3K unit http://www.arroyoinstruments.com/manuals/ArroyoThermistorInstructions.pdf

You could use that table of values you posted and put it in an array in your sketch. Then, when you want to perform a conversion, scan through the array to find which pair of resistance values your latest reading falls between. Then use the map() function to interpolate between the two temperature values. This should hopefully be faster than performing the floating point maths of your equation, but will loose a little accuracy because the curve will be approximated by a series of lines.

Paul

If you are using a thermistor you can take resistance measurements at three known temperatures and use those points to calculate the Steinhart-Hart coefficients. Then you can use the coefficients and the Steinhart-Hart equation to calculate temperature from resistnce:

http://en.wikipedia.org/wiki/Steinhartâ€“Hart_equation

I'm sure there is an online calculator to give you the coefficients given three temperature/resistance pairs.

This is a job for MultiMap

Thank you everyone for all your suggestions. I have been trying vigorously to figure out the Steinhart-Hart coefficients with no success. I have also tried online calculators that have given me the equation but they do not work.

Anybody able to help me with figuring out how to setup the Steinhart-Hart equation?

I would go with PaulRB's suggestion. If you have all the values which will bracket your readings, then you can create an array in the Arduino and set up a lookup table...pretty simple.

plotting my points using the website http://www.rusefi.com/Steinhart-Hart.html provides me with this equation

1 / (0.0014561535647761783 + 0.00023388124440064398 * logR + 8.338807490987289e-8 * logR * logR * logR)

unfortunately it does not work and i must of tried it 100 times

When I use the extreme high and low values plus the middle value for 0C I get:

``````1 / (0.0014626876573909427 + 0.00023173143050064543 * lnR + 1.0232140848664025e-7 * lnR * lnR * lnR)
``````

NOTE that the webpage uses log() in the formula that it produces but when testing in Excel you must change log() to ln() - I've changed the equation to reflect this. The spotchecks that I've done of the formula agree with your data. [I hope this is my last edit and I have now got everything right!]

But also note that the webpage requires the temperature in degrees F. Did you convert from C to F?

You haven't explained why you are using this formula and how it specifically applies to arduino. What is it exactly that you are measuring? If you are going to measure the resistance with one of the A/D converters to get a corresponding temperature then you can only have 1024 possible values. You could precompute the temperature for each possible value and store it in an array in PROGMEM to save sram. Or store the values in an EEPROM.

Pete