Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F81473483
plot.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
Fri, Sep 6, 06:22
Size
1 KB
Mime Type
text/x-python
Expires
Sun, Sep 8, 06:22 (2 d)
Engine
blob
Format
Raw Data
Handle
20574685
Attached To
R9490 Homework_sp4e_Peruzzo_SáezUribe
plot.py
View Options
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
matplotlib
import
cm
from
mpl_toolkits.mplot3d
import
Axes3D
def
plotminimizer
(
f
,
points
,
method
):
"""
Plot the function f and the successive approximations of the minimizer
Parameters
----------
f : scalar function of a vector of 2 variables.
points : numpy ndarray
2-D array of shape (number of iterations + 1, 2) containing the successive approximations
for the solution obtained with the iterations
method : strig
e.g. "CG" for Conjigate gradient, "BFGS" for quasi-Newton method of Broyden,
Fletcher, Goldfarb, and Shanno
"""
fig
=
plt
.
figure
(
1
)
ax
=
fig
.
gca
(
projection
=
'3d'
)
# Remove grid lines (dotted lines inside plot)
ax
.
grid
(
False
)
# evaluating the functions at the points
fp
=
np
.
zeros
(
points
.
shape
[
0
])
i
=
0
for
p
in
points
:
fp
[
i
]
=
f
(
p
)
i
=
i
+
1
# plotting points and lines for each point pair
ax
.
plot
(
points
[:,
0
],
points
[:,
1
],
fp
,
'ro--'
,
linewidth
=
2
,
markersize
=
5
)
# Plot the surface
X
=
np
.
linspace
(
-
3
,
3
,
50
)
Y
=
np
.
linspace
(
-
3
,
3
,
50
)
X
,
Y
=
np
.
meshgrid
(
X
,
Y
)
Z
=
np
.
zeros
((
X
.
shape
[
0
],
X
.
shape
[
1
]))
for
i
in
range
(
X
.
shape
[
0
]):
for
j
in
range
(
X
.
shape
[
1
]):
Z
[
i
,
j
]
=
f
(
np
.
array
([
X
[
i
,
j
],
Y
[
i
,
j
]]))
ax
.
plot_surface
(
X
,
Y
,
Z
,
cmap
=
cm
.
coolwarm
,
linewidth
=
0
,
antialiased
=
False
,
alpha
=
0.2
)
# Plot the contour lines
ax
.
contour
(
X
,
Y
,
Z
,
colors
=
"black"
)
# Add title and axis names
plt
.
title
(
'Convergence of '
+
method
+
' method'
)
plt
.
xlabel
(
'x'
)
plt
.
ylabel
(
'y'
)
plt
.
show
()
Event Timeline
Log In to Comment