-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob5_Trape.c
More file actions
executable file
·83 lines (66 loc) · 1.87 KB
/
Copy pathprob5_Trape.c
File metadata and controls
executable file
·83 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void trapezium(double ,double ,int); //通常の台形則
void trapezium_improved(double ,double ,int); //小さい順に足す台形則
//1つ目の変数は積分範囲の下限,2つ目の変数は積分範囲の上限,3つ目の変数は分割数
//被積分関数
double function (double);
int main(int argc, char *argv[]){
double integral; // 積分結果
double x_min = 0; //積分範囲の最小値
double x_max = 1.0; //積分範囲の最大値
int div_Num = 100; //分割数
int method;// 1:通常の台形則 2:小さい順に足す台形則
double x;
int i;
method = atoi(argv[1]);
for(i=1;i<11;i++){
div_Num *= 2;
if (method == 1){
trapezium(x_min,x_max,div_Num);
}else{
trapezium_improved(x_min,x_max,div_Num);
}
}
return 0;
}
void trapezium(double x_min,double x_max,int div_Num){
double integral = 0; // 積分結果
double h; // 分割数
double x;
int i;
h = (x_max - x_min) / div_Num;
x = x_min;
integral = function(x)/2.0;
x += h;
for (i=1; i<div_Num; i++) {
integral += function(x);
x += h;
}
integral += function(x)/2.0;
integral *= h;
printf("%.10lf %.20lf\n",h,fabs(M_PI/4.0-integral));
}
void trapezium_improved(double x_min,double x_max,int div_Num){
double integral = 0; // 積分結果
double h;// 分割数
double x;
int i;
h = (x_max - x_min) / div_Num;
x = x_min;
integral = (function(x_min) + function(x_max))/2.0;
x += h;
for (i=1; i<div_Num/2.0; i++) {
integral += (function(x) + function(x_max - x));
x += h;
}
if(div_Num % 2 == 0) integral += function(x);
integral *= h;
printf("%lf %.20lf\n",h,fabs(1-integral));
}
double function (double x){
double result;
result = 1.0/(1+x*x);
return result;
}