0

I'm using a thermistor to measure temperature, and have defined a function using datasheet function which gives temperature as a function of measured resistance.

Recip610[R_] := 
 0.0011279 + (2.3429 Log[R])/10^4 + (8.7298 Log[R]^3)/10^8 (* This is the datasheet function, which gives the reciprocal of absolute temperature *)

temp[R_] := 1/Recip610[R] - 273.15 (* This gives temp in degrees C *)

FindRoot can easily numerically solve this, finding a resistance for any given temperature.

r610[T_] := Values[FindRoot[temp[R] == T, {R, 10000}]][[1]]  (* This function works. *)

Next, I am using the thermistor in a resistive voltage divider circuit, and measuring the voltage across the thermistor. The equation for the voltage divider is similarly straightforward. I define a function for this voltage, and it evaluates to a temperature.

v610[T_] := (5 r610[T])/(200000 + r610[T])

I can even plot this function. But when I try to use FindRoot a second time to give me the temperature for any given measured value of the voltage, I get a list of errors:

FindRoot[v610[T] - .3, {T, 20}]

FindRoot::nlnum: The function value {25.0016 -1. T} is not a list of numbers with dimensions {1} at {R} = {10000.}.

Values::invrl: The argument FindRoot[temp[R]==T,{R,10000}] is not a valid Association or a list of rules.

FindRoot::nlnum: The function value {25.0016 -1. T} is not a list of numbers with dimensions {1} at {R} = {10000.}.

FindRoot::nlnum: The function value {25.0016 -1. T} is not a list of numbers with dimensions {1} at {R} = {10000.}.

General::stop: Further output of FindRoot::nlnum will be suppressed during this calculation.

Values::invrl: The argument FindRoot[temp[R]==T,{R,10000}] is not a valid Association or a list of rules.

This puzzles me, because the function v610[T], as defined above, evaluates correctly. Yet when I ask FindRoot to find what T evaluates to, say, 0.3, it fails. (But, by trial and error, I found that v610[19.5231] evaluates to 0.3.)

Why does FindRoot fail to give a solution, when the function I am asking it find the root of both evaluates correctly and even plots correctly?

Here is a plot of v610[T], evaluated as T goes from 0 to 50 degrees C.  Over that temperature range, the voltage drops from about 0.7 V down to just over 0.1 V.

Plot[v610[T], {T, 0, 50}]

I'd appreciate two different answers to this question: first, what is code that will work? (voltage in, temperature out, for the circuit and the thermistor function that I have). Second, I'd like to understand the specific reason that FindRoot is not able to find a solution when the function I'm asking it to solve appears to be well-behaved, i.e. it evaluates normally and can be plotted. Is the problem that FindRoot can't be nested?

v610[T_] := 5*(r610[T]/(200000 + r610[T])) 
r610[T_] := Values[FindRoot[temp[R] == T, {R, 10000}]][[1]] 
temp[R_] := 1/Recip610[R] - 273.15 
Recip610[R_] := 0.0011279 + (2.3429*Log[R])/10^4 + 
(8.7298*Log[R]^3)/10^8
v610[T_] := 5*(r610[T]/(200000 + r610[T])) 
r610[T_] := Values[FindRoot[temp[R] == T, {R, 10000}]][[1]] 
temp[R_] := 1/Recip610[R] - 273.15 
Recip610[R_] := 0.0011279 + (2.3429*Log[R])/10^4 + 
(8.7298*Log[R]^3)/10^8

(Edited to add code window)

Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67
btiemann
  • 1
  • 1
  • Hi! You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is useful for learning how to format your questions and answers. You may also find this meta Q&A helpful – Michael E2 Dec 24 '20 at 19:13
  • First, r610[20] does not return a number (it returns a list), which you probably want to do. (?) Second, you probably need ?NumericQ: ClearAll[v610]; v610[T_?NumericQ] := ... – Michael E2 Dec 24 '20 at 19:20
  • Maybe for r610[T_?NumericQ] := .. too – Michael E2 Dec 24 '20 at 19:30
  • I included Values[ ... ][[1]] in the definition of r610 to pick out the first item of the list, so what is returned is actually just a numerical value, not a list. r610 does plot. (Which I don't think it would do if it evaluated to lists.) – btiemann Dec 24 '20 at 19:36
  • When I evaluate r610[20], I get 12493.5 – btiemann Dec 24 '20 at 19:37
  • In[827]:= r610[20]

    Out[827]= 12493.5

    – btiemann Dec 24 '20 at 19:37
  • `ClearAll[r610, v610];

    r610[T_?NumericQ] := First@Values[FindRoot[temp[R] == T, {R, 10000}]];

    v610[T_?NumericQ] := (5 r610[T])/(200000 + r610[T]);` -- the explanation is in the link I posted earlier, along with the foregoing suggestion.

    – Michael E2 Dec 24 '20 at 20:29
  • r610 would plot if it evaluated to a list. Try it: ff[x_] := {x^2}; Plot[ff[x], {x, -1, 2}]. Your [[1]] was formatted as a link in the initial post. It didn't copy properly, if the part 1 was what was intended. – Michael E2 Dec 24 '20 at 20:32
  • I tried the ?NumericQ in the v610 definition, that didn't work, but I then tried it the r610 definition, as you actually posted, and that did work. Thanks! – btiemann Dec 24 '20 at 20:35
  • You're welcome! – Michael E2 Dec 24 '20 at 20:37

0 Answers0