I have an input file that consists of data in the form "Celsius,Resistance", for example one point is "5,12700" which stands for a temperature of 5 degrees Celsius, my resistance is 12700. This input file consists of multiple sets of data each on their individual line.
Here's a small part of the data:
-4,20080
-3,19060
-2,18100
-1,17190
0,16330
1,15520
2,14750
3,14030
4,13340
5,12700
6,12090
I am trying to enter a resistance value of my choice, and return a temperature that corresponds to the resistance I entered. However, the resistance I input might not be an exact data set in the input file so I'm trying to linearly interpolate to assure accurate results where my inputted resistance value falls between two data points.
I'm unsure on how to get the two data points which fall both above and below my inputted resistance so I can linearly interpolate both sets of data as well as my inputted resistance to return the corresponding degrees Celsius. My linear interpolation formula I'm using is
Celsius = resis1 + (resistance - temp1) * ((resis2 - resis1) / (temp2 - temp1))
with temp1,resis1 being the data point below and temp2,resis2 being the data point above my inputted resistance.
Anything helps! Thanks for taking the time to help!
Here is a rough start to what I have.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
int resistance, celsius, temp1, temp2, resis1, resis2;
ifstream data;
data.open("e:/TempResisData.txt");
if (data.fail())
cout << "Problem opening input file";
do
{
cout << "Enter your resistance (enter -1 to quit): ";
cin >> resistance;
// Here is where I am stuck. I am unsure on how to search through my input file to obtain the boundary points that are above and below my inputted resistance
celsius = resis1 + (resistance - temp1) * ((resis2 - resis1) / (temp2 - temp1));
cout << "For a resistance of " << resistance << " the corresponding temperature is ";
cout << celsius << " degrees Celcius.";
} while (resistance != -1);
data.close();
system("pause");
return 0;
}
|
Also, here is the project I am attempting to complete.
I will provide you with 6 resistance values and you will calculate the corresponding temperature values (in Celsius). You will need to linearly interpolate, as needed, to assure accurate results where the given resistance
may fall between two data points.
You are to reject any resistance values that are
less than/greater than the high and low values in the data table.
Your task is to read in the thermistor data (unknown number of data points . . .) and prompt for the 5 test resistance values that I provide. For each resistance value you will produce a temperature
value.