COP4813 Assignment 4 - Equation plotting

by joseph_kanawall2400

HTML

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
  Orbital period of two bodies orbiting eachother
<br/><br/>
<img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/3e3b818e77117af84080cc6130389c9908f2fde8" />
<br/><br/>
m1 = Mass of Body 1 in kilograms<br/>
m2 = Mass of Body 2 in kilograms<br/>
a = Distance between two bodies in meters
<br/><br/>
m1: <input type="text" id="m1" />
<br/> 
m2: <input type="text" id="m2" />
<br/>
a min: <input type="text" id="amin" />
<br/>
a max: <input type="text" id="amax" />
<br/>
<input type="button" value="Plot" id="plot" />
<input type="button" value="Calculate" id="calculate" />
<br/><br/>
<div id="result"></div>
<br/>
<div id="container"></div>

JavaScript

G = 6.67408 * Math.pow(10, -11)
stepCount = 50

function plot() {
	values = calcForce()
	Highcharts.chart('container', {
		title: {
			text: "Orbital period of two bodies orbiting eachother"
		},
		yAxis: {
			title: {
				text: 'Orbital Period (s)'
			}
		},
		xAxis: {
			title: {
				text: 'Distance from eachother (m)'
			}
		},
		series: [{
			name: "Results",
			data: values
		}]
	})
}

function calculate() {
	values = calcForce()
	str = ""
	for(i = 0; i < values.length; i++) {
		v = values[i]
		str += "a = " + values[i][0] + " T = " + values[i][1] + "<br/>"
	}
	$('#result').html(str)
}

function calcForce() {
	values = new Array()

	m1 = parseInt($('#m1').val())
	m2 = parseInt($('#m2').val())
	amin = parseInt($('#amin').val())
	amax = parseInt($('#amax').val())

	step = Math.abs(amax - amin) / stepCount

	for(i = 0; i <= stepCount; i++) {
		a = amin + (step * i)

		values[i] = new Array()
		values[i][0] = a
		values[i][1] = Math.round(force(m1, m2, a) * 100) / 100
	}
	return values
}

function force(m1, m2, a) {
	return 2 * Math.PI * Math.sqrt(Math.pow(a, 3) / (G * (m1 + m2)))
}

$('#plot').click(function() {
	plot();
});

$('#calculate').click(function() {
	calculate();
});

$('#m2').val(10)
$('#m1').val(1000000)
$('#amin').val(0)
$('#amax').val(500)