Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
2f7c9750
Commit
2f7c9750
authored
Feb 28, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added regula falsi method and two-point gauss.
parent
55549caa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
25 deletions
+73
-25
modsim/ass2/q5.c
modsim/ass2/q5.c
+32
-25
modsim/ass2/regula_falsi.h
modsim/ass2/regula_falsi.h
+41
-0
No files found.
modsim/ass2/q5.c
View file @
2f7c9750
...
...
@@ -19,12 +19,15 @@ double simpson(func_ptr f, double a, double b) {
return
(
2
*
rectangle
(
f
,
a
,
b
)
+
trapezoidal
(
f
,
a
,
b
))
/
3
;
}
#define GAUSS_F(x) (f(x / sqrt(3) * (b - a) / 2 + (a + b) / 2))
double
gauss
(
func_ptr
f
,
double
a
,
double
b
)
{
double
s
=
(
b
-
a
)
/
sqrt
(
3
),
// calculate abscissae
left
=
(
b
+
a
-
s
)
/
2
.
0
,
right
=
(
b
+
a
+
s
)
/
2
.
0
;
return
(
b
-
a
)
*
(
f
(
left
)
+
f
(
right
))
/
2
.
0
;
return
(
b
-
a
)
/
2
*
(
GAUSS_F
(
1
)
+
GAUSS_F
(
-
1
));
//double s = (b - a) / sqrt(3),
// // calculate abscissae
// left = (b + a - s) / 2.0,
// right = (b + a + s) / 2.0;
//return (b - a) * (f(left) + f(right)) / 2.0;
}
double
integral
(
func_ptr
f
,
method_ptr
method
,
double
a
,
double
b
)
{
...
...
@@ -48,31 +51,35 @@ double f2(double x) {
#define PRINT_INTEGRAL(func, method, a, b) (printf(#func " from " #a " to " \
#b " using %-19s %.11f\n", #method " method:", \
integral(&func, &method, a, b)))
#define PRINT_GAUSS(func, a, b) (printf(#func " from " #a " to " \
#b " using %-19s %.11f\n", "gauss method:", gauss(&func, a, b)))
int
main
(
void
)
{
//PRINT_INTEGRAL(f1, rectangle, 0, 1);
//PRINT_INTEGRAL(f1, trapezoidal, 0, 1);
//PRINT_INTEGRAL(f1, simpson, 0, 1);
//puts("");
//PRINT_INTEGRAL(f2, rectangle, 0, 2);
//PRINT_INTEGRAL(f2, trapezoidal, 0, 2);
//PRINT_INTEGRAL(f2, simpson, 0, 2);
//puts("");
//PRINT_INTEGRAL(f2, rectangle, 0, 20);
//PRINT_INTEGRAL(f2, trapezoidal, 0, 20);
//PRINT_INTEGRAL(f2, simpson, 0, 20);
//puts("");
//PRINT_INTEGRAL(f2, rectangle, 0, 200);
//PRINT_INTEGRAL(f2, trapezoidal, 0, 200);
//PRINT_INTEGRAL(f2, simpson, 0, 200);
//puts("");
//PRINT_INTEGRAL(sin, rectangle, 0, 8 * M_PI);
//PRINT_INTEGRAL(sin, trapezoidal, 0, 8 * M_PI);
//PRINT_INTEGRAL(sin, simpson, 0, 8 * M_PI);
PRINT_INTEGRAL
(
f1
,
rectangle
,
0
,
1
);
PRINT_INTEGRAL
(
f1
,
trapezoidal
,
0
,
1
);
PRINT_INTEGRAL
(
f1
,
simpson
,
0
,
1
);
PRINT_GAUSS
(
f1
,
0
,
1
);
puts
(
""
);
PRINT_INTEGRAL
(
f2
,
rectangle
,
0
,
2
);
PRINT_INTEGRAL
(
f2
,
trapezoidal
,
0
,
2
);
PRINT_INTEGRAL
(
f2
,
simpson
,
0
,
2
);
PRINT_GAUSS
(
f2
,
0
,
2
);
puts
(
""
);
PRINT_INTEGRAL
(
f2
,
rectangle
,
0
,
20
);
PRINT_INTEGRAL
(
f2
,
trapezoidal
,
0
,
20
);
PRINT_INTEGRAL
(
f2
,
simpson
,
0
,
20
);
PRINT_GAUSS
(
f2
,
0
,
20
);
puts
(
""
);
PRINT_INTEGRAL
(
f2
,
rectangle
,
0
,
200
);
PRINT_INTEGRAL
(
f2
,
trapezoidal
,
0
,
200
);
PRINT_INTEGRAL
(
f2
,
simpson
,
0
,
200
);
PRINT_GAUSS
(
f2
,
0
,
200
);
puts
(
""
);
PRINT_INTEGRAL
(
sin
,
rectangle
,
0
,
8
*
M_PI
);
PRINT_INTEGRAL
(
sin
,
trapezoidal
,
0
,
8
*
M_PI
);
PRINT_INTEGRAL
(
sin
,
simpson
,
0
,
8
*
M_PI
);
PRINT_GAUSS
(
sin
,
0
,
8
*
M_PI
);
return
0
;
}
modsim/ass2/regula_falsi.h
0 → 100644
View file @
2f7c9750
#include "func_ptr.h"
double
regula_falsi
(
func_ptr
f
,
double
s
,
double
t
,
double
e
,
int
*
steps
,
int
m
)
{
int
n
,
side
=
0
;
double
r
,
fr
,
fs
=
f
(
s
),
ft
=
f
(
t
);
for
(
n
=
0
;
n
<
m
;
n
++
)
{
r
=
(
fs
*
t
-
ft
*
s
)
/
(
fs
-
ft
);
if
(
fabs
(
t
-
s
)
<
e
*
fabs
(
t
+
s
)
)
break
;
fr
=
f
(
r
);
if
(
fr
*
ft
>
0
)
{
t
=
r
;
ft
=
fr
;
if
(
side
==
-
1
)
fs
/=
2
;
side
=
-
1
;
}
else
if
(
fs
*
fr
>
0
)
{
s
=
r
;
fs
=
fr
;
if
(
side
==
1
)
ft
/=
2
;
side
=
1
;
}
else
break
;
}
*
steps
=
n
;
return
r
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment