Javascript over-equation solving (run under Nodejs)

Source: Internet
Author: User
[Javascript] // solves the functionf1 (x) {return2 * x * Math. pow (Math. sin (x), 7) + 3 * Math. sqrt (x) * Math. cos (x)-Math. exp (x)/5;} // functionf2 (x) {returnMath...

[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 );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.