Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MatLab Lab5FindMinStart.m %% Simplified version of find the minimum of a function % Basically walk down the gradient from one side % Start at t

MatLab

Lab5FindMinStart.m

%% Simplified version of find the minimum of a function

% Basically walk down the gradient from one side

% Start at t = -10 and keep adding deltaT = 1 to t until the next

% value is bigger

% At which point divide deltaT by 2 and continue...

% until deltaT is smaller than 0.001

clear

clc

clf

% A parabola

% STEP 1 - write the anonymous function from the lab here

% y = @(t) [fill in here]

% Plot it just to see what it looks like

fplot( y, [-10,10] );

hold on;

xlabel('t');

ylabel('y');

title('Find minimum');

%% Setting up before the while loop

% t to start at (save it for fprintf below)

tStart = -10.0;

% How much to keep incrementing t by

deltaT = 1;

% t will change in the while loop

t = tStart;

% Evaluate at t and t+deltaT

yPrev = y(t);

yNext = y(t + deltaT);

% Plot starting point

plot( t, yPrev, 'Xk');

%% The while loop structure

% you have to fill in the actual conditions (where it says CHANGE)

% and set t and deltaT

while false % CHANGE: this to be as long as deltaT is big enough

if true % CHANGE: Next step is BIGGER than previous

% plot in red

plot( t+deltaT, yNext, 'Xr');

% CHANGE: Cut the step size in half

%deltaT = ???

else

% Next step is SMALLER than previous - take one deltaT step

% plot in black

plot( t+deltaT, yNext, 'Xk');

% CHANGE: Take next step...

%t = ??

end

% Re-evaluate yPrev and yNext with the changed t or deltaT values

yPrev = y(t);

yNext = y(t + deltaT);

end

% The t and y you found

fprintf('Starting t is %0.2f, minimum t is %0.6f, y is %0.6f, ', tStart,

t, y(t) );

% fminsearch does a much more sophisticated version of this, but basically

% the same idea - call here to see what *it* would return

tMin = fminsearch(y, t );

fprintf('fminsearch %0.6f, y is %0.6f ', tMin, y(tMin) );

% Add a blue * where the minimum actually is

plot( tMin, y(tMin), '*b', 'MarkerSize', 20);

Problem 3

Find the (approximate) minimum of a function y(t) = 3t2 + 2 t + 40. Start at t=-10 and search in increments of t = t+1 until you hit the bottom (next y value is bigger than the previous, instead of smaller), then keep cutting the search increments in half (0.5, 0.25, etc) until your divisions are less than 0.001.

Question 1: Once you hit the bottom how many steps (at most) will you take at each of the smaller step values (0.5, 0.25, etc?) Why?

Question 2: Why do you get different answers when you start at t = -10 and t = -9.5?

Deliverables:

Plot the next point in RED if it is bigger than the previous point

Plot the next point in BLACK if it is smaller than the previous point

Provide the plot for a starting value of -10

Print out the final t that you found for a starting value of t of -10 and -9.5

Answer the question

Step by Step Instructions:

Start with the script Lab5FindMinStart.m

STEP 1

Fill in the function and make sure it plots ok (looks like plot below, but without the Xs)

STEP 2The while/if structure is already set up for you; you just have to edit the conditions.

Fill in the while condition

Fill in the if condition

Change t where indicated

Change deltaT where indicated

Run the script with startT set to -9.5 and -10.0

Set break points to make sure t changes the way you expect

Plot

The QuestionsSet a break point in the while loop and run it to see what happens

Set a break point in each branch of the if statement

To answer the question, step through the code and see what happens to the values of t, delta t, yPrev and yNext.

Self-check: (plot is for starting t of -9.5)

Starting t is -9.50, minimum t is -0.2X0000, y is 39.XX7500, fminsearch -0.333301, y is 39.666667

Starting t is -10.00, minimum t is 0.XX0000, y is 40.XX0000, fminsearch -0.333313, y is 39.666667

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions