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
5b4e36b7
Commit
5b4e36b7
authored
Apr 07, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented both Runke-Kutta methods.
parent
27ae34d8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
10 deletions
+85
-10
modsim/ass3/methods.c
modsim/ass3/methods.c
+84
-7
modsim/ass3/methods.h
modsim/ass3/methods.h
+0
-2
modsim/ass3/test.c
modsim/ass3/test.c
+1
-1
No files found.
modsim/ass3/methods.c
View file @
5b4e36b7
...
...
@@ -21,34 +21,111 @@ occurs (overflow, NaN or such)
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
;
double
slope
[
N
],
t
=
t0
;
double
dy
[
N
],
t
=
t0
;
// Initialize with first values (specified)
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y0
[
j
];
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y0
,
slope
,
params
)
)
{
if
(
f
(
t
,
y0
,
dy
,
params
)
)
{
puts
(
"Error calculating slope."
);
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y1
[
j
]
+
slope
[
j
]
*
dt
;
for
(
j
=
0
;
j
<
N
;
j
++
)
{
y1
[
j
]
=
y1
[
j
]
+
dy
[
j
]
*
dt
;
// Value can be printed/logged here
}
t
=
t0
+
i
*
dt
;
}
return
0
;
}
/*
int
RungeKutta2
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr
f
,
void
*
params
)
{
int
i
,
j
,
intervals
=
(
t1
-
t0
)
/
dt
;
double
dy1
[
N
],
dy2
[
N
],
t
=
t0
;
// Initialize with first values (specified)
for
(
j
=
0
;
j
<
N
;
j
++
)
y1
[
j
]
=
y0
[
j
];
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y0
,
dy1
,
params
)
||
f
(
t
+
dt
,
y1
,
dy2
,
params
)
)
{
puts
(
"Error calculating slope."
);
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
{
y1
[
j
]
=
y1
[
j
]
+
.
5
*
(
dy1
[
j
]
+
dy2
[
j
])
*
dt
;
// Value can be printed/logged here
}
t
=
t0
+
i
*
dt
;
}
return
0
;
}
int
RungeKutta4
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr f, void *params)
f_ptr
f
,
void
*
params
)
{
int
i
,
j
,
intervals
=
(
t1
-
t0
)
/
dt
;
double
y
[
N
],
dy
[
N
],
e1
[
N
],
e2
[
N
],
e3
[
N
],
e4
,
t
=
t0
,
half_dt
=
.
5
*
dt
;
// Initialize with first values (specified)
for
(
j
=
0
;
j
<
N
;
j
++
)
y
[
j
]
=
y1
[
j
]
=
y0
[
j
];
// Estimate for each interval
for
(
i
=
0
;
i
<
intervals
;
i
++
)
{
if
(
f
(
t
,
y1
,
dy
,
params
)
)
{
puts
(
"Error calculating first estimate."
);
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
{
e1
[
j
]
=
dy
[
j
]
*
dt
;
y
[
j
]
=
y1
[
j
]
+
.
5
*
e1
[
j
];
}
if
(
f
(
t
+
half_dt
,
y
,
dy
,
params
)
)
{
puts
(
"Error calculating second estimate."
);
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
{
e2
[
j
]
=
dy
[
j
]
*
dt
;
y
[
j
]
=
y1
[
j
]
+
.
5
*
e2
[
j
];
}
if
(
f
(
t
+
half_dt
,
y
,
dy
,
params
)
)
{
puts
(
"Error calculating third estimate."
);
return
-
1
;
}
for
(
j
=
0
;
j
<
N
;
j
++
)
{
e3
[
j
]
=
dy
[
j
]
*
dt
;
y
[
j
]
=
y0
[
j
]
+
e3
[
j
];
}
if
(
f
(
t
+
dt
,
y
,
dy
,
params
)
)
{
puts
(
"Error calculating fourth estimate."
);
return
-
1
;
}
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
}
t
=
t0
+
i
*
dt
;
}
return
0
;
}
*/
}
modsim/ass3/methods.h
View file @
5b4e36b7
...
...
@@ -2,10 +2,8 @@ typedef int (*f_ptr)(double t, double *y, double *dy, void *params);
int
Euler
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr
f
,
void
*
params
);
/*
int
RungeKutta2
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr
f
,
void
*
params
);
int
RungeKutta4
(
double
t0
,
double
t1
,
double
dt
,
double
*
y0
,
double
*
y1
,
int
N
,
f_ptr
f
,
void
*
params
);
*/
modsim/ass3/test.c
View file @
5b4e36b7
...
...
@@ -38,7 +38,7 @@ int f_cos(double t, double *y, double *dy, void *params) {
int
main
(
void
)
{
double
y0
[
1
]
=
{.
0
},
y1
[
1
];
if
(
Euler
(.
0
,
10
.
0
,
.
0001
,
y0
,
y1
,
1
,
&
f_cos
,
NULL
)
)
{
if
(
RungeKutta4
(.
0
,
10
.
0
,
.
00
0001
,
y0
,
y1
,
1
,
&
f_cos
,
NULL
)
)
{
puts
(
"FAIL"
);
}
else
{
printf
(
"y1: %f
\n
"
,
*
y1
);
...
...
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