Difference between revisions of "EGR 103/Fall 2017/Lab 7"
Jump to navigation
Jump to search
Line 18: | Line 18: | ||
== Chapra 7.23, 7.24, and 7.25(b/c) == | == Chapra 7.23, 7.24, and 7.25(b/c) == | ||
* These problems will require [[MATLAB:Fminbnd_and_fminsearch|fminsearch]] to find the extremes, as well as <code>meshgrid</code> and <code>surfc</code> to make the plots; more on that at [[MATLAB:Plotting_Surfaces|Plotting Surfaces]] | * These problems will require [[MATLAB:Fminbnd_and_fminsearch|fminsearch]] to find the extremes, as well as <code>meshgrid</code> and <code>surfc</code> to make the plots; more on that at [[MATLAB:Plotting_Surfaces|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 |
Revision as of 15:03, 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