Problem Set 1
Due Thursday Jan. 30, 12:30 pm
Problems
Write a function that implements a basic version of Newton’s method for minimizing a function of one variable.
Start with a simple implementation. Use an existing Julia package to implement the finite difference estimates for the gradient and Hessian. Think about the arguments and any defaults, as well as the type of the output. For the moment don’t set the types of the input arguments. You can start by assuming the function takes only one argument and simply returns the value at which the function is minimized.
Now consider returning richer output. Consider using a named tuple, a dictionary, or a struct. What seem like the advantages/disadvantages? Choose one and implement it.
Set up an array and save the progression of values along the optimization path.
(We won’t cover this in class until Tuesday Jan. 28.) Add argument typing to your Newton function. Allow the initial value to be of any numeric type. See what the the type of the return value is for various numeric input types.
Use a ChatBot (
lmarena.aiprovides free access if you don’t want to sign up for the free tier of one of the commercial services) to write the code. Compare it to your code and indicate strengths/weaknesses.
Define the matrix
A = [1:4 5:8 ones(Int64,4)]. Predict the result of performing the following operations in Julia (before checking your answers by running them). Note that the lines are meant to be run one-by-one in the same workspace, so when you change an array, this will affect the subsequent statements.x = A[3,:]B = A[2:2,1:3]A[1,1] = 9 + A[2,3]A[1:3,2:3] = [0 0; 0 0; 0 0]A[1:2,2:3] = [1 3; 1 3]y = A[1:4, 3]A = [A [2 1 7; 7 4 5; ones(Int64,2,3)]]C = A[[1,3],2]D = A[[1,2,4],[1,3,4]]
Experiment with some ways to extract the elements of a vector that correspond to the even indices, i.e.,
x[2],x[4],…Use array functions and vectorization to solve the problems below using only a single line of code for each problem, where
A = reshape((-22:22) .% 11, 9, 5).- Count the number of elements for which \(A_{i,j}^2<10\).
- Create a matrix containing only the columns of
Awhere the first element \(A_{1,j} >= 0\). - Modify
Asuch that all elements that are even are multiplied by 3.
Consider dictionaries, named tuples, and structs. Experiment with
sizeofandpointer_from_objrefto try to understand memory use (including any pointers) of these data structures. Next consider arrays that have homogeneous types and those with heterogeneous types.
Comments