Can someone please show me how to display an ouput with the following program? T
ID: 3826076 • Letter: C
Question
Can someone please show me how to display an ouput with the following program? The output has to display the four ending values of Y:
(a) Euler's method
(b) Heun's method
(c ) RK-3 method
(d) RK-4 method
Following is program:
mathfunction.js
-------------------------------------------------------------------------------------------------------------------------------------
var yinit = 1;
var step = 0.25;
var xmax = 2;
var n = xmax/step;
//method to calculate euler function eulerInit(){ var x,y,h,t,k; x=0; y=yinit; h= step; t=xmax; while(x<=t) { k=h*mainLogic(x,y); y=y+k; x=x+h; }
alert(y); }
//method to calculate euler function heunsInit(){ var x,y,h,t,k; x=0; y=yinit; h=step; t=xmax; while(x+h<=t) { l=(h/2)*(mainLogic(x,y)+mainLogic(x+h,y+h*mainLogic(x,y))); y=y+l; x=x+h; }
alert(y); }
//method to calculate runge kutta 4th order
function rk4Init(){
var k1,k2,k3,k4,m=0,y,x,h,t;
x=0;
y=yinit;
h=step;
t=xmax;
while(x<t)
{
k1=mainLogic(x,y);
k2=mainLogic((x+h/2.0),(y+k1*h/2.0));
k3=mainLogic((x+h/2.0),(y+k2*h/2.0));
k4=mainLogic((x+h),(y+k3*h));
m=((k1+2*k2+2*k3+k4)/6);
y=y+m*h;
x=x+h;
}
alert(y);
}
//method to calculate runge kutta 3rd order
function rk3Init(){
var k1,k2,k3,m=0,y,x,h,t;
x=0;
y=yinit;
h=step;
t=xmax;
while(x<t)
{
k1=mainLogic(x,y);
k2=mainLogic((x+h/2.0),(y+k1*h/2.0));
k3=mainLogic((x+h),(y-k2*h+2.0*k2*h));
m=((k1+4*k2+k3)/6);
y=y+m*h;
x=x+h;
}
alert(y);
}
//we apply values to the function here
function mainLogic(x,y){
var f;
f= y*x*x - 1.2*y;
return f;
}
--------------------------------------------------------------------------------------------------------------------------------------------------
index.html
__________________________________
<html>
<head></head>
<body>
<script type="text/javascript" src="mathfunction.js"></script>
<script type="text/javascript">eulerInit();heunsInit();rk3Init();rk4Init();</script>
</body>
</html>
Explanation / Answer
//Save the file mathfunction.js and index.html in the same folder and run the index.html file
//The mathfunction.js needed some formatting which is done.
//The code is attached below
var yinit = 1;
var step = 0.25;
var xmax = 2;
var n = xmax/step;
//method to calculate euler
function eulerInit(){ var x,y,h,t,k; x=0; y=yinit; h= step; t=xmax; while(x<=t) { k=h*mainLogic(x,y); y=y+k; x=x+h; }
alert(y); }
//method to calculate euler
function heunsInit(){ var x,y,h,t,k; x=0; y=yinit; h=step; t=xmax; while(x+h<=t) { l=(h/2)*(mainLogic(x,y)+mainLogic(x+h,y+h*mainLogic(x,y))); y=y+l; x=x+h; }
alert(y); }
//method to calculate runge kutta 4th order
function rk4Init(){
var k1,k2,k3,k4,m=0,y,x,h,t;
x=0;
y=yinit;
h=step;
t=xmax;
while(x<t)
{
k1=mainLogic(x,y);
k2=mainLogic((x+h/2.0),(y+k1*h/2.0));
k3=mainLogic((x+h/2.0),(y+k2*h/2.0));
k4=mainLogic((x+h),(y+k3*h));
m=((k1+2*k2+2*k3+k4)/6);
y=y+m*h;
x=x+h;
}
alert(y);
}
//method to calculate runge kutta 3rd order
function rk3Init(){
var k1,k2,k3,m=0,y,x,h,t;
x=0;
y=yinit;
h=step;
t=xmax;
while(x<t)
{
k1=mainLogic(x,y);
k2=mainLogic((x+h/2.0),(y+k1*h/2.0));
k3=mainLogic((x+h),(y-k2*h+2.0*k2*h));
m=((k1+4*k2+k3)/6);
y=y+m*h;
x=x+h;
}
alert(y);
}
//we apply values to the function here
function mainLogic(x,y){
var f;
f= y*x*x - 1.2*y;
return f;
}