Y
Yun
Hi, Javascriptists!
I wrote a code with Javascript and I am annoyed by the member 'dydt'
called from the member 'rk2'. I can not get the value of this.rho,
with the error message that this.rho is undefined.
However when dydt is an inner function or outer function, the value of
rho can be correctly obtained.
I guess that it may be the enclosure problem but I could not find a
reasonable explanation about it.
If I insist to use the dydt as the member of methods, how should I do
in order to obtained the value of rho in the methods?
Thanks a lot in advance.
Here is the source code?
-----------------------------------------------------------------------------------------------
<html>
<head>
<title>Test</title>
</head>
<body>
<textarea id="results" cols="100" rows="10" style="clear:float">OK</
textarea>
<script>
function RK() {}
RK.prototype.rk2 = function(ynref, dydtref, t, dt) {
var gamma = 0.75; // Ralston's minimisation of error bounds
var alpha = 0.5 / gamma;
var beta = 1.0 - gamma;
var alphadt = alpha * dt;
var betadt = beta * dt;
var gammadt = gamma * dt;
var ny = ynref.length;
var ynp1 = [],
dydtn = [],
ynpalpha = [],
dydtnpalpha = [];
alert("rk2: "+ "t="+t + "ynref = "+ ynref);
dydtn = dydtref(t, ynref);
var i;
for (i = 0; i < ny; i++) {
ynpalpha = ynref + alphadt * dydtn;
}
dydtnpalpha = dydtref(t + alphadt, ynpalpha);
for (i = 0; i < ny; i++) {
ynp1 = ynref + betadt * dydtn + gammadt * dydtnpalpha
;
}
return [t + dt, ynp1];
};
/////////////////////////////////////////////////////////////////////////////////////////////
var methods = {
n: 5,
//final time to terminate the calculation.
t_final: 0.5,
set_dt: function() {
return this.dt = this.t_final / this.n;
},
//thermal properties
rho: 100,
//
get_dt: function() {
return this.dt;
},
// initial condition
t: 0,
y: [0],
// time interval
dt: 1,
//????????????????????????????????????????????????????
// I could not get the members in dydt when dydt is called from
rk2.
dydt : function(t, y) {
var dydt_arr = [];
//?????????????????????????????????????????????????????????????????????????????
alert("dydt: " + "this.rho = " + this.rho ); // why here
this.rho is undefined?
//?????????????????????????????????????????????????????????????????????????????
dydt_arr[0] = 10 - 10 * y[0];
return dydt_arr;
},
//////////////////////////////////////////
rk2: function() {
alert("rk2: " + "this.dt="+ this.dt);
var i;
var n = this.n;
var data = "";
var tmp = [];
// set dt
this.set_dt();
//////////////////////////////////////////
var that = this;
var dydt = function(t, y) {
var dydt_arr = [];
//?????????????????????????????????????????????????????????????????????????????
alert("dydt: " + "methods.rho = " + that.rho ); // OK
//?????????????????????????????????????????????????????????????????????????????
dydt_arr[0] = 10 - 10 * y[0];
return dydt_arr;
};
var rk = new RK();
for (i = 1; i <= n; i++) {
// tmp = rk.rk2(this.y, dydt, this.t, this.dt ); //OK
// the following is wrong?
tmp = rk.rk2(this.y, this.dydt, this.t, this.dt ); //
wrong
this.t = tmp[0];
this.y = tmp[1];
data += this.t + "\t" + "\t" + this.y[0] + "\n";
}
document.getElementById("results").value = data;
alert(data);
}
};
methods.rk2();
</script>
</body>
</html>
I wrote a code with Javascript and I am annoyed by the member 'dydt'
called from the member 'rk2'. I can not get the value of this.rho,
with the error message that this.rho is undefined.
However when dydt is an inner function or outer function, the value of
rho can be correctly obtained.
I guess that it may be the enclosure problem but I could not find a
reasonable explanation about it.
If I insist to use the dydt as the member of methods, how should I do
in order to obtained the value of rho in the methods?
Thanks a lot in advance.
Here is the source code?
-----------------------------------------------------------------------------------------------
<html>
<head>
<title>Test</title>
</head>
<body>
<textarea id="results" cols="100" rows="10" style="clear:float">OK</
textarea>
<script>
function RK() {}
RK.prototype.rk2 = function(ynref, dydtref, t, dt) {
var gamma = 0.75; // Ralston's minimisation of error bounds
var alpha = 0.5 / gamma;
var beta = 1.0 - gamma;
var alphadt = alpha * dt;
var betadt = beta * dt;
var gammadt = gamma * dt;
var ny = ynref.length;
var ynp1 = [],
dydtn = [],
ynpalpha = [],
dydtnpalpha = [];
alert("rk2: "+ "t="+t + "ynref = "+ ynref);
dydtn = dydtref(t, ynref);
var i;
for (i = 0; i < ny; i++) {
ynpalpha = ynref + alphadt * dydtn;
}
dydtnpalpha = dydtref(t + alphadt, ynpalpha);
for (i = 0; i < ny; i++) {
ynp1 = ynref + betadt * dydtn + gammadt * dydtnpalpha
;
}
return [t + dt, ynp1];
};
/////////////////////////////////////////////////////////////////////////////////////////////
var methods = {
n: 5,
//final time to terminate the calculation.
t_final: 0.5,
set_dt: function() {
return this.dt = this.t_final / this.n;
},
//thermal properties
rho: 100,
//
get_dt: function() {
return this.dt;
},
// initial condition
t: 0,
y: [0],
// time interval
dt: 1,
//????????????????????????????????????????????????????
// I could not get the members in dydt when dydt is called from
rk2.
dydt : function(t, y) {
var dydt_arr = [];
//?????????????????????????????????????????????????????????????????????????????
alert("dydt: " + "this.rho = " + this.rho ); // why here
this.rho is undefined?
//?????????????????????????????????????????????????????????????????????????????
dydt_arr[0] = 10 - 10 * y[0];
return dydt_arr;
},
//////////////////////////////////////////
rk2: function() {
alert("rk2: " + "this.dt="+ this.dt);
var i;
var n = this.n;
var data = "";
var tmp = [];
// set dt
this.set_dt();
//////////////////////////////////////////
var that = this;
var dydt = function(t, y) {
var dydt_arr = [];
//?????????????????????????????????????????????????????????????????????????????
alert("dydt: " + "methods.rho = " + that.rho ); // OK
//?????????????????????????????????????????????????????????????????????????????
dydt_arr[0] = 10 - 10 * y[0];
return dydt_arr;
};
var rk = new RK();
for (i = 1; i <= n; i++) {
// tmp = rk.rk2(this.y, dydt, this.t, this.dt ); //OK
// the following is wrong?
tmp = rk.rk2(this.y, this.dydt, this.t, this.dt ); //
wrong
this.t = tmp[0];
this.y = tmp[1];
data += this.t + "\t" + "\t" + this.y[0] + "\n";
}
document.getElementById("results").value = data;
alert(data);
}
};
methods.rk2();
</script>
</body>
</html>