这个homework是用Matlab解决弹簧的数学问题等。

AMATH 301 – Spring 2020

Homework #5

Due on Monday, May 11, 2020

Instructions for submitting:

• Problems 1-3 should be submitted to MATLAB Grader. You have 3 attempts for

each problem.

• Problems 4-5 should be submitted to Gradescope. The solutions and the code used

to get those solutions should be submitted as a single pdf. All code should be at the

end of the file. You must select which page each problem is on when you submit

to Gradescope.

(32 points) Problem 1: MATLAB Grader

Consider an object attached to a spring.

The object has a mass of m = 1 kg, the spring has a stiffness of k = 1/4 N/m, and there

is a damping force of 1 N (the spring is critically damped). Let x(t) be a function that

gives the position of the object at time t where x = 0 represents the equilibrium position

of the spring. If the object has an initial position of x(0) = 1/3 and an initial velocity of

v(0) = 17/6, then the position of the object is given by the formula

x(t) = 1

3

e

−t/2 + 3te−t/2

.

We wish to find the maximum value of x(t) and the time tmax at which that value

occurs.

(a) Use fminbnd to find the time tmax at which the spring is most elongated. Use the

range 0 < t < 10. Also find the value of x(tmax) which is how far the spring is

stretched past its equilibrium length when it is most elongated. Create a 1 × 2 row

vector named ans1 that contains the value of tmax as the first component and x(tmax)

as the second component.

Hint: The function fminbnd only finds the minimum of a function. How can you

restate the problem as a minimization problem?

(b) Use golden section search to find tmax by minimizing the same function you minimized

in part (a). Use an initial interval [0, 10] (i.e. a = 0 and b = 10), and stop when

b − a < 10−3

. Create a 1 × 2 row vector named ans2 with the final value of a as the

first component and the final value of b as the second component.

(c) Recall that you can find the maximum of the function x(t) by finding the root of the

function x

0

(t). Use Netwon’s method to find tmax by finding the root of x

0

(t) (where

x(t) is the same function as part (a)). Use an initial guess of t0 = 0 and stop when

|tk+1 − tk| < 10−3

. Your maximum number of iterations should be large enough that

you reach the tolerance. Store the final guess of Newton’s method in the variable

ans3 and the number of iterations in the variable ans4.

Note: You are free to use the Newton function which is posted on the course Canvas

page (see the 4-20 lecture). MATLAB Grader has its own version of that function so

you do not need to have it in your script.

(24 points) Problem 2: MATLAB Grader

Consider the function

f(x, y) = (2 − x)

2 + (y − x

2

)

2

(a) Create a MATLAB function that computes f using only one input (a column vector

[x; y]). Use fminsearch to find the column vector [x; y] that minimizes f and

store it in the variable ans5. Use an initial guess of [0; 5].

(b) We now wish to find the minimum using gradient descent. To start, create a MATLAB function that calculates the gradient ∇f(x, y). The gradient is given by the

following formula

∇f(x, y) =

−2(2 − x) − 4x(y − x

2

)

2(y − x

2

)

.

(c) Recall from class that we wrote the following code to perform one step of gradient

descent.

p = [6; 4]; % The current guess

grad = fgrad(p); % Find which direction to go

phi = @ (t) p – t*grad; % Define the “path”

f of phi = @ (t) f(phi(t)); % Create a function of “heights along path”

tmin = fminbnd(f of phi,0,1); % Find time it takes to reach min height

p = phi(tmin); % Find the point on the path and update your guess

Adapt the code so that it performs up to 10000 iterations of gradient descent and

stops if the infinity norm of the vector grad is less than a tolerance of 10−4

. You can

leave 0 ≤ t ≤ 1 as the range for fminbnd.

(d) Now use your gradient descent code to find the column vector [x; y] that minimizes

the function f(x, y). Use an initial guess of [0; 5]. Store the final guess in the

variable ans6 and the number of iterations in the variable ans7. The initial guess

does not count as an iteration.

(24 points) Problem 3: MATLAB Grader

For this problem, we are still finding the minimum of the function from Problem 2:

f(x, y) = (2 − x)

2 + (y − x

2

)

2

Instead of calculating tmin with fminbnd for every step of gradient descent, you can

use a fixed step size, which we will call tstep, to calculate the next guess in gradient

descent. In order to do this, you can replace the lines of code

phi = @ (t) p – t*grad; % Define the path

f of phi = @ (t) f(phi(t)); % Create a function of “heights along path”

tmin = fminbnd(f of phi,0,1); % Find time it takes to reach min height

p = phi(tmin); % Find the point on the path and update your guess

with the single line of code

p = p – tstep*grad;

where the variable tstep must be defined previously in the code.

(a) Use gradient descent with tstep = 0.01 and an initial guess of [0; 5] to find the

column vector [x; y] that minimizes the function f(x, y). Use a tolerance of 10−4

.

Store the number of iterations in the variable ans8.

(b) Using the same initial guess and tolerance but using tstep = 0.03, find the number

of iterations it takes for gradient descent to converge and store it in the variable ans9.

(c) Repeat the same process with tstep = 0.05 and store the number of iterations in

the variable ans10.

(15 points) Problem 4: Gradescope

Run your code for Problem 2(d) and Problem 3(a). In other words, find the minimum

of f(x, y) using gradient descent with fminbnd and then using gradient descent with

tstep = 0.01. Use the tic and toc commands to time each method. The function

and all of the parameters should be the same as they were previously. You do not need

to time the commands that are used to define the functions f(x, y) or ∇f(x, y). You

also don’t need to time where you set up the parameters for the tolerance or maximum

number of iterations. You can essentially just time what is inside of a loop for each

method.

Report the times for each method. State which is faster and explain why it is faster.

(5 points) Problem 5: Gradescope

Let f(x) be a unimodal function with its minimum between x = a and x = b. Assume

that a < x1 < x2 < b and f(x1) > f(x2). Between which two values is the minimum of

f(x) guaranteed to be?

(a) between x1 and x2

(b) between a and x2

(c) between x1 and b

(d) between a and x1

(e) None of the above

Gradescope Deliverables Your gradecope writeup should contain the following:

• Problem 4: The times of each method and a sentence or two about which is faster

and why.

• Problem 5: A letter corresponding to your answer choice

• Code: Code for problem 4

## 发表评论