| (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("%f %f", &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= %f\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("%f", &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= %f\n", x1); return 0; } |
|
|
|
|
|
| (30) |
| (31) |
| (32) |
| (33) |
| (34) |
| (35) |
|
| (38) |
|
n | 10 | 12 | 14 | 16 | 18 | 20 |
Time | 0.4 sec. | 1 min. | 3.6 hrs. | 41 days | 38 years | 16,000 years |
| (42) |
| (43) |
| (44) |
|
| (45) |
Ref. line number | x | y | z | = | Comment |
(1) | 2 | 3 | 4 | 6 | |
(2) | 3 | 5 | 2 | 5 | |
(3) | 4 | 3 | 30 | 32 | |
(4) | 1 | 1.5 | 2 | 3 | (1)÷2 |
(5) | 3 | 5 | 2 | 5 | |
(6) | 4 | 3 | 30 | 32 | |
(7) | 1 | 1.5 | 2 | 3 | |
(8) | 0 | 0.5 | -4 | -4 | (5)−(4)×3 |
(9) | 0 | -3 | 22 | 20 | (6)−(4)×4 |
(10) | 1 | 1.5 | 2 | 3 | |
(11) | 0 | 1 | -8 | -8 | (8)÷0.5 |
(12) | 0 | -3 | 22 | 20 | |
(13) | 1 | 0 | 14 | 15 | (10)−(11)×1.5 |
(14) | 0 | 1 | -8 | -8 | |
(15) | 0 | 0 | -2 | -4 | (12)−(11)×(−3) |
(16) | 1 | 0 | 14 | 15 | |
(17) | 0 | 1 | -8 | -8 | |
(18) | 0 | 0 | 1 | 2 | (15)÷(−2) |
(19) | 1 | 0 | 0 | -13 | (16)−(18)×14 |
(20) | 0 | 1 | 0 | 8 | (17)− (18)×(−8) |
(21) | 0 | 0 | 1 | 2 | |
|
| (46) |
|
| (48) |
#include <stdio.h> #define N 3 int main() { double a[N][N+1]={{2, 3, 4, 6},{3, 5, 2, 5},{4, 3, 30, 32}}; double pivot,d; int i,j,k; for(k=0; k<N; k++) { pivot=a[k][k]; for(j=k; j<N+1; j++) a[k][j]=a[k][j]/pivot; for(i=0; i<N; i++) { if(i != k) { d=a[i][k]; for(j=k; j<N+1; j++) a[i][j]=a[i][j]-d*a[k][j]; } } } for(i=0; i<N; i++) printf("x[%d]=%f\n", i+1, a[i][N]); return 0; } |
| (49) |
| (50) |
| (51) |
| (52) |
| (53) |
| (54) |
| (55) |
| (56) |
| (57) |
| (58) |
| (59) |
| (60) |
| (61) |
| (62) |
| (63) |
| (64) |
#include <stdio.h> int main() { double x, y, z; int i,n; x=y=z=0.0; printf("Enter # of iteration = "); scanf("%d", &n); for (i=0;i<n;i++) { x = (10-y-2*z)/7; y = (8-x-3*z)/8.0; z = (6-2*x-3*y)/9.0; } printf("x = %f, y= %f, z=%f\n", x,y,z); return 0; } |