It is not clear why you create S with linspace() but then overwrite S with the result of the calculation?
Possibly you mean the final line as a kind of equality that has to be satisfied, that for each left hand side value, you want to find the location on the right that most nearly equals it. If so then,
Scalc = Cp*log(T./293)-R*log(97.87./500)+9.2;
tempidx = interp1(S, 1:length(S), Scalc, 'nearest', NaN);
The locations at which the Scalc is outside of the linspace() will have a result of NaN, and the other locations will have the index of the T.
You could easily calculate exact temperatures instead of approximate. Algebraically,
S = Cp*log(t/293) - R * log(97.87/500) + 9.2 implies
S - 9.2 + R * log(97.87/500) = Cp * log(t/293) implies
(S - 9.2 + R * log(97.87/500)) / Cp = log(t/293) implies
exp((S - 9.2 + R * log(97.87/500)) / Cp) = t/293 implies
t = 293 * exp((S - 9.2 + R * log(97.87/500)) / Cp)
and that last line is also valid code.
Best Answer