Javascript:求解方程组
我正在使用维基百科上的“更接近的近似值”制作露点计算器。
如果用户输入任意两个变量,我希望能够计算露点。
有没有一种简单的方法可以做到这一点,而不是使用大量的 if 语句?
更具体地说:如果我想使用湿球温度而不是相对湿度怎么办?我是否必须创建一个新函数或使用 if 语句来排除一组变量?
目前我正在使用温度和相对湿度:
$('#calculate').click(function(){
//Get Temp
var T = parseInt($('#val1').val());
//Get RH
var RH = parseInt($('#val2').val());
//Get es and ex
var es = 6.112*Math.exp(17.76*T/(T+243.5));
var ex = (RH*es)/100;
//Calculate Dew Point
var Tdp = (243.5*Math.log(ex/6.112))/(17.67-Math.log(ex/6.112));
$('#output').append("<p>Dew Point"+Tdp+"</p>");
});
I'm making a dew point calculator using the "closer approximation" on wikipedia.
I want to be able to calculate the dew point if the user enters any two variables.
Is there an easy way to do this rather than having a lot of if-statements?
More specifically: What if I wanted to use the wet-bulb temperature instead of the relative humidity? Would I have to make a new function or use an if-statement to exclude a set of variables?
Currently I'm using the temperature and relative humidity:
$('#calculate').click(function(){
//Get Temp
var T = parseInt($('#val1').val());
//Get RH
var RH = parseInt($('#val2').val());
//Get es and ex
var es = 6.112*Math.exp(17.76*T/(T+243.5));
var ex = (RH*es)/100;
//Calculate Dew Point
var Tdp = (243.5*Math.log(ex/6.112))/(17.67-Math.log(ex/6.112));
$('#output').append("<p>Dew Point"+Tdp+"</p>");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅供参考,求解非线性方程组通常是一个难题。尽你所能避免这种情况。
如果可以使用多对值来计算答案,我通常的方法是使用给定的一对值来计算一对规范值,然后将其用于进行实际计算。此外,由于必须对输入进行排序以找出所提供的内容以便进行 DWIM 会变得很混乱,因此为我接受的每对输入设置单独的函数可能是有意义的。 (或者它可能不取决于程序中的控制流程,您比我更清楚这一点。)
FYI solving systems of nonlinear equations is generally a hard problem. Do whatever you can to avoid that.
My usual approach if multiple pairs of values can be used to calculate an answer is to use the pair of values I'm given to calculate a canonical pair of values, which then is used to do the real calculation. Furthermore since it gets messy to have to sort through input to figure out what you have been given so you can DWIM, it might make sense to have separate functions for each pair of inputs that I'll accept. (Or it might not depending on the flow of control in your program, you know that better than I do.)