Jump to content

JavaScript Calculator and Plotting


dinasity

Recommended Posts

Hello Everyone, I'm breaking my neck trying to see what I'm doing wrong can any one give me any pointers. When I click on "Calculate or Plot" nothing happens. I believe it has something to do with my script. I would really appreciate it.

<!doctype html><html lang="en"> <head>  <meta charset="UTF-8">  <meta name="Generator" content="EditPlus®">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <title>Document</title> </head> <body>  <title>Sine Function Graph</title>y = a sin bx<br/><br/>     Input a <input type="text" id="a" size="5" value="2"/><br/>     Input b <input type="text" id="b" size="5" value="-6" /><br/>     X Min<input type="text" id="xmin" value="-10" size="5"/>     X Max<input type="text" id="xmax" value="10" size="5" /><br/><br/>    <input type="button" value="Calculate" id="calculate" /><input type="button" value="Plot" id="plot" /><br/><br/><p id="output"> </p>  <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div><script>var a = 0;var b = 0;var n = 0;var x = new Array();var y = new Array(); var v = new Array();function calculateY(b, a, x) {    return (a * Math.sin(x) * ;}function calculate() {    a = Number($('#a').val());    b = Number($('#b').val());    var xmin = Number($('#xmin').val());    var xmax = Number($('#xmax').val());    var xt = 0;            var i = 0;    for (xt = xmin; xt <= xmax; xt++) {        x[i] = xt;        y[i] = calculateY(a, b, xt);        v[i] = [x[i], y[i]];        i++;    }    n = i - 1;    }function displayValues(){   var s = "";        s = "Y = " + a;    s+= " sin (" + b + ")<br/><br/>";        for (var i = 0; i <= n; i++)    {        s += " X = " + x[i] + " Y = " + y[i] + "<br/>";           }        output.innerHTML = s;}function plotValues(){   calculate();   chart = new Highcharts.Chart({            chart: {                renderTo: 'container',                type: 'spline',                marginRight: 130,                marginBottom: 25            },            title: {                text: 'Sine',                x: -20 //center            },            xAxis: {                title: {                    text: 'X'                }            },            yAxis: {                title: {                    text: 'Y'                }               },               plotOptions: {                scatter: {                    marker: {                        radius: 5,                        states: {                            hover: {                                enabled: true,                                lineColor: 'rgb(100,100,100)'                            }                        }                    },                    states: {                        hover: {                            marker: {                                enabled: false                            }                        }                    }                }            },              series: [{                name: 'Y Values',                color: 'rgba(223, 83, 83, .5)',                data: v       }]                   })      }$('#calculate').click( function() {     calculate();     displayValues();    });$('#plot').click( function() {     calculate();     plotValues();   });</script> </body></html>
Link to comment
Share on other sites

You are missing these includes...

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://code.highcharts.com/highcharts.js"></script>

put them in the <head>

Link to comment
Share on other sites

I've noticed that every time I open the page 2,-6,6, and 10 appear.

Is there a way were I can change it so it can be blank?

Sorry for all the questions, but W3 schools has been teaching me a lot...

<!doctype html><html lang="en"><head>  <meta charset="UTF-8">  <meta name="Generator" content="EditPlus®">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <title>Document</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://code.highcharts.com/highcharts.js"></script></head><body>y = a sin bx<br/><br/>Input a <input type="text" id="a" size="5" value="2"/><br/>Input b <input type="text" id="b" size="5" value="-6" /><br/>X Min<input type="text" id="xmin" value="-10" size="5"/>X Max<input type="text" id="xmax" value="10" size="5" /><br/><br/>    <input type="button" value="Calculate" id="calculate" /><input type="button" value="Plot" id="plot" /><br/><br/><p id="output"> </p>  <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div><script>var a = 0;var b = 0;var n = 0;var x = new Array();var y = new Array(); var v = new Array();function calculateY(b, a, x) {    return (a * Math.sin(x) * ;}function calculate() {    a = Number($('#a').val());    b = Number($('#b').val());    var xmin = Number($('#xmin').val());    var xmax = Number($('#xmax').val());    var xt = 0;            var i = 0;    for (xt = xmin; xt <= xmax; xt++) {        x[i] = xt;        y[i] = calculateY(a, b, xt);        v[i] = [x[i], y[i]];        i++;    }    n = i - 1;    }function displayValues(){   var s = "";        s = "Y = " + a;    s+= " sin (" + b + ")<br/><br/>";        for (var i = 0; i <= n; i++)    {        s += " X = " + x[i] + " Y = " + y[i] + "<br/>";           }        output.innerHTML = s;}function plotValues(){   calculate();   chart = new Highcharts.Chart({            chart: {                renderTo: 'container',                type: 'spline',                marginRight: 130,                marginBottom: 25            },            title: {                text: 'Sine',                x: -20 //center            },            xAxis: {                title: {                    text: 'X'                }            },            yAxis: {                title: {                    text: 'Y'                }               },               plotOptions: {                scatter: {                    marker: {                        radius: 5,                        states: {                            hover: {                                enabled: true,                                lineColor: 'rgb(100,100,100)'                            }                        }                    },                    states: {                        hover: {                            marker: {                                enabled: false                            }                        }                    }                }            },              series: [{                name: 'Y Values',                color: 'rgba(223, 83, 83, .5)',                data: v       }]                   })      }$('#calculate').click( function() {     calculate();     displayValues();    });$('#plot').click( function() {     calculate();     plotValues();   });</script> </body></html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...