Need a very basic javascript program along with an output using the following in
ID: 3825552 • Letter: N
Question
Need a very basic javascript program along with an output using the following instructions:
Consider a first order ordinary differential equation given by: y ' = f (x,y) = y*x^2 -1.2y with a starting value of y (0) = 1 on the interval x = 0 to 2 with n = 8 (step size will be h = 0.25 ) determine the value of y (2) using:
(a) Euler's method on each interval the next y value = previous y value + slope times step size. or yn = y + f(x,y) * h
(b) Heun's method k1 = f ( x, y ) k2 = f ( x+h, y+k1*h ) and yn = y + (k1 + k2) * h / 2
(c) RK-3 method k1 = f ( x, y ) k2 = f ( x+h/2, y+k1*h/2 ) k3 = f ( x+h, y -k1*h + 2*k2*h) and yn = y + (k1 + 4*k2 + k3) * h/6
(d) RK-4 method k1 = f ( x, y ) k2 = f ( x+h/2, y+k1*h/2 ) k3 = f ( x+h/2, y+k2*h/2 ) k4 = f ( x+h, y + k3*h) and yn = y + (k1 + 2*k2 + 2*k3 + k4) * h / 6
at the start of the program I need you to define: function f (x,y) initial values: x = 0 and y = 1 final value: b = 2 number of steps: n = 8 you will need to display the four ending values for y:
(a) Euler's method
(b) Heun's method
(c) RK-3 method
(d) RK-4 method
Explanation / Answer
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>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.