Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F66382681
optimizer.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Jun 10, 04:33
Size
1 KB
Mime Type
text/x-python
Expires
Wed, Jun 12, 04:33 (2 d)
Engine
blob
Format
Raw Data
Handle
18214349
Attached To
R7554 sp4e_exercices
optimizer.py
View Options
import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
mpl_toolkits.mplot3d
import
Axes3D
from
scipy
import
optimize
def
S
(
x
):
x_1
,
x_2
=
x
return
2
*
x_1
**
2
+
1.5
*
x_2
**
2
+
x_1
*
x_2
-
x_1
-
2
*
x_2
+
6
def
getIterationSteps
(
x
):
x_1
,
x_2
=
x
iterationPoints
.
append
([
x_1
,
x_2
,
S
(
x
)])
def
getMinimum
(
fun
,
x0
,
**
kwargs
):
return
optimize
.
minimize
(
S
,
x0
,
**
kwargs
)
if
__name__
==
"__main__"
:
fig
=
plt
.
figure
()
ax
=
Axes3D
(
fig
)
X_1
,
X_2
=
np
.
meshgrid
(
np
.
linspace
(
-
3
,
3
,
100
),
np
.
linspace
(
3
,
-
3
,
100
))
f
=
np
.
array
(
[
S
((
x_1
,
x_2
))
for
x_1
,
x_2
in
zip
(
np
.
ravel
(
X_1
),
np
.
ravel
(
X_2
))])
F
=
f
.
reshape
(
X_1
.
shape
)
ax
.
plot_surface
(
X_1
,
X_2
,
F
,
cmap
=
'viridis'
)
x0
=
(
1
,
3
)
iterationPoints
=
[]
print
(
getMinimum
(
S
,
x0
,
tol
=
1e-9
,
callback
=
getIterationSteps
))
iterationPoints
=
np
.
array
(
iterationPoints
)
ax
.
plot
(
iterationPoints
[:,
0
],
iterationPoints
[:,
1
],
iterationPoints
[:,
2
],
'ro-'
)
ax
.
set_title
(
"Minimisation from scipy"
)
ax
.
set_xlabel
(
"x"
)
ax
.
set_ylabel
(
"y"
)
Event Timeline
Log In to Comment