Question
%% Simplified version of find the minimum of a function % Basically walk down the gradient from one side % Start at t = -10
%% 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);
Step by Step Instructions:+ Start with the script Lab5FindMinStart.m* STEP 1 o Fill in the function and make sure it plots ok (looks like plot below, but without the X's) STEP 2 The while/if structure is already set up for you; you just have to edit the conditions. o -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* * o Set break points to make sure t changes the way you expect* o Plot The Questions o Set 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) Find minimum 400 350 300 250 200 150 100 50 -5 0 10 Starting t is -9.50, minimum t is -0.2x0000, y is 39 Xx7500, fminsearsh - 0.333301, y is 39.666667 Starting t is -10.00, minimum t is 0.XX0000, is 40XX0000, fminsearch - 0.333313, y is 39.666667
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