Even if we stick to arithmetic, not all functions are invertable. As a quick example, I recently implemented the Callendar-Van Dusen Equation for linearizing the output of a platinum RTD thermometer. The equation changes slightly depending on if the temperature is above or below 0C, so there isn't a simple inverse function. Furthermore the equation gives the resistance of the thermometer based on temperature (because they calibrate probes with a known temperature and measure resistance), but I needed the inverse of that (Temp given resistance). Because there was no analytical solution to the inverse of my equation I was forced to use an approximation technique (others mentioned scipy.optimize
). I chose Newton's method because it was easy to solve for the derivative of both equations (for above and below 0C). The code is in Lua rather than python, but the example should still be understandable
local _CVD_root_finder = function(r0, R)
--The Callendar Van Dusen Equation for Platinum Based RTD Thermometers
--Newtons method root finder
local T = 0
local a, b, c = max31865._A, max31865._B , max31865._C
local i = max31865._root_find_iterations
for _ = 1,i do
if T < 0 then
local r = r0 * (1 + a*T + b*T*T + c*(T - 100)*T*T*T) - R
local slope = r0 * (a + 2*b*T + 4*c*(T - 75)*T*T)
T = T - r/slope
else
local r = r0 * (1 + a*T + b*T*T) - R
local slope = r0 * (a + 2*b*T)
T = T - r/slope
end
end
return T
end