| (1) |
| (2) |
| (3) |
#include <stdio.h> #include <math.h> int main() { float a, b, c, disc,x1, x2; a=1.0;b=200000;c=-3; disc=b*b-4*a*c; x1=(-b-sqrt(disc))/(2*a); x2=(-b+sqrt(disc))/(2*a); printf("x1 = %f, x2 = %f\n",x1,x2); return 0; } |
|
|
|
| (11) |
| (12) |
|
| (16) |
| (17) |
| (18) |
/* Compute the square root of 2 */ #include <stdio.h> #include <math.h> #define EPS 1.0e-10 #define N 100 double f(double x) { return pow(x,2)-2; } /* start of main */ int main() { double x1, x2, x3; int count; printf("Enter xleft and xright separated by space ="); scanf("%lf %lf", &x1, &x2); /* sanity check */ if (f(x1)*f(x2)>0) {printf("Invalid x1 and x2 !\n"); return 0;} /* bisection start */ for (count=0;count< N; count++) { x3= (x1+x2)/2.0; if (f(x1)*f(x3)<0 ) x2=x3; else x1=x3; if ( f(x3)==0.0 || fabs(x1-x2)< EPS ) break; } printf("iteration = %d\n", count); printf("x= %lf\n", x1); return 0; } |
|
|
|
|
|
|
|
| (19) |
|
#include <stdio.h> #include <math.h> #define EPS 1.0e-10 double f(double x) { return x*x-2; } double fp(double x) { return 2*x; } double newton(double x) { return x - f(x)/fp(x); } int main() { double x1, x2; int i; printf("Enter initial guess ="); scanf("%lf", &x1); if (fp(x1)==0.0) { printf("No convergence.\n"); return 0; } for (i=0;i<100;i++) { x2=newton(x1); if (fabs(x1-x2)< EPS) break; x1=x2; } printf("iteration = %d\n", i); printf("x= %lf\n", x1); return 0; } |
|
|
|
|
|