Can someone please show me how I can see an output of the four values, listed a
ID: 3836210 • Letter: C
Question
Can someone please show me how I can see an output of the four values, listed a thru d, when opening the program below with a browser. I need to save program below as a ".html" file and be able to open it to see an output with a browser. 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
(I saved the program with notepad ++ but I cannot yield the outputs I am looking for when I open program with browser.)
Program is as follows:
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 heuns
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;
}
Explanation / Answer
Solution:
<script>
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("Using Euler's method : " + y);
}
//method to calculate heuns
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("Using Heun's method : "+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("Using RK-3 method : " + 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("Using RK-4 method : " + y)
}
//we apply values to the function here
function mainLogic(x,y){
var f;
f= y*x*x - 1.2*y;
return f;
}
eulerInit();
heunsInit();
rk4Init();
rk3Init();
</script>
save this file as program_name.html and run it in the browser then we got the four alert dialogs with the following values as outputs:
Sample Output:
Using Euler's method : 1.0464727412574657
Using Heun's method : 1.2882575938294534
Using RK-3 method : 1.3049557988761693
Using RK-4 method : 1.279931738333795