Javascript: Solve a system of equations
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>");
});
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.)
精彩评论