Question
Using Python/numpy In this problem, we will work on demonstrating the triangle inequality for norms using numpy and matplotlib. We are given two vectors u
Using Python/numpy
In this problem, we will work on demonstrating the triangle inequality for norms using numpy and matplotlib. We are given two vectors u and v as 1D numpy arrays.
In addition to regular pp-norms, we will use the "0.5-norm" below. By this we mean that you should use the expression for the pp-norm with p=0.5p=0.5. Note that this is not actually a norm.
Do the following:
Compute the 1-norm, 2-norm, 5-norm, and "0.5-norm" of the vector u+v. Store these norms in a 1D array sum_norm in the order of 1, 2, 5, and 0.5.
NOTE: Do NOT use numpy.linalg.norm to compute the norms.
Compute the 1-norm, 2-norm, 5-norm, and the "0.5-norm" of the vectors uu and vv. Store the sum u+v in a 1D array called norm_sum in the order of 1, 2, 5, and 0.5.
Next, we will plot what a ball looks like in each of these norms. Your code should produce the following four plots. (NOTE: Refer to the FAQ section for more information on how to construct these plots.)
a. Figure 1:
Draw a 2D ball (with respect to the 1-norm) with radius u1 and centered around the origin using 500 points.
(That is, all vectors ww, which make up the ball satisfy the equation w1=u1.)
Draw a 2D ball (with respect to the 1-norm) with radius v1 centered around the point uu.
Draw a 2D ball (with respect to the 1-norm) with radius u+v1 centered around origin.
Draw a 2D ball (with respect to the 1-norm) with radius u1+v1 centered around origin.
Using plt.arrow(), plot the vector u+v. Also plot the vector u with vector v such that the start of vector v is the end of vector u.
Pass length_includes_head=True to arrow() as a keyword argument, to prevent the arrows from "sticking out" across the boundary of the norm boundaries.
Save this plot's axes to fig_1 using plt.gca()
For example:
plt.figure() # ...code for the plot... fig_1 = plt.gca()
b. Figure 2: Do everything in part (a) using the 2-norm.
Save this plot to fig_2
c. Figure 3: Do everything in part (a) using the 5-norm.
Save this plot to fig_3
d. Figure 4: Do everything in part (a) using the "0.5-norm".
Save this plot to fig_4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started