Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1.     public static float Newton(float x)
  2.     {
  3.         var a = 5;
  4.         var u = 2;
  5.         var y = 204;
  6.         var n = 0.5f;
  7.  
  8.         // Since everything but x is constant that whole sqrt(a^3 / u) can be treated like a constant so calculate it early.
  9.         var d = Mathf.Sqrt(Mathf.Pow(a, 3) / u);
  10.  
  11.         // Number of iterations for Newton's method.
  12.         var steps = 3;
  13.  
  14.         // Actual Newton's method where xi+1 = xi - f(xi) / f'(xi)
  15.         for (int i = 0; i < steps; i++)
  16.         {
  17.             x = x - f(x, d, y, n) / fp(x, d, y, n);
  18.         }
  19.        
  20.         // Final approximated value of x is returned
  21.         return x;
  22.     }
  23.  
  24.     // Original function from moving every term to one side.
  25.     public static float f(float x, float d, float y, float n)
  26.     {
  27.         return y - d * (x - n * Mathf.Sin(x));
  28.     }
  29.  
  30.     // Derivative of original function.
  31.     public static float fp(float x, float d, float y, float n)
  32.     {
  33.         return  - d * (1 - n * Mathf.Cos(x));
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement