t1nman

Math.lab4

May 19th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define EPS 0.0001
  4.  
  5. void newton(float x0, float y0);
  6. void check(float x, float y);
  7. float F(float x, float y);
  8. float fx(float x, float y);
  9. float fy(float x, float y);
  10. float G(float x, float y);
  11.  
  12. int main()
  13. {
  14.     printf("\nf(x,y) = sin(x+y) - 1.2x - 0.2\ng(x,y) = x^2 + y^2 - 1\n\n");
  15.     printf("\n==================================\n");
  16.     printf("1st pair of roots:\n");
  17.     newton(-1, -1);
  18.     printf("\n==================================\n");
  19.     printf("2nd pair of roots:\n");
  20.     newton(1, 1);
  21.  
  22.     return 0;
  23. }
  24.  
  25. void newton(float x, float y)
  26. {
  27.     float A, B, J;
  28.     float x0, y0, x1 = x, y1 = y;
  29.  
  30.     do {
  31.         x0 = x1;
  32.         y0 = y1;
  33.  
  34.         A = F(x0,y0) * 2*y - fy(x0,y0) * G(x0,y0);
  35.         B = fx(x0,y0) * G(x0,y0) - F(x0,y0) * 2*x;
  36.         J = fx(x0,y0) * 2*y - fy(x0,y0) * 2*x;
  37.  
  38.         if (J == 0) {
  39.             printf("Jacobian is equal to zero!\n");
  40.             break;
  41.         }
  42.  
  43.         x1 = x0 - A / J;
  44.         y1 = y0 - B / J;
  45.     } while ((fabs(x1 - x0) >= EPS) && (fabs(y1 - y0) >= EPS));
  46.  
  47.     printf("\nx = %f\ty = %f\n", x1, y1);
  48.     check(x1,y1);
  49. }
  50.  
  51. void check(float x, float y)
  52. {
  53.     float f = sin(x+y) - 1.2*x - 0.2;
  54.     float g = x*x + y*y - 1;
  55.     printf("\nChecking...\n\n");
  56.     printf("f(%f,%f) = %f\n\n",x,y,f);
  57.     printf("g(%f,%f) = %f\n\n",x,y,g);
  58. }
  59.  
  60. float F(float x, float y)
  61. {
  62.     float sinxy = sin(x+y);
  63.     return (sinxy - 1.2*x - 0.2);
  64. }
  65.  
  66. float fx(float x, float y)
  67. {
  68.     return (cos(x+y) - 1.2);
  69. }
  70.  
  71. float fy(float x, float y)
  72. {
  73.     return cos(x+y);
  74. }
  75.  
  76. float G(float x, float y)
  77. {
  78.     return (x*x + y*y - 1);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment