Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define EPS 0.0001
- void newton(float x0, float y0);
- void check(float x, float y);
- float F(float x, float y);
- float fx(float x, float y);
- float fy(float x, float y);
- float G(float x, float y);
- int main()
- {
- printf("\nf(x,y) = sin(x+y) - 1.2x - 0.2\ng(x,y) = x^2 + y^2 - 1\n\n");
- printf("\n==================================\n");
- printf("1st pair of roots:\n");
- newton(-1, -1);
- printf("\n==================================\n");
- printf("2nd pair of roots:\n");
- newton(1, 1);
- return 0;
- }
- void newton(float x, float y)
- {
- float A, B, J;
- float x0, y0, x1 = x, y1 = y;
- do {
- x0 = x1;
- y0 = y1;
- A = F(x0,y0) * 2*y - fy(x0,y0) * G(x0,y0);
- B = fx(x0,y0) * G(x0,y0) - F(x0,y0) * 2*x;
- J = fx(x0,y0) * 2*y - fy(x0,y0) * 2*x;
- if (J == 0) {
- printf("Jacobian is equal to zero!\n");
- break;
- }
- x1 = x0 - A / J;
- y1 = y0 - B / J;
- } while ((fabs(x1 - x0) >= EPS) && (fabs(y1 - y0) >= EPS));
- printf("\nx = %f\ty = %f\n", x1, y1);
- check(x1,y1);
- }
- void check(float x, float y)
- {
- float f = sin(x+y) - 1.2*x - 0.2;
- float g = x*x + y*y - 1;
- printf("\nChecking...\n\n");
- printf("f(%f,%f) = %f\n\n",x,y,f);
- printf("g(%f,%f) = %f\n\n",x,y,g);
- }
- float F(float x, float y)
- {
- float sinxy = sin(x+y);
- return (sinxy - 1.2*x - 0.2);
- }
- float fx(float x, float y)
- {
- return (cos(x+y) - 1.2);
- }
- float fy(float x, float y)
- {
- return cos(x+y);
- }
- float G(float x, float y)
- {
- return (x*x + y*y - 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment