s M B e E s: (+ or -) M: B: (=2) e: E:
ax 2 + bx + c = 0 y = ax 2 + bx + c x a, b y +/- [a, b] a, b y (a+b) / 2 1-2 1-3 x 1
A a, b y 1. 2. a, b 3. for Loop (b-a)/ 4. y=a*x*x + b*x + c 5. y==0.0 y (y2) * y sol[] 6. 5. y y y2 7. Loop ( 9-1a.c ) 1.0x 2 2.0x + 0.0 = 0 [-10.0, 10.0] 0.001 1.0-2.0 0.0-10.0 10.0 0.001
A 9-1a.c 1/2 /* yamikumo method of solution for a quadratic equation */ #include <stdio.h> #include <math.h> #define hantei 0.001 int main(void) { double a, b, c; /* equation coefficients */ double area_a, area_b, step; /* searching area */ int i, j=0, kizami, nsol=0; double sol[2], soly[2]; /* there are two solutions */ double x, y1=1000.0, y2=1000.0; /* input equation coefficients and searching area */ printf("input floating number of a,b,c of ax^2 + bx + c =0 \n"); printf("by spaceing a b c\n"); scanf("%lf %lf %lf", &a, &b, &c); printf("given equation is %lf x^2 + %lf x + %lf = 0\n", a, b, c); \n"); step); printf("input floating point number of start point, end point and step printf("by space start end step\n"); scanf("%lf %lf %lf", &area_a, &area_b, &step); printf("search area is from %lf to %lf by step %lf\n", area_a, area_b,
A 9-1a.c 2/2 /* Loop number count from area_a to area_b by step */ kizami = round((area_b - area_a)/step); /* calculate y of starting point */ y2 = a * area_a * area_a + b * area_a + c; /* search! */ for (i=0; i<kizami; i++) { x = area_a + step * i; y1 = a * x * x + b * x + c; printf("i/end= %d/%d y1= %lf \t y2=%lf y1*y2= %lf \r", i, kizami, y1, y2, y1*y2); /* solution exists at y==0.0 or between +y - -y */ if ( (y1 * y2)<0.0 y1==0.0 ) { sol[j] = x; soly[j] = y1; printf("\nfind! sol[%d]= %lf soly[%d]=%lf \n", j, sol[j], j, soly[j]); j++; /* include solution number */ } y2 = y1; /* transfer to next cell */ } /* display solutions */ printf("\n\nthe solutions are %lf, %lf \t y=%lf, %lf\n", sol[0], sol[1], soly[0], soly[1]); } return (0);
A unix time time./9-1a real 0m25.673s user 0m0.150s sys 0m0.050s real user 9-1a CPU sys UNIX CPU CPU 0.150
B [1, 2] 1. 2. a, b 3. a, b f(a), f(b) 4. 50 (x) (dx) (xacc) f(x) a f(a ) 5.
B ( 9-1b.c ) 1.0x 2 2.0x + 0.0 = 0 [0.5, 100.0] 0.00001 1.0-2.0 0.0 0.5 100.0 0.00001 time 9-1b.c Excel
B 9-1b.c 1/2 /* nibun method of solution for a quadratic equation */ #include <stdio.h> #include <math.h> #define NMAX 50 double quadratic(double a, double b, double c, double x); int main(void) { double a, b, c; /* equation coefficients */ double area_a, area_b, xacc; /* searching area and x_accuracy */ int nloop; double x1, x2, x, dx; /* input equation coefficients and searching area */ printf("input floating number of a,b,c of ax^2 + bx + c =0 \n"); printf("by spaceing a b c\n"); scanf("%lf %lf %lf", &a, &b, &c); printf("given equation is %lf x^2 + %lf x + %lf = 0\n", a, b, c); printf("input floating point number of start point, end point and step \n"); printf("by space start end x accuracy\n"); scanf("%lf %lf %lf", &area_a, &area_b, &xacc); printf("search area is from %lf to %lf with solution accuracy %lf\n", area_a, area_b, xacc); /* check bad range f(area_a) x f(area_b) >0 */ if ( quadratic(a, b, c, area_a) * quadratic(a, b, c, area_b) >=0.0 ){ printf("illegal section [%g, %g]\n", area_a, area_b); return 1; }
B 9-1b.c 2/2 x1 = area_a; x2 = area_b; for (nloop=0; nloop<nmax; nloop++) { x = ( x1 + x2 )/2.0; dx = fabs(x2-x1); if (dx < xacc) { printf("answer: %g \n", x); return 0; } if ( quadratic(a, b, c, x1) * quadratic(a, b, c, x) < 0.0) { x2 = x; }else{ x1 = x; } } } printf(" can not find solution! \n"); return 1; double quadratic(double a, double b, double c, double x) { double y; } y = a * x * x + b * x + c; return y;
C x c x = a (b a) f (a) f (b) f (a) 9-1b.c 9-1c.c
! f(x) = -1 x<0 f(x) = +1 x 0 0 0-1 1 f(x) = x -1 0 f(x) x=0 f(x) f(x) = x 2 x=0 f(x)
Newton f(x) Newton x i
D f(x) = cos(x) -x -1.0 ~ 3.0
f(x) [a, b] N [ ] I = f (x)dx = f (x i ) Δx = Δx f (x 0 ) + f (x 1 ) + + f (x N ) a b i=1 [a, b] N h=(b-a)/n x 0 =a, x 1 =x 0 +h,, x N =b f(x)
f(x) N N [ ] I = f (x)dx = f (x i ) Δx = h f (x 0 ) + f (x 0 + Δx) + + f (x N ) a b i=1
f(x) N I = f (x)dx = a b N i=1 1 2 f (x ) + 1 i 2 f (x ) i+1 Δx = h 1 N 1 2 f (x ) + f (x ) 0 i + 1 2 f (x ) N i=1 f(x) f(x) I = f (x)dx = a b N i=1 1 3 f (x ) + 4 i 3 f (x ) + 1 i+1 3 f (x ) i+2 Δx Simpson
A 0.01 0.0 9.42 cos x 0.01 xy OpenOffice cos x sin x OpenOffice OpenOffice C cos xdx = sin x I = cos xdx = cos(x i ) 0.01 0 9.42 N i=1 ( 9-2a.c ) 9-2a 0.0 9.42 0.1 9-2a.txt
A 9-2a.c 1/2 /* kukei sekibun */ #include <stdio.h> #include <math.h> double xsin(double x); double integ_xsin(double x); int main(void){ double start_p, end_p, kizami; int i, max; double x, integ=0.0; /* set arb. const of integral to zero */ FILE *file1; /* file pointer, nessary to use file input/output */ /* FILE *file2 file pointer nessary to use file input/output */ file */ file1 = fopen("9-2a.txt","w"); /* open 9-2a.dat to write data*/ /* file2 = fopen("text data","r"); open test.data to read */ /* fscanf(file2,"%lf %lf %lf", &start_p, &end_p, &kizami); read data from printf("input start, end points and step by floating point number\n"); scanf("%lf %lf %lf", &start_p, &end_p, &kizami); printf("given integral section is [%lf, %lf] step %lf\n", start_p, end_p, kizami); max = round((end_p - start_p)/kizami); printf("x\tcos x\tnumeric integration\tsin x\n"); for (i=0; i<max; i++){ x = start_p + kizami * i ; integ += cos( x ) * kizami; printf("%lf\t%lf\t%lf\t%lf\n", x, cos(x), integ, sin(x) );
A 9-2a.c 2/2 /* output data to standard output*/ fprintf(file1,"%lf\t%lf\t%lf\t%lf\n", x, cos(x), integ, sin(x)); /* output data to file2 */ } } fclose(file1); /* close file1 */ /* fclose(file2); close file2 */ return 0; /* function ha Integral(x sin x dx) = sin x - x cos x */ double xsin(double x){ return (x*sin(x)); } /* sin x - x cos x */ double integ_xsin(double x){ return (sin(x)-x*cos(x)); }
B x sin xdx = sin x 0.1 0.0 9.42 sin x - x cos x x cos x I = cos xdx = 1 2 cos(a) + cos(x ) 0.1 i + 1 2 cos(b) 0 9.42 N 1 i= 2 OpenOffice OpenOffice C C
B [8, 9] 2 8 8.2 8.4 8.6 8.8 9 x h 3 Simpson h 5 h 5 Integral(x sin(x)) 9 8 7 6 5 4 3 Numerical integration of x sin(x)
f(x) [a, b] 0 f(x) U I xy [a, b] y=0 (x ) y=f(x) a x b, 0 y U (x, y) y f(x) P = I / [U (b-a)] (x, y) y f(x) I y f(x)
f(x) df (x) dx = lim Δx 0 f (x + Δx) f (x) Δx f(x) x Δx
A [a, b] N Δx=(b - a)/n [0.0, 9.24] 0.01 sin x xy OpenOffice sin x cos x OpenOffice OpenOffice C cos xdx = sin x d sin x dx dsin x dx = cos x = sin(x + Δx) sin(x) Δx 0.1 1.0 sin x