Difference between revisions of "EGR 103/Fall 2017/Lab 7"

From PrattWiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 30: Line 30:
 
** Second argument is the left end of the bounded range
 
** Second argument is the left end of the bounded range
 
** Third argument is the right end of the bounded range
 
** Third argument is the right end of the bounded range
** '''The bounds are in two different argument!'''
+
** '''The bounds are in two different arguments!'''
 
** If you want to ''maximize'' something, use the negative of the function ar the first argument (so you are minimizing the negative); note however that the function's value as given by fminbnd will have the wrong sign
 
** If you want to ''maximize'' something, use the negative of the function ar the first argument (so you are minimizing the negative); note however that the function's value as given by fminbnd will have the wrong sign
 
* Find a single vector of values of a single variable that minimizes a function within a particular bounded range:
 
* Find a single vector of values of a single variable that minimizes a function within a particular bounded range:
Line 95: Line 95:
 
%% Find minimum with initial guess of -800
 
%% Find minimum with initial guess of -800
 
[MinLoc, MinVal] = fminsearch(@(xi) f(xi), -800)
 
[MinLoc, MinVal] = fminsearch(@(xi) f(xi), -800)
 +
</source>
 +
 +
=== Code for finding minima/maxima of a surface ===
 +
<source lang=matlab>
 +
clear; format long e
 +
%% Make a surface plot with contours
 +
[x, y] = meshgrid(linspace(-7, 7, 50));
 +
f = @(x, y) sin(x).*sin(y).*exp(-sqrt(x.^2+y.^2)/5)
 +
figure(1); clf
 +
surfc(x, y, f(x, y))
 +
xlabel('x'); ylabel('y')
 +
 +
%% Initial guess of 2, -2: close to deepest hole
 +
[MinLoc, MinVal] = ...
 +
    fminsearch(@(r) f(r(1), r(2)), [2 -2])
 +
%% Initial guess of -5, -1: close to not-deepest-hole
 +
[MinLoc, MinVal] = ...
 +
    fminsearch(@(r) f(r(1), r(2)), [-5 -1])
 +
%% Initial guess of 1, 1 and negative of function
 +
[MaxLoc, NotMaxVal] = ...
 +
    fminsearch(@(r) -f(r(1), r(2)), [1 1])
 +
-NotMaxVal
 +
f(MaxLoc(1), MaxLoc(2))
 +
</source>

Latest revision as of 12:43, 19 October 2017

Basic Root-Finding Problems

  • Main thing is to look at MATLAB:Fzero and the different ways of calling it.
  • The sign plots are really helpful.
  • The last equation has three sign changes but only two roots

Basic Min/Max Finding Problems

  • Main thing is to look at MATLAB:Fminbnd and the different ways of calling it.

Chapra Problem 6.16

Chapra 6.20

Chapra 6.21

  • This simply requires using fzero twice - just be careful on how you initialize fzero and also be careful about units for the angles.

Chapra 7.23, 7.24, and 7.25(b/c)

  • These problems will require fminsearch to find the extremes, as well as meshgrid and surfc to make the plots; more on that at Plotting Surfaces

General Notes - Code from Lab

First, you need to think about what it is you are being asked to do:

  • Find a single value of a single variable that sets a function equal to zero (or sets a function equal to a particular value, such that the difference between the function and the particular value is zero):
    • Use fzero
    • First argument is an anonymous function calculating the function you want to be set to zero
    • Second argument is either a valid bracket surrounding the root of interest (preferred) or a single initial guess from which MATLAB will try to build a bracket.
  • Find a single value of a single variable that minimizes a function within a particular bounded range:
    • Use fminbnd
    • First argument is an anonymous function calculating the function you want to minimize
    • Second argument is the left end of the bounded range
    • Third argument is the right end of the bounded range
    • The bounds are in two different arguments!
    • If you want to maximize something, use the negative of the function ar the first argument (so you are minimizing the negative); note however that the function's value as given by fminbnd will have the wrong sign
  • Find a single vector of values of a single variable that minimizes a function within a particular bounded range:
    • Use fminsearch
    • First argument is an anonymous function calculating the function you want to minimize; this function must be of a single variable but you may use different entries within that variable
    • Second argument is an initial guess which must have as many entries as the number of entries used in the function
    • If you want to maximize something, use the negative of the function ar the first argument (so you are minimizing the negative); note however that the function's value as given by fminbnd will have the wrong sign
    • Because you are giving an initial guess versus a boundary, you must be careful in picking the initial guess

Code for finding roots

clear; format long e
%% Define function for finding roots; roots at 0, 0, 0, 10
f = @(t) t.^4-10*t.^3

%% Make a plot of the function and its sign
x = linspace(-3, 11, 1000);
figure(1); clf
subplot(2, 1, 1)
plot(x, f(x), 'k-')
subplot(2, 1, 2)
plot(x, sign(f(x)), 'k-')
axis([-4 12 -1.2 1.2]) % zooms out a little

%% Find roots with brackets (preferred)
[TheRoot, TheValue] = fzero(@(tau) f(tau), [-2 2])
[TheRoot, TheValue] = fzero(@(tau) f(tau), [8 10])

%% Find roots with initial guesses
[TheRoot, TheValue] = fzero(@(tau) f(tau), 1)
[TheRoot, TheValue] = fzero(@(tau) f(tau), 12)

%% Danger: fail to find root with initial guess
[TheRoot, TheValue] = fzero(@(tau) f(tau), 1234)

%% Find where f(x)=1000 by finding zero of f(x)-1000
[TheRoot, TheValue] = fzero(@(tau) f(tau)-1000, [8 12])

Code for finding minima

clear; format long e
%% Define function for finding roots; roots at 0, 0, 0, 10
f = @(t) t.^4-10*t.^3

%% Make a plot of the function and its sign
x = linspace(-3, 11, 1000);
figure(1); clf
subplot(2, 1, 1)
plot(x, f(x), 'k-')
subplot(2, 1, 2)
plot(x, sign(f(x)), 'k-')
axis([-4 12 -1.2 1.2]) % zooms out a little

%% Find minimum for input values between 6 and 10
[MinLoc, MinVal] = fminbnd(@(xi) f(xi), 6, 10)

%% Find minimum for input values between 0 and 5
[MinLoc, MinVal] = fminbnd(@(xi) f(xi), 0, 5)

%% Find minimum with initial guess of 8
[MinLoc, MinVal] = fminsearch(@(xi) f(xi), 8)

%% Find minimum with initial guess of -800
[MinLoc, MinVal] = fminsearch(@(xi) f(xi), -800)

Code for finding minima/maxima of a surface

clear; format long e
%% Make a surface plot with contours 
[x, y] = meshgrid(linspace(-7, 7, 50));
f = @(x, y) sin(x).*sin(y).*exp(-sqrt(x.^2+y.^2)/5)
figure(1); clf
surfc(x, y, f(x, y))
xlabel('x'); ylabel('y')

%% Initial guess of 2, -2: close to deepest hole
[MinLoc, MinVal] = ...
    fminsearch(@(r) f(r(1), r(2)), [2 -2])
%% Initial guess of -5, -1: close to not-deepest-hole
[MinLoc, MinVal] = ...
    fminsearch(@(r) f(r(1), r(2)), [-5 -1])
%% Initial guess of 1, 1 and negative of function
[MaxLoc, NotMaxVal] = ...
    fminsearch(@(r) -f(r(1), r(2)), [1 1])
-NotMaxVal
f(MaxLoc(1), MaxLoc(2))