[英]Creating a graphing of a mathematical function in Javascript that dynamically changes as variables change in the function
I am trying to create a javascript app that allow users to see a graph of a function with multiple variables.For my program, I want the use the variable S as the dependent variable for the graph and use all other variables as constant parameters. 我正在尝试创建一个JavaScript应用,以允许用户查看具有多个变量的函数的图。对于我的程序,我希望使用变量S作为图的因变量,并希望将所有其他变量用作常量参数。 However, I want the user to be able to change value of the parameters and I want the graph to be updated according to those changes. 但是,我希望用户能够更改参数值,并且希望根据这些更改来更新图形。 I tried to do that with my program, but it seems that the graph would not even show up. 我试图用我的程序来做到这一点,但似乎该图甚至不会显示出来。 How would I address this issue? 我将如何解决这个问题? Also, should I use another plotting script? 另外,我应该使用其他绘图脚本吗? Down below is my code. 下面是我的代码。 For all those interested in finance, you may notice that I am trying to plot the value of a call option with the Black-Scholes formula. 对于所有对金融感兴趣的人,您可能会注意到,我试图用Black-Scholes公式来绘制看涨期权的价值。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="http://www.jqplot.com/tests/../src/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="http://www.jqplot.com/tests/../src/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="http://distributome.googlecode.com/svn-history/r375/trunk/js/calc/distributions.js"></script>
<link rel="stylesheet" type="text/css" href="http://www.jqplot.com/tests/../src/jquery.jqplot.min.css">
<script type="text/javascript" src="http://www.jqplot.com/tests/../src/plugins/jqplot.canvasTextRenderer.min.js"></script>
<script type="text/javascript" src="http://www.jqplot.com/tests/../src/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
<script class="code" type="text/javascript">
function invoke () {
Sigma = quad.variance.value;
K = quad.strike_price.value;
r = quad.r_val.value;
div = quad.div_val.value;
graph ();
}
function answer(S) {
d1 = (Math.log( S / K )+(r - div + .5*Math.pow(Sigma, 2))*T)/(Sigma*Math.sqrt(T));
d2 = d1 - Sigma*Math.sqrt(T);
return S*Math.exp(-div*T)*stdNormalCDF(d1) - K*Math.exp(-r*T)*stdNormalCDF(d2);
};
function graph() {
var CallPoints = [];
for (var i=0; i<90; i+=0.1){
CallPoints.push([i, answer(i)]);
}
var plot1 = $.jqplot('chart1', [CallPoints], {
series:[{showMarker:false}],
axes:{
xaxis:{
label:'Stock Price'
},
yaxis:{
label:'Premium'
}
}
});
};
</script>
</head>
<body onload="invoke()">
<form name="quad" onchange="invoke()">
<input type="text" value=".30" name="variance" size="5" onchange="invoke()"> = Volatility
<br>
<input type="text" value="30" name="strike_price" size="5" onchange="invoke()"> = K
<br>
<input type="text" value=".05" name="r_val" size="5" onchange="invoke()" > = r
<br>
<input type="text" value=".05" name="div_val" size="5" onchange="invoke()"> = div
<br>
<input type="text" value="1" name="T_val" size="5" onchange="invoke()"> = T
</form>
<div id="chart1" style="height: 300px; width: 500px; position: relative;" class="jqplot-target">
</div>
</body>
</html>
T is not defined in invoke() function add line: 在invoke()函数添加行中未定义T:
T = quad.T_val.value; T =四倍T_val.value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.