-
Notifications
You must be signed in to change notification settings - Fork 0
/
a4.c
138 lines (118 loc) · 3.73 KB
/
a4.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Practicum Modelleren Simuleren
* Jan Laan (5756529) & Joost Hekman (5887232)
*
* Assignment 2, part 5:
* Numerical intergration introduction of the six specified function
* and their range with all of the methods defined in the headerfile.
*/
#include <stdio.h>
#include <math.h>
#include "integrations.h"
#define E 2.7182818284590452353602874713526624977572470936999595749669
/* Function 1 */
double f1(double x) {
return pow(x, 2);
}
/* Function 2 */
double f2(double x) {
return pow(x, 3);
}
/* Function 3 */
double f3(double x) {
return pow(x, 5);
}
/* Function 4 */
double f4(double x) {
return pow(E, -x);
}
/* Function 5 */
double f5(double x){
return x * pow(E, -x);
}
/* Function 6 */
double f6(double x){
return pow(x, -0.5);
}
/* Main function */
int main(int argc, char* argv[]) {
double j = 0.0;
integral fun = &f1;
/* Print the integrals of the first function using all of the available
* methods including trapezoidal, rectangle, Simpson and Gauss
*/
printf("\nIntegrals of x^2 on [0,1]:\n\n");
j = trapezoid(10, 0, 1, fun);
printf("Trapezoidal \t= %G\n", j);
j = rect(10, 0, 1, fun);
printf("Rectangle \t= %G\n", j);
j = simpson(10, 0, 1, fun);
printf("Simpson \t= %G\n", j);
j = gauss(10, 0, 1, fun);
printf("Gauss \t\t= %G\n", j);
fun = &f2;
/* Print the integrals of the second function using all of the available
* methods including trapezoidal, rectangle, Simpson and Gauss
*/
printf("\nIntegrals of x^3 on [0,1]:\n\n");
j = trapezoid(10, 0, 1, fun);
printf("Trapezoidal \t= %G\n", j);
j = rect(10, 0, 1, fun);
printf("Rectangle \t= %G\n", j);
j = simpson(10, 0, 1, fun);
printf("Simpson \t= %G\n", j);
j = gauss(10, 0, 1, fun);
printf("Gauss \t\t= %G\n", j);
fun = &f3;
/* Print the integrals of the third function using all of the available
* methods including trapezoidal, rectangle, Simpson and Gauss
*/
printf("\nIntegrals of x^5 on [0,1]:\n\n");
j = trapezoid(10, 0, 1, fun);
printf("Trapezoidal \t= %G\n", j);
j = rect(10, 0, 1, fun);
printf("Rectangle \t= %G\n", j);
j = simpson(10, 0, 1, fun);
printf("Simpson \t= %G\n", j);
j = gauss(10, 0, 1, fun);
printf("Gauss \t\t= %G\n", j);
fun = &f4;
/* Print the integrals of the fourth function using all of the available
* methods including trapezoidal, rectangle, Simpson and Gauss
*/
printf("\nIntegrals of e^-x on [0,1]:\n\n");
j = trapezoid(10, 0, 1, fun);
printf("Trapezoidal \t= %G\n", j);
j = rect(10, 0, 1, fun);
printf("Rectangle \t= %G\n", j);
j = simpson(10, 0, 1, fun);
printf("Simpson \t= %G\n", j);
j = gauss(10, 0, 1, fun);
printf("Gauss \t\t= %G\n", j);
fun = &f5;
/* Print the integrals of the fifth function using all of the available
* methods including trapezoidal, rectangle, Simpson and Gauss
*/
printf("\nIntegrals of xe^-x on [0,2]:\n\n");
j = trapezoid(10, 0, 2, fun);
printf("Trapezoidal \t= %G\n", j);
j = rect(10, 0, 2, fun);
printf("Rectangle \t= %G\n", j);
j = simpson(10, 0, 2, fun);
printf("Simpson \t= %G\n", j);
j = gauss(10, 0, 2, fun);
printf("Gauss \t\t= %G\n", j);
fun = &f6;
/* Print the integrals of the sixth function using all of the available
* methods including trapezoidal, rectangle, Simpson and Gauss
*/
printf("\nIntegrals of x^-0.5 on [0,2]:\n\n");
j = trapezoid(10, 0, 2, fun);
printf("Trapezoidal \t= %G\n", j);
j = rect(10, 0, 2, fun);
printf("Rectangle \t= %G\n", j);
j = simpson(10, 0, 2, fun);
printf("Simpson \t= %G\n", j);
j = gauss(10, 0, 2, fun);
printf("Gauss \t\t= %G\n", j);
return 1;
}