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
d5155e87
Commit
d5155e87
authored
Feb 28, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ModSim: Finished assignment 2.5
parent
155a9451
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
1 deletion
+52
-1
modsim/ass2/q5.c
modsim/ass2/q5.c
+2
-1
modsim/ass2/q5.py
modsim/ass2/q5.py
+50
-0
No files found.
modsim/ass2/q5.c
View file @
d5155e87
...
@@ -42,7 +42,8 @@ double f2(double x) {
...
@@ -42,7 +42,8 @@ double f2(double x) {
}
}
#define PRINT_INTEGRAL(func, method, a, b) (printf(#func " from " #a " to " \
#define PRINT_INTEGRAL(func, method, a, b) (printf(#func " from " #a " to " \
#b " using %-19s %.11f\n", #method " method:", integral(&func, &method, a, b)))
#b " using %-19s %.11f\n", #method " method:", \
integral(&func, &method, a, b)))
#define PRINT_GAUSS(func, a, b) (printf(#func " from " #a " to " \
#define PRINT_GAUSS(func, a, b) (printf(#func " from " #a " to " \
#b " using %-19s %.11f\n", "gauss method:", gauss(&func, a, b)))
#b " using %-19s %.11f\n", "gauss method:", gauss(&func, a, b)))
...
...
modsim/ass2/q5.py
0 → 100644
View file @
d5155e87
# Gauss two-point integration formula from:
# <http://kr.cs.ait.ac.th/~radok/math/mat7/step32.htm>
from
math
import
sqrt
,
exp
,
sin
,
pi
def
rectangle
(
fn
,
a
,
b
):
return
(
b
-
a
)
*
fn
((
a
+
b
)
/
2.0
)
def
trapezoidal
(
fn
,
a
,
b
):
return
0.5
*
(
b
-
a
)
*
(
fn
(
a
)
+
fn
(
b
))
def
simpson
(
fn
,
a
,
b
):
return
(
2
*
rectangle
(
fn
,
a
,
b
)
+
trapezoidal
(
fn
,
a
,
b
))
/
3.0
def
gauss
(
fn
,
a
,
b
):
s
=
(
b
-
a
)
/
sqrt
(
3
)
# calculate abscissae
left
=
(
b
+
a
-
s
)
/
2.0
right
=
(
b
+
a
+
s
)
/
2.0
return
(
b
-
a
)
*
(
fn
(
left
)
+
fn
(
right
))
/
2
def
integrate
(
method
,
DX
,
fn
,
a
,
b
):
surface
=
0.0
;
x
=
a
for
i
in
xrange
(
int
((
b
-
a
)
/
DX
)):
x
+=
DX
surface
+=
method
(
fn
,
x
,
x
+
DX
);
return
surface
;
integrals
=
[(
lambda
x
:
exp
(
-
x
),
0
,
1
),
(
lambda
x
:
x
*
exp
(
-
x
),
0
,
2
),
(
lambda
x
:
x
*
exp
(
-
x
),
0
,
20
),
(
lambda
x
:
x
*
exp
(
-
x
),
0
,
200
),
(
lambda
x
:
sin
(
x
),
0
,
8
*
pi
)]
methods
=
[
rectangle
,
trapezoidal
,
simpson
]
DX
=
1e-3
for
i
in
integrals
:
for
method
in
methods
:
print
'integrate from %d to %d using %-11s: %.16f'
\
%
(
i
[
1
],
i
[
2
],
method
.
func_name
,
integrate
(
method
,
DX
,
*
i
))
print
'integrate from %d to %d using %-11s: %.16f'
\
%
(
i
[
1
],
i
[
2
],
'gauss'
,
gauss
(
*
i
))
print
'-------------'
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