jsFiddle --> http://jsfiddle.net/rVLN2/1/
I'm a noob - I know my script is inefficient but I'm working on it. I have an object array setup like:
var SatWaterMetric = [
{"T":0,"Psat":0.6117,"vf":0.001,"vg":206,"hf":0.001,"hfg":2500.9,"hg":2500.9},
{"T":5,"Psat":0.8725,"vf":0.001,"vg":147.03,"hf":21.02,"hfg":2489.1,"hg":2510.1},
{"T":10,"Psat":1.2281,"vf":0.001,"vg":106.32,"hf":42.022,"hfg":2477.2,"hg":2519.2},
...................................];
Then I have a function that pulls a value from the html and will interpolate the number value according to the tables.
function interpolate(myval, unit) {
if (unit == "T") {
for (var i=0;i<SatWaterMetric.length;i++) {
if (myval < SatWaterMetric[i].T) {
T_low = SatWaterMetric[i-2].T;
T_high = SatWaterMetric[i-1].T;
Psat_low = SatWaterMetric[i-2].Psat;
Psat_high = SatWaterMetric[i-1].Psat;
vf_low = SatWaterMetric[i-2].vf;
vf_high = SatWaterMetric[i-1].vf;
vg_low = SatWaterMetric[i-2].vg;
vg_high = SatWaterMetric[i-1].vg;
hf_low = SatWaterMetric[i-2].hf;
hf_high = SatWaterMetric[i-1].hf;
hfg_low = SatWaterMetric[i-2].hfg;
hfg_high = SatWaterMetric[i-1].hfg;
hg_low = SatWaterMetric[i-2].hg;
hg_high = SatWaterMetric[i-1].hg;
var factor = (myval - T_low) / (T_high - T_low);
Psatx = (1 * Psat_low) + ( 1 * factor * (Psat_high - Psat_low));
vfx = (1 * vf_low) + ( 1 * factor * (vf_high - vf_low));
vgx = (1 * vg_low) + ( 1 * factor * (vg_high - vg_low));
hfx = (1 * hf_low) + ( 1 * factor * (hf_high - hf_low));
hfgx = (1 * hfg_low) + ( 1 * factor * (hfg_high - hfg_low));
hgx = (1 * hg_low) + ( 1 * factor * (hg_high - hg_low));
Tx = myval;
break;
}
}
$('#txtpsat').val(Math.round(Psatx*100000)/100000);
$('#txtvf').val(Math.round(vfx*10000000)/10000000);
$('#txtvg').val(Math.round(vgx*100000)/100000);
$('#txthf').val(Math.round(hfx*10000)/10000);
$('#txthg').val(Math.round(hgx*100)/100);
$('#txthfg').val(Math.round(hfgx*100)/100);
} else if (unit == "Psat") {
//Repeat everything but for Psat
} else if (unit == "vf") {
//Repeat everything but for vf
} else {}
}
});
Now this is only to solve the values if "T" is known. I am trying to make a program that will determine which value was typed into ("Psat", "vg", "vf", etc...) and then evaluate all of the other numbers based on that value. The way I have this setup right now, I would essentially have to repeat all of that code for Psat, vg, vf, hg, hf, and hfg. It seems very inefficient. Is there a way to trim this down to one smooth function? .
. jsFiddle --> http://jsfiddle.net/rVLN2/1/
.
SatWaterMetric[i-2]
will beundefined
andSatWaterMetric[i-2].T
will be in a hell. It is not a thingT
is not known, your code just doesn't work. – zsong Sep 5 '13 at 19:27myval < SatWaterMetric[i].T
wherei
is less than 2. – James Montagne Sep 5 '13 at 19:29