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
c36e9ff8
Commit
c36e9ff8
authored
Feb 21, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Worked on ass4.
parent
22ff97f8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
2 deletions
+15
-2
modsim/ass2/q4.c
modsim/ass2/q4.c
+15
-2
No files found.
modsim/ass2/q4.c
View file @
c36e9ff8
...
...
@@ -4,6 +4,8 @@
#include "func_ptr.h"
#define EPSILON 1e-11
#define MAX_STEPS 1000
#define BISEC_STEPS 5
double
bisec_limited
(
func_ptr
f
,
double
left
,
double
right
,
int
max_steps
)
{
int
i
;
...
...
@@ -23,10 +25,13 @@ double bisec_limited(func_ptr f, double left, double right, int max_steps) {
return
mid
;
}
double
newton
(
func_ptr
f
,
func_ptr
df
,
double
x_start
,
int
*
steps
)
{
double
newton
_raphson
(
func_ptr
f
,
func_ptr
df
,
double
x_start
,
int
*
steps
)
{
double
x
=
x_start
,
last_x
=
x
-
1
;
for
(
*
steps
=
0
;
x
!=
last_x
;
(
*
steps
)
++
)
{
if
(
*
steps
==
MAX_STEPS
)
return
NAN
;
last_x
=
x
;
x
-=
f
(
x
)
/
df
(
x
);
}
...
...
@@ -34,6 +39,10 @@ double newton(func_ptr f, func_ptr df, double x_start, int *steps) {
return
x
;
}
/*
* Manually define the functions and their derivatives.
*/
double
f1
(
double
x
)
{
return
x
*
x
-
x
+
2
;
}
...
...
@@ -60,8 +69,12 @@ double df3(double x) {
int
main
(
void
)
{
int
steps
;
double
root
;
printf
(
"f1: %.11f (%d steps)
\n
"
,
newton
(
&
f2
,
&
df2
,
-
100
,
&
steps
),
steps
);
if
(
!
isnan
(
root
=
newton_raphson
(
&
f2
,
&
df2
,
1000000
,
&
steps
))
)
printf
(
"f2: %.11f (%d steps)
\n
"
,
root
,
steps
);
else
printf
(
"f2: could not find a root after %d steps
\n
"
,
steps
);
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