我有4个变量的函数,让我们说$f(x,t,w,n)$
,函数$g(x,n)$
定义为
g(x,n)=\int_a^b\int_c^d f(x,t,w,n) dt dw
其中$a$, $b$, $c$, $d$
给定常量,并且不能以闭合形式显式计算积分。然后,$h(x,n)$
由
h(x,n)=\ln\frac{g(x,n)}{g(-x,n)}
我希望$y=h(x,n)$
将$x$
作为$n$
的函数用于同一情节中$f(x,t,w,n)$
的不同值。我怎样才能做到这一点。如果有帮助,f(x,t,w,n)=\exp{-\frac{x^2+tw+wx}{n}}+\exp{-\frac{t^2+tx^2-2tx}{2n}}
具有以下形式
//....
var crystalValues = {};
crystalValues[1] = Math.floor(Math.random()*12+1);
crystalValues[2] = Math.floor(Math.random()*12+1);
crystalValues[3] = Math.floor(Math.random()*12+1);
crystalValues[4] = Math.floor(Math.random()*12+1);
//.... more code here
function getCrystalHandler(crystalKey) {
return function() {
userTotal = userTotal + crystalValues[crystalKey];
console.log("New userTotal " + userTotal);
$("#score").text(userTotal);
if (userTotal === random) {
winner()
}
else if (userTotal > random) {
loser()
}
}
}
$("#image1").on("click", getCrystalHandler(1));
$("#image2").on("click", getCrystalHandler(2));
$("#image3").on("click", getCrystalHandler(3));
$("#image4").on("click", getCrystalHandler(4));
答案 0 :(得分:1)
我认为这可能会做你想要的。我将f,g和h指定为匿名函数,并使用quad2d估计double积分的值。
%% Input bounds
a = 0;
b = 1;
c = 0;
d = 2;
%% Specify functions
% vectorize function as a prerequisite to using in quad2d
f = @(x,t,w,n) exp( -(x.^2 + t.*w + w.*x)./n) + exp(-(t.^2 + t.*x.^2 - 2.*t.*x)./(2.*n));
% keeps x,n fixed in function call to f(...), varies a < t < b; c < w < d
g = @(x,n) quad2d(@(t,w) f(x, t, w, n), a, b, c, d);
% wrap functions into h
h = @(x,n) log(g(x,n)/g(-x,n));
%%
figure();
hold on % keep lines
x_range = linspace(-1,1);
for n = 1:5
plotMe = zeros(1, length(x_range));
for iter = 1:length(x_range)
plotMe(iter) = h(x_range(iter), n);
end
lineHandle(n) = plot(x_range, plotMe);
end
legend(lineHandle, {
['N: ', num2str(1)],...
['N: ', num2str(2)],...
['N: ', num2str(3)],...
['N: ', num2str(4)],...
['N: ', num2str(5)]...
}...
)