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
02f831b4
Commit
02f831b4
authored
Apr 08, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ModSim ass3: Added log function.
parent
6d2f51b7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
7 deletions
+25
-7
modsim/ass3/methods.c
modsim/ass3/methods.c
+22
-7
modsim/ass3/methods.h
modsim/ass3/methods.h
+3
-0
No files found.
modsim/ass3/methods.c
View file @
02f831b4
...
...
@@ -18,6 +18,11 @@ All functions return 0 upon success, or -1 when a computational error
occurs (overflow, NaN or such)
*/
#define LOG(t, y) { \
if( log_func != NULL ) \
log_func(t, y); \
}
int
Euler
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr
f
,
void
*
params
)
{
int
i
,
j
,
intervals
=
(
t1
-
t0
)
/
dt
;
...
...
@@ -27,6 +32,8 @@ int Euler(double t0, double t1, double dt, double *y0, double *y1, int N,
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y0
[
j
];
LOG
(
t0
,
y0
)
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y1
,
dy
,
params
)
)
{
...
...
@@ -34,10 +41,11 @@ int Euler(double t0, double t1, double dt, double *y0, double *y1, int N,
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
{
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y1
[
j
]
+
dy
[
j
]
*
dt
;
// Value can be printed/logged here
}
// Value can be printed/logged here
LOG
(
t
,
y1
);
t
=
t0
+
i
*
dt
;
}
...
...
@@ -54,6 +62,8 @@ int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N,
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y0
[
j
];
LOG
(
t0
,
y0
)
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y1
,
dy1
,
params
)
||
f
(
t
+
dt
,
y1
,
dy2
,
params
)
)
{
...
...
@@ -61,10 +71,11 @@ int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N,
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
{
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y1
[
j
]
+
.
5
*
(
dy1
[
j
]
+
dy2
[
j
])
*
dt
;
// Value can be printed/logged here
}
// Value can be printed/logged here
LOG
(
t
,
y1
);
t
=
t0
+
i
*
dt
;
}
...
...
@@ -81,6 +92,8 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
for
(
j
=
0
;
j
<
N
;
j
++
)
y
[
j
]
=
y1
[
j
]
=
y0
[
j
];
LOG
(
t0
,
y0
)
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y1
,
dy
,
params
)
)
{
...
...
@@ -121,9 +134,11 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
for
(
j
=
0
;
j
<
N
;
j
++
)
{
e4
=
dy
[
j
]
*
dt
;
y1
[
j
]
=
y1
[
j
]
+
(
e1
[
j
]
+
2
*
(
e2
[
j
]
+
e3
[
j
])
+
e4
)
/
6
;
// Value can be printed/logged here
}
// Value can be printed/logged here
LOG
(
t
,
y1
);
t
=
t0
+
i
*
dt
;
}
...
...
modsim/ass3/methods.h
View file @
02f831b4
typedef
int
(
*
f_ptr
)(
double
t
,
double
*
y
,
double
*
dy
,
void
*
params
);
typedef
void
(
*
log_function
)(
double
t
,
double
*
y
);
log_function
log_func
=
NULL
;
int
Euler
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr
f
,
void
*
params
);
...
...
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