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
cebe0b55
Commit
cebe0b55
authored
Feb 21, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added 3 out of 4 integral functions to ass5.
parent
701929ef
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
2 deletions
+42
-2
modsim/ass2/func_ptr.h
modsim/ass2/func_ptr.h
+1
-1
modsim/ass2/q5.c
modsim/ass2/q5.c
+41
-1
No files found.
modsim/ass2/func_ptr.h
View file @
cebe0b55
typedef
double
(
*
func_ptr
)(
double
x
);
typedef
double
(
*
func_ptr
)(
double
);
modsim/ass2/q5.c
View file @
cebe0b55
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "func_ptr.h"
int
main
(
void
)
{
#define DX 1e-6
typedef
double
(
*
method_ptr
)(
func_ptr
,
double
,
double
);
double
rectangle
(
func_ptr
f
,
double
a
,
double
b
)
{
return
(
b
-
a
)
*
f
((
a
+
b
)
/
2
);
}
double
trapezoidal
(
func_ptr
f
,
double
a
,
double
b
)
{
return
0
.
5
*
(
b
-
a
)
*
(
f
(
a
)
+
f
(
b
));
}
double
simpson
(
func_ptr
f
,
double
a
,
double
b
)
{
return
(
2
*
rectangle
(
f
,
a
,
b
)
+
trapezoidal
(
f
,
a
,
b
))
/
3
;
}
double
integral
(
func_ptr
f
,
method_ptr
method
,
double
a
,
double
b
)
{
int
steps
=
(
int
)((
b
-
a
)
/
DX
),
i
;
double
x
,
surface
=
0
;
for
(
i
=
0
;
i
<
steps
;
i
++
)
surface
+=
method
(
f
,
x
=
a
+
i
*
DX
,
x
+
DX
);
return
surface
;
}
double
f1
(
double
x
)
{
return
pow
(
M_E
,
-
x
);
}
double
f2
(
double
x
)
{
return
x
*
pow
(
M_E
,
-
x
);
}
#define PRINT_INTEGRAL(func, method, a, b) (printf(#func " from " #a " to " \
#b " using " #method " method: %.11f\n", integral(&func, &method, a, b)))
int
main
(
void
)
{
PRINT_INTEGRAL
(
f1
,
rectangle
,
0
,
1
);
PRINT_INTEGRAL
(
f1
,
trapezoidal
,
0
,
1
);
PRINT_INTEGRAL
(
f1
,
simpson
,
0
,
1
);
return
0
;
}
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