Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static float Newton(float x)
- {
- var a = 5;
- var u = 2;
- var y = 204;
- var n = 0.5f;
- // Since everything but x is constant that whole sqrt(a^3 / u) can be treated like a constant so calculate it early.
- var d = Mathf.Sqrt(Mathf.Pow(a, 3) / u);
- // Number of iterations for Newton's method.
- var steps = 3;
- // Actual Newton's method where xi+1 = xi - f(xi) / f'(xi)
- for (int i = 0; i < steps; i++)
- {
- x = x - f(x, d, y, n) / fp(x, d, y, n);
- }
- // Final approximated value of x is returned
- return x;
- }
- // Original function from moving every term to one side.
- public static float f(float x, float d, float y, float n)
- {
- return y - d * (x - n * Mathf.Sin(x));
- }
- // Derivative of original function.
- public static float fp(float x, float d, float y, float n)
- {
- return - d * (1 - n * Mathf.Cos(x));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement