Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
numer-60-2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kittipong Maneewong
numer-60-2
Commits
2f8ed6b1
Commit
2f8ed6b1
authored
Apr 26, 2018
by
Kittipong Maneewong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ส่งงาน week12
parent
121124af
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
0 deletions
+57
-0
Initial-Value.py
week12/Initial-Value.py
+57
-0
No files found.
week12/Initial-Value.py
0 → 100644
View file @
2f8ed6b1
#Bracketing minimum
def
f
(
x
)
:
return
x
**
2
-
2
*
x
+
1
from
matplotlib
import
pyplot
as
plt
import
numpy
as
np
x
=
[
x
/
10
for
x
in
range
(
-
20
,
20
,
1
)]
y
=
[
f
(
xi
)
for
xi
in
x
]
print
(
x
)
print
(
y
)
a
,
b
=-
2
,
2
fa
,
fb
=
f
(
a
),
f
(
b
)
plt
.
plot
(
x
,
y
,
"g-"
)
plt
.
show
()
#next
def
bracket
(
f
,
**
kwargs
):
a
=
float
(
kwargs
[
'a'
])
b
=
float
(
kwargs
[
'b'
])
h
=
float
(
kwargs
[
'h'
])
xi
=
a
oldf
=
f
(
xi
)
xi
+=
h
newf
=
f
(
xi
)
while
newf
<
oldf
:
oldf
=
newf
xi
+=
h
newf
=
f
(
xi
)
xi
-=
h
print
(
'Solution xi'
,
xi
,
f
(
xi
))
bracket
(
f
,
a
=-
2
,
b
=
2
,
h
=
0.01
)
#Golden Section Search
import
math
R
=
(
-
1
+
5
**
0.5
)
/
2
def
n
(
a
=-
2
,
b
=
2
,
ebs
=
10
**-
9
)
:
from
math
import
log
as
ln
return
int
(
-
ln
(
R
)
*
ln
(
eps
/
abs
(
b
-
a
)))
def
search
(
f
,
a
,
b
,
tol
=
1.0e-9
):
nIter
=
n
(
a
,
b
)
0.618033989
C
=
1.0
-
R
# First telescoping
x1
=
R
*
a
+
C
*
b
;
x2
=
C
*
a
+
R
*
b
f1
=
f
(
x1
);
f2
=
f
(
x2
)
# Main loop
for
i
in
range
(
nIter
):
if
f1
>
f2
:
a
=
x1
x1
=
x2
;
f1
=
f2
x2
=
C
*
a
+
R
*
b
;
f2
=
f
(
x2
)
else
:
b
=
x2
x2
=
x1
;
f2
=
f1
x1
=
R
*
a
+
C
*
b
;
f1
=
f
(
x1
)
if
f1
<
f2
:
return
x1
,
f1
return
x2
,
f2
search
(
f
,
-
2
,
2
)
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