jsFiddle --> http://jsfiddle.net/rVLN2/1/
我是菜鸟 - 我知道我的脚本效率低下,但我正在努力。我有一个对象数组设置,例如:
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},
...................................];
然后我有一个从 html 中提取值并根据表格插入数值的函数。
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 {}
}
});
现在这只是在“T”已知的情况下解决这些值。我正在尝试制作一个程序来确定输入了哪个值(“Psat”、“vg”、“vf”等),然后根据该值评估所有其他数字。按照我现在的设置方式,我基本上必须为 Psat、vg、vf、hg、hf 和 hfg 重复所有这些代码。这似乎非常低效。有没有办法将其缩减为一个平滑函数?.
. jsFiddle --> http://jsfiddle.net/rVLN2/1/
.