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
a23b3172
Commit
a23b3172
authored
Apr 08, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished integration implementations and test file.
parent
ba399f55
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
15 deletions
+34
-15
modsim/ass3/methods.c
modsim/ass3/methods.c
+5
-5
modsim/ass3/test.c
modsim/ass3/test.c
+29
-10
No files found.
modsim/ass3/methods.c
View file @
a23b3172
...
...
@@ -29,7 +29,7 @@ int Euler(double t0, double t1, double dt, double *y0, double *y1, int N,
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y
0
,
dy
,
params
)
)
{
if
(
f
(
t
,
y
1
,
dy
,
params
)
)
{
puts
(
"Error calculating slope."
);
return
-
1
;
}
...
...
@@ -56,7 +56,7 @@ int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N,
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y
0
,
dy1
,
params
)
||
f
(
t
+
dt
,
y1
,
dy2
,
params
)
)
{
if
(
f
(
t
,
y
1
,
dy1
,
params
)
||
f
(
t
+
dt
,
y1
,
dy2
,
params
)
)
{
puts
(
"Error calculating slope."
);
return
-
1
;
}
...
...
@@ -93,7 +93,7 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
y
[
j
]
=
y1
[
j
]
+
.
5
*
e1
[
j
];
}
if
(
f
(
t
+
half_dt
,
y
,
dy
,
params
)
)
{
if
(
f
(
t
+
half_dt
,
y
1
,
dy
,
params
)
)
{
puts
(
"Error calculating second estimate."
);
return
-
1
;
}
...
...
@@ -103,7 +103,7 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
y
[
j
]
=
y1
[
j
]
+
.
5
*
e2
[
j
];
}
if
(
f
(
t
+
half_dt
,
y
,
dy
,
params
)
)
{
if
(
f
(
t
+
half_dt
,
y
1
,
dy
,
params
)
)
{
puts
(
"Error calculating third estimate."
);
return
-
1
;
}
...
...
@@ -113,7 +113,7 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
y
[
j
]
=
y0
[
j
]
+
e3
[
j
];
}
if
(
f
(
t
+
dt
,
y
,
dy
,
params
)
)
{
if
(
f
(
t
+
dt
,
y
1
,
dy
,
params
)
)
{
puts
(
"Error calculating fourth estimate."
);
return
-
1
;
}
...
...
modsim/ass3/test.c
View file @
a23b3172
...
...
@@ -3,12 +3,20 @@
#include <math.h>
#include "methods.h"
#define DT .001
int
f_test_1
(
double
t
,
double
*
y
,
double
*
dy
,
void
*
params
)
{
*
dy
=
1
.
0
;
return
0
;
}
int
f_test_2
(
double
t
,
double
*
y
,
double
*
dy
,
void
*
params
)
{
*
dy
=
t
;
return
0
;
}
int
f_test_3
(
double
t
,
double
*
y
,
double
*
dy
,
void
*
params
)
{
if
(
isnan
(
*
y
)
)
{
return
-
1
;
...
...
@@ -29,20 +37,31 @@ int f_test_4(double t, double *y, double *dy, void *params) {
}
}
int
f_cos
(
double
t
,
double
*
y
,
double
*
dy
,
void
*
params
)
{
*
dy
=
cos
(
t
);
return
0
;
#define INTEGRATE(method, y_start, t0, t1, dy) { \
*y0 = y_start; \
if( method(t0, t1, DT, y0, y1, 1, &dy, NULL) ) \
puts("FAIL"); \
else \
printf("y1 for dy = " #dy " using %-12s %.10f\n", #method ":", *y1); \
}
#define COMPARE(y_start, t0, t1, dy) { \
INTEGRATE(Euler, y_start, t0, t1, dy); \
INTEGRATE(RungeKutta2, y_start, t0, t1, dy); \
INTEGRATE(RungeKutta4, y_start, t0, t1, dy); \
}
int
main
(
void
)
{
double
y0
[
1
]
=
{.
0
},
y1
[
1
];
double
y0
[
1
]
=
{.
0
},
y1
[
1
]
=
{.
0
}
;
if
(
RungeKutta4
(.
0
,
10
.
0
,
.
000001
,
y0
,
y1
,
1
,
&
f_cos
,
NULL
)
)
{
puts
(
"FAIL"
);
}
else
{
printf
(
"y1: %f
\n
"
,
*
y1
);
}
COMPARE
(.
0
,
.
0
,
10
.
0
,
f_test_1
);
puts
(
""
);
COMPARE
(.
0
,
.
0
,
10
.
0
,
f_test_2
);
puts
(
""
);
COMPARE
(
1
.
0
,
.
0
,
5
.
0
,
f_test_3
);
puts
(
""
);
COMPARE
(
1
.
0
,
-
1
.
0
,
1
.
0
,
f_test_4
);
puts
(
""
);
COMPARE
(
-
1
.
0
,
1
.
0
,
10
.
0
,
f_test_4
);
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