+ 2

Finding root of this equation e^-x+cos(x^3+2*x-1) -12 using iteration method it's -2. 5486 exactly but it's oscillating

Help me correct this code so that it will be able to find root of any trancedental equation using iteration method. Here's my code Thanks in advance #include<stdio.h> #include<conio.h> #include<math.h> #define e1 2.7182818 #define f(x) pow(e1,-x)+cos(pow(x,3)+2*x-1)-12 #define g(x) -log(12-cos(pow(x,3)+2*x-1)) int main() { int step=1, N; float x0, x1, e; /* Inputs */ printf("Enter initial guess: "); scanf("%f", &x0); printf("Enter tolerable error: "); scanf("%f", &e); printf("Enter maximum iteration: "); scanf("%d", &N); /* Implementing Fixed Point Iteration */ printf("\nStep\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n"); do { x1 = g(x0); printf("%d\t%f\t%f\t%f\t%f\n",step, x0, f(x0), x1, f(x1)); step = step + 1; if(step>N) { printf("Not Convergent."); break; } x0 = x1; }while( fabs(f(x1)) > e); printf("\nRoot is %f", x1); getch(); return(0); }

30th Aug 2020, 9:03 AM
O.Naga Lakshmi Lalasa
O.Naga Lakshmi Lalasa - avatar
1 Antwort
+ 1
Do not use #define. It's too dangerous. #define SQUARE(a) a*a int main() { int f = SQUARE(3 + 3); // 3 + 3*3 + 3 = 15, not 16 return 0; } If you want to use #define, put breakets around every variable. In my example #define SQUARE(a) (a)*(a)
30th Aug 2020, 10:56 AM
Sebastian Ahlborn
Sebastian Ahlborn - avatar