Difference between revisions of "EGR 103/Fall 2017/Lab 7"
Line 38: | Line 38: | ||
** 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 | ||
** Because you are giving an initial guess versus a boundary, you must be careful in picking the initial guess | ** Because you are giving an initial guess versus a boundary, you must be careful in picking the initial guess | ||
+ | |||
+ | === Code for finding roots === | ||
+ | <source lang=matlab> | ||
+ | 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]) | ||
+ | </source> | ||
+ | |||
+ | === Code for finding minima === | ||
+ | <source lang=matlab> | ||
+ | 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) |
Revision as of 16:54, 17 October 2017
Contents
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
- This is a one-parameter search problem. There is an examples for a one-parameter searches at MATLAB:Fzero#One_Parameter
Chapra 6.20
- Be sure to run fzero in a loop - see MATLAB:Fzero#One_Parameter for help.
Chapra 6.21
- This simply requires using
fzero
twice - just be careful on how you initializefzero
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
andsurfc
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 argument!
- 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
<source lang=matlab> 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)