[Javascript] // solving the transcendence Equation
Function f1 (x)
{
Return 2 * x * Math. pow (Math. sin (x), 7) +
3 * Math. sqrt (x) * Math. cos (x)-Math. exp (x)/5;
}
// Multi-Solution
Function f2 (x)
{
Return Math. sin (2 * Math. PI * x );
}
// Find the exact solution by half between two x
Function getAnswer (x0, x1, y0, y1, func, precision)
{
Var mid, val;
While (true)
{
Mid = (x0 + x1)/2;
Val = func (mid );
If (Math. abs (val) <precision) break;
If (y0 * val <0)
{
X1 = mid;
Y1 = val;
}
Else
{
X0 = mid;
Y0 = val;
}
}
Return {x: mid, y: val };
}
// Solve within the specified range --- calculate all the solutions between [start, end]
Function solve (start, end, func)
{
Var rst = [];
Var lastx = start + 1e-5;
Var y0, y1, y2;
Y0 = func (lastx );
Y1 = y0;
For (var x = start; x <= end + 0.001; x + = 0.001)
{
Y2 = func (x );
If (y2 * y1 <0)
{
Rst. push (getAnswer (lastx, x, y1, y2, func, 1e-8 ));
}
Y1 = y2;
Lastx = x;
}
Return rst;
}
// Test the solution of the two equations
Var rst = solve (2, 3, f1 );
Console. log (rst );
Var rst = solve (0, 5, f2 );
Console. log (rst. length );
Console. log (rst );
// Solving the transcendence Equation
Function f1 (x)
{
Return 2 * x * Math. pow (Math. sin (x), 7) +
3 * Math. sqrt (x) * Math. cos (x)-Math. exp (x)/5;
}
// Multi-Solution
Function f2 (x)
{
Return Math. sin (2 * Math. PI * x );
}
// Find the exact solution by half between two x
Function getAnswer (x0, x1, y0, y1, func, precision)
{
Var mid, val;
While (true)
{
Mid = (x0 + x1)/2;
Val = func (mid );
If (Math. abs (val) <precision) break;
If (y0 * val <0)
{
X1 = mid;
Y1 = val;
}
Else
{
X0 = mid;
Y0 = val;
}
}
Return {x: mid, y: val };
}
// Solve within the specified range --- calculate all the solutions between [start, end]
Function solve (start, end, func)
{
Var rst = [];
Var lastx = start + 1e-5;
Var y0, y1, y2;
Y0 = func (lastx );
Y1 = y0;
For (var x = start; x <= end + 0.001; x + = 0.001)
{
Y2 = func (x );
If (y2 * y1 <0)
{
Rst. push (getAnswer (lastx, x, y1, y2, func, 1e-8 ));
}
Y1 = y2;
Lastx = x;
}
Return rst;
}
// Test the solution of the two equations
Var rst = solve (2, 3, f1 );
Console. log (rst );
Var rst = solve (0, 5, f2 );
Console. log (rst. length );
Console. log (rst );