I would like the following code to do a calculation based on the results of a for(var i...etc loop. I cannot 'feed ' the array results into the calculation and keep on getting NaN. Can someone help please.
Thank you - function doCalculation () {
-
for (var i=0; i<=10; i++) {
-
var x=[i];
-
var y=[x*Math.exp(3/8).toFixed(2)];
-
document.writeln('<p>x= '+x +' y= ' +y +'</p>');//this gives results but the next won't compute
-
var G= (((x[9]-x[8])*(y[9]-y[8]))+((x[8]-x[7])*(y[8]-y[7]))+((x[7]-x[6])*(y[7]-y[6]))+((x[6]-x[5])*(y[6]-y[5]))+((x[5]-x[4])*(y[5]-y[4]))+((x[4]-x[3])*(y[4]-y[3]))+((x[3]-x[2])*(y[3]-y[2]))+((x[2]-x[1])*(y[2]-y[1]))+((x[1]-x[0])*(y[1]-y[0])));
-
document.writeln('G = '+G);
-
}
-
}
-
8 1427
Hi Peter.
What is it you are trying to accomplish here? You haven't declared any arrays. Are you trying to fill an array with values and then do a calculation?
Here is an example of an array and looping through it to print. -
var names = new Array();
-
names[0] = "Lisa";
-
names[1] = "Bart";
-
names[2] = "Maggie";
-
-
for (var i=0;i<3; i++)
-
{
-
document.write(names[i]);
-
}
-
//This will print Lisa Bart Maggie
-
I am trying to use the values generated by the loop in the calculation that subtracts , in order, the lower value from the higher the precedes it in the list for x and y, multiplies these two and then adds the next product of the next two pairs as described in the line after the document-writeln. Help with getting these values correctly into one or two arrays ( x and y array) and then correctly identifying which is x10, x 9 and so on is what I cannot get my head around. I hope that this is clear. I don't seem to be able to describe my problem very well!
Okay, I think this is what you are looking for. -
function doCalculation ()
-
{
-
//Declare Array Variables
-
var x = new Array();
-
var y = new Array();
-
-
//Loop from 1 to 10 to fill x and y
-
for (var i=0; i<=10; i++)
-
{
-
x[i] = i;
-
y[i]=x[i]*Math.exp(3/8).toFixed(2);
-
document.writeln('<p>x= '+x +' y= ' +y +'</p>');//this gives results but the next won't compute
-
}
-
-
//Use the filled arrays in calculation
-
var G= (((x[9]-x[8])*(y[9]-y[8]))+((x[8]-x[7])*(y[8]-y[7]))+((x[7]-x[6])*(y[7]-y[6]))+((x[6]-x[5])*(y[6]-y[5]))+((x[5]-x[4])*(y[5]-y[4]))+((x[4]-x[3])*(y[4]-y[3]))+((x[3]-x[2])*(y[3]-y[2]))+((x[2]-x[1])*(y[2]-y[1]))+((x[1]-x[0])*(y[1]-y[0])));
-
document.writeln('G = '+G);
-
}
-
thank you so much. I will try this with the calculation I am actually trying to do and see whether I can make it work. Once again it is to get the x and y values into the subsequent calculations as in this example. I'll give it a go, but will appreciate your help, once again.
Thank you once again!! - <script type="text/javascript">
-
-
var FPF=[];
-
var TPF =[];
-
var xArray=[];
-
var yArray=[];
-
var x=2;
-
//parseFloat($('#display').val());//1;//slider_form.elements["display"].value;//slider_form.elements.display.value;// slider value
-
var LT = parseFloat(-3 + (0.8571 *x));//changes slider value to the left tail value 'with disease'
-
var RT= 3;//value for right tail of 'without disease'
-
var c= parseFloat(1.739 + (0.6116*x)+(0.01297*x*Math.exp(2))-(0.01142*x*Math.exp(3))-(0.004572*x*Math.exp(4))); //intersect between two curves
-
-
//from Stephen Whitney application 25yearsofprogramming.com 'probability.js'
-
function NormalDensityZx(x, Mean, StdDev)
-
{
-
var a = x - Mean;
-
return Math.exp(-(a * a) / (2 * StdDev * StdDev)) / (Math.sqrt(2 * Math.PI) * StdDev);
-
}
-
-
//----------------------------------------------------------------------------------------------
-
// Calculates Q(x), the right tail area under the Standard Normal Curve.
-
function StandardNormalQx(x)
-
{
-
x = i;
-
if(x === 0) // no approximation necessary for 0
-
return 0.50;
-
-
var t1,t2,t3,t4,t5,qx;
-
var negative = false;
-
if(x < 0)
-
{
-
x = -x;
-
negative = true;
-
}
-
t1 = 1/(1 + (0.2316419 * x));
-
t2 = t1 * t1;
-
t3 = t2 * t1;
-
t4 = t3 * t1;
-
t5 = t4 * t1;
-
qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) +
-
(1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
-
if(negative == true)
-
qx = 1 - qx;
-
return qx;
-
}
-
-
-
//----------------------------------------------------------------------------------------------
-
// Calculates P(x), the left tail area under the Standard Normal Curve, which is 1 - Q(x).
-
function StandardNormalPx(x)
-
{
-
-
-
x = -2.8+(i-LT);
-
if(x === 0) // no approximation necessary for 0
-
return 0.50;
-
-
var t1,t2,t3,t4,t5,qx;
-
var negative = false;
-
if(x < 0)
-
{
-
x = -x;
-
negative = true;
-
}
-
t1 = 1/(1 + (0.2316419 * x));
-
t2 = t1 * t1;
-
t3 = t2 * t1;
-
t4 = t3 * t1;
-
t5 = t4 * t1;
-
qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) +
-
(1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
-
if(negative == true)
-
qx = 1 - qx;
-
-
-
return 1 - qx;
-
-
}
-
//end Stephen Whitney probability.js
-
-
//calculate points for ROC plot from -3 to +3 at unit intervals
-
for (var i =-3; i<3.1; i+=0.2 ){
-
-
if (i<LT){
-
-
PQAQX = (StandardNormalQx(x).toFixed(2)); //AUC false positives based on AUC = 1
-
FPF =parseFloat(StandardNormalQx(x).toFixed(2));
-
TPF = 1;}
-
-
else if (i>LT && i<RT){
-
-
-
PQAQX= (StandardNormalQx(x).toFixed(2));
-
PQAPX= (StandardNormalPx(x).toFixed(2));
-
-
TPF = (1-StandardNormalPx(x).toFixed(2));
-
FPF = parseFloat(StandardNormalQx(x).toFixed(2));
-
}
-
-
-
else if (i >3.01){
-
-
PQAPX= (StandardNormalPx(x).toFixed(2));
-
TPF = (1-StandardNormalPx(x).toFixed(2));
-
FPF = 1;
-
}
-
var xArray=[FPF.toFixed(2)];
-
var yArray=[TPF.toFixed(2)];
-
-
xArray.push([FPF]);
-
yArray.push([TPF]);
-
document.writeln('x: '+(xArray[0])+', y: '+(yArray[0])+'<BR>');
-
G1=1-((xArray[0]-xArray[1])*(yArray[0]-yArray[1])+(xArray[1]-xArray[2])*(yArray[1]-yArray[2])+(xArray[2]-xArray[3])*(yArray[2]-yArray[3])+(xArray[3]-xArray[4])*(yArray[3]-yArray[4])+(xArray[4]-xArray[5])*(yArray[4]-yArray[5])+(xArray[5]-xArray[6])*(yArray[5]-yArray[6])+(xArray[6]-xArray[7])*(yArray[6]-yArray[7])+(xArray[7]-xArray[8])*(yArray[7]-yArray[8])+(xArray[8]-xArray[9])*(yArray[8]-yArray[9])+(xArray[9]-xArray[10])*(yArray[9]-yArray[10])+(xArray[10]-xArray[11])*(yArray[10]-yArray[11])+(xArray[11]-xArray[12])*(yArray[11]-yArray[12])+(xArray[12]-xArray[13])*(yArray[12]-yArray[13])+(xArray[13]-xArray[14])*(yArray[13]-yArray[14])+(xArray[15]-xArray[16])*(yArray[15]-yArray[16])+(xArray[16]-xArray[17])*(yArray[16]-yArray[17])+(xArray[18]-xArray[19])*(yArray[18]-yArray[19])+(xArray[19]-xArray[20])*(yArray[19]-yArray[20])+(xArray[20]-xArray[21])*(yArray[20]-yArray[21])+(xArray[21]-xArray[22])*(yArray[21]-yArray[22])+(xArray[22]-xArray[23])*(yArray[22]-yArray[23])+(xArray[23]-xArray[24])*(yArray[23]-yArray[24])+(xArray[24]-xArray[25])*(yArray[24]-yArray[25])+(xArray[25]-xArray[26])*(yArray[25]-yArray[26])+(xArray[26]-xArray[27])*(yArray[26]-yArray[27])+(xArray[27]-xArray[28])*(yArray[27]-yArray[28])+(xArray[28]-xArray[29])*(yArray[28]-yArray[29])+(xArray[29]-xArray[30])*(yArray[29]-yArray[30]));
-
var AUC= ((G1 + 1)/2);
-
document.writeln('area under ROC curve ='+ AUC);
-
}
-
//calculate area under the curve
-
//take x and y values from function and do AUC calculation
-
//var G1= 1-Math.sum((x1-x2)*(y1-y2)+...); this function does not exist! description only
-
var AUC= (G1 + 1)/2;
-
//var d=AUC-0.5;
-
-
</script>
-
I see a few problems.
Your for loop will never enter into this if block.
You increment your for loop by 0.2 and terminate when i < 3.1. i will equal 3.0 on its last iteration. -
else if (i >3.01){
-
-
PQAPX= (StandardNormalPx(x).toFixed(2));
-
TPF = (1-StandardNormalPx(x).toFixed(2));
-
FPF = 1;
-
}
-
The following lines in your for loop means that every iteration of the for loop you are creating an array variable with a single index and a single value. You then add FPF to xArray and TPF to yArray. This means both of these arrays never have more than 2 values. -
var xArray=[FPF.toFixed(2)];
-
var yArray=[TPF.toFixed(2)];
-
-
xArray.push([FPF]);
-
yArray.push([TPF]);
-
Now when you go to output all of this and attempt to do your calculation it will fail because you trying to access an index of the array that doesn't exist.
I rearranged things for you once again. -
<script type="text/javascript">
-
-
var FPF=[];
-
var TPF =[];
-
var xArray=[];
-
var yArray=[];
-
var x=2;
-
//parseFloat($('#display').val());//1;//slider_form.elements["display"].value;//slider_form.elements.display.value;// slider value
-
var LT = parseFloat(-3 + (0.8571 *x));//changes slider value to the left tail value 'with disease'
-
var RT= 3;//value for right tail of 'without disease'
-
var c= parseFloat(1.739 + (0.6116*x)+(0.01297*x*Math.exp(2))-(0.01142*x*Math.exp(3))-(0.004572*x*Math.exp(4))); //intersect between two curves
-
-
//from Stephen Whitney application 25yearsofprogramming.com 'probability.js'
-
function NormalDensityZx(x, Mean, StdDev)
-
{
-
var a = x - Mean;
-
return Math.exp(-(a * a) / (2 * StdDev * StdDev)) / (Math.sqrt(2 * Math.PI) * StdDev);
-
}
-
-
//----------------------------------------------------------------------------------------------
-
// Calculates Q(x), the right tail area under the Standard Normal Curve.
-
function StandardNormalQx(x)
-
{
-
x = i;
-
if(x === 0) // no approximation necessary for 0
-
return 0.50;
-
-
var t1,t2,t3,t4,t5,qx;
-
var negative = false;
-
if(x < 0)
-
{
-
x = -x;
-
negative = true;
-
}
-
t1 = 1/(1 + (0.2316419 * x));
-
t2 = t1 * t1;
-
t3 = t2 * t1;
-
t4 = t3 * t1;
-
t5 = t4 * t1;
-
qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) +
-
(1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
-
if(negative == true)
-
qx = 1 - qx;
-
return qx;
-
}
-
-
-
//----------------------------------------------------------------------------------------------
-
// Calculates P(x), the left tail area under the Standard Normal Curve, which is 1 - Q(x).
-
function StandardNormalPx(x)
-
{
-
-
-
x = -2.8+(i-LT);
-
if(x === 0) // no approximation necessary for 0
-
return 0.50;
-
-
var t1,t2,t3,t4,t5,qx;
-
var negative = false;
-
if(x < 0)
-
{
-
x = -x;
-
negative = true;
-
}
-
t1 = 1/(1 + (0.2316419 * x));
-
t2 = t1 * t1;
-
t3 = t2 * t1;
-
t4 = t3 * t1;
-
t5 = t4 * t1;
-
qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) +
-
(1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
-
if(negative == true)
-
qx = 1 - qx;
-
-
-
return 1 - qx;
-
-
}
-
//end Stephen Whitney probability.js
-
-
-
var xArray=[];
-
var yArray=[];
-
-
//calculate points for ROC plot from -3 to +3 at unit intervals
-
for (var i =-3; i<3.1; i+=0.2 ){
-
-
if (i<LT){
-
-
PQAQX = (StandardNormalQx(x).toFixed(2)); //AUC false positives based on AUC = 1
-
FPF =parseFloat(StandardNormalQx(x).toFixed(2));
-
TPF = 1;
-
}
-
-
else if (i>LT && i<RT){
-
-
-
PQAQX= (StandardNormalQx(x).toFixed(2));
-
PQAPX= (StandardNormalPx(x).toFixed(2));
-
-
TPF = (1-StandardNormalPx(x).toFixed(2));
-
FPF = parseFloat(StandardNormalQx(x).toFixed(2));
-
}
-
-
-
else if (i >3.01){
-
-
PQAPX= (StandardNormalPx(x).toFixed(2));
-
TPF = (1-StandardNormalPx(x).toFixed(2));
-
FPF = 1;
-
}
-
-
-
xArray.push(FPF);
-
yArray.push(TPF);
-
-
}
-
//calculate area under the curve
-
//take x and y values from function and do AUC calculation
-
//var G1= 1-Math.sum((x1-x2)*(y1-y2)+...); this function does not exist! description only
-
document.writeln('x: '+(xArray[0])+', y: '+(yArray[0])+'<BR>');
-
var G1=1-((xArray[0]-xArray[1])*(yArray[0]-yArray[1])+(xArray[1]-xArray[2])*(yArray[1]-yArray[2])+(xArray[2]-xArray[3])*(yArray[2]-yArray[3])+(xArray[3]-xArray[4])*(yArray[3]-yArray[4])+(xArray[4]-xArray[5])*(yArray[4]-yArray[5])+(xArray[5]-xArray[6])*(yArray[5]-yArray[6])+(xArray[6]-xArray[7])*(yArray[6]-yArray[7])+(xArray[7]-xArray[8])*(yArray[7]-yArray[8])+(xArray[8]-xArray[9])*(yArray[8]-yArray[9])+(xArray[9]-xArray[10])*(yArray[9]-yArray[10])+(xArray[10]-xArray[11])*(yArray[10]-yArray[11])+(xArray[11]-xArray[12])*(yArray[11]-yArray[12])+(xArray[12]-xArray[13])*(yArray[12]-yArray[13])+(xArray[13]-xArray[14])*(yArray[13]-yArray[14])+(xArray[15]-xArray[16])*(yArray[15]-yArray[16])+(xArray[16]-xArray[17])*(yArray[16]-yArray[17])+(xArray[18]-xArray[19])*(yArray[18]-yArray[19])+(xArray[19]-xArray[20])*(yArray[19]-yArray[20])+(xArray[20]-xArray[21])*(yArray[20]-yArray[21])+(xArray[21]-xArray[22])*(yArray[21]-yArray[22])+(xArray[22]-xArray[23])*(yArray[22]-yArray[23])+(xArray[23]-xArray[24])*(yArray[23]-yArray[24])+(xArray[24]-xArray[25])*(yArray[24]-yArray[25])+(xArray[25]-xArray[26])*(yArray[25]-yArray[26])+(xArray[26]-xArray[27])*(yArray[26]-yArray[27])+(xArray[27]-xArray[28])*(yArray[27]-yArray[28])+(xArray[28]-xArray[29])*(yArray[28]-yArray[29])+(xArray[29]-xArray[30])*(yArray[29]-yArray[30]));
-
var AUC= ((G1 + 1)/2);
-
document.writeln('G1: ' + G1 + '<br />');
-
document.writeln('area under ROC curve ='+ AUC);
-
//var d=AUC-0.5;
-
-
</script>
-
Thank you so much. You have no idea how long I have wrestled with this. I shall go through this carefully to understand how one ought to order things
Hi there,
Weird behaviour of the calculation. Changing the x value changes the AUC, however at -3 the AUC should be 0.5 , but any value over or below 0 gives an AUC approaching 1, whilst at 0, the AUC gives 0.47 which is probably a flaw in the values in the calculation since I have used a 5th power polynomial instead of 6 or 7. Do your kingly powers stretch to resolving this? Basically in the actual application, there is a slider which changes the shape of a plot and consequently the AUC. I could send the whole thing to you except that it is quite large and uses loads of add-ins from jqplot - probably best sent as a zipped file straight to you. Please don't hesitate to tell me that I am asking too much! The whole thing is actually to allow medical students and doctors to understand the relativity of numbers in biology and diagnosis and will from part of a teaching module on medical arithmetic.
PS I am acknowledging your great help with the following:
Help with javascript J. King of Ontario Canada via www.bytes.com
Would you prefer something else?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gram |
last post by:
Hello,
Can anyone tell me if ASP can pass an Array intp a Function? If so, can
anyone point me to an example?
Thanks in advance.
Gram.
|
by: Nath |
last post by:
Please help!?
I am new to writing html, javascript, pretty new to MySQL but quite
proficient at writing Perl and i'm a quick learner.
I am building a database driven website and i am a little...
|
by: Herrcho |
last post by:
in K&R Chapter 6.3
it mentions two methods to calculate NKEYS.
and points out the first one which is to terminate the list of
initializers with a null pointer, then loop along keytab until the...
|
by: RyanTaylor |
last post by:
I have a final coming up later this week in my beginning Java class and my
prof has decided to give us possible Javascript code we may have to write.
Problem is, we didn't really cover JS and what...
|
by: P |
last post by:
Hi,
I am finding it little harder to get this done, kindly help me.
I need to convert an XML file to a JavaScript file (Array) using XSLT,
1. Can I use <xsl:output method="text">?
2. I am...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is...
|
by: IRC |
last post by:
hey, i am pretty new on javascript as well as PHP,
Hey, anyone can you help me, how to pass the javascript array value to
php page.........
i want to retrieve the values which are arrayed on...
|
by: akshay01 |
last post by:
Hi All,
I am using the following code in which i am creating some textboxes and and as the for loop continues the name of the textboxes will also be unique for all the textboxes.
now i want to...
|
by: Xx r3negade |
last post by:
EDIT: Title is a misnomer, it's not an array it's a pointer
I have a struct that I use to pass arguments to a function referenced by pthread_create(). It looks like this:
struct...
|
by: unpresedented |
last post by:
I have a php array that consists of 4 string elements :
$arr = Array("A", "B", "C", "D");
i want to pass this array as an argument to a javascript function and alert the contents, how ? so i need...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |