In this screencast I am going to show you the solver tool in matlab. Depending upon
what you want to solve matlab has a bunch of built in solvers. If you want to solve
for the roots of a polynomial equation you can do that quite easily. The roots of a single
nonlinear algebraic equation. Systems of linear algebraic, nonlinear algebraic equations. The maximum or minimum
of a function. You can optimize a function of several variables and you can integrate.
I am going to go through each of these and an example. If we have a polynomial there
is a built in root function. In matlab I can put in a vector here into the roots function.
My coefficients I put into a vector roots function. When I push go it gives me the roots
of the polynomial. You see that two of these are imaginary roots. What about solving for
the root of a nonlinear algebraic equation.An example is shown here an we want to find a
root of this equation near x = 1. The first thing I am going to do is set this up in the
matlab editor. I have defined my equation here and I can save this in a directory. I
can close this down and now I am ready do use this is a matlab command window. I can
type it in here as solution = fzero(@f,1). We are going to use the "fzero" because it
is a function of a single variable. I have a function handle of the function I just defined
"@f" and our initial guess is 1. When I press go it cranks out the answer as 0.7836. What
if we have to solve a system of linear equations as shown here. We can do this easily in matlab.
We see that this is set up in the form A*x=b where "A" is the coefficient matrix and "b"
is the resulting vector. I have defined what "A" and "b" are. In the command window I can
do this two ways. I can say x = inv(A) * b or in matlab you can just use the "\" which
would be A\b and you get the same thing. These are the "x1", "x2" and "x3 that satisfy that
system of the three equations and the three unknowns. Now we have a more complex problem
where we have a system of nonlinear equations. We have two equations and two unknowns but
they are nonlinear. The first thing I have done is defined a function z = eqns(x). We
do have two input variables of "x" and "y" but what I am doing is saying I have a vector
of inputs and "x1" is equal to "x" and "x2" is equal to "y". Just an example to show you
that my function works I am going to do the equations of 3,4 and again we have a single
input into that function of equations but it is going to be a vector because we have
"x1" and "x2". We have two components of the "x" vector input and that gives us an answer.
Now we wish to solve that system of the two equations. The first thing I am going to do
is show you how to set this up. You have your solution that is going to be "fsolve" and
then a function handle of "@eqns" so we are using our equations as our function that we
are trying to solve. Then we initial guesses here. This would be x = 0.6 and y = 0.5. Again
we have vector "x" that we are working with so we have to put this into vector form. When
I press enter we see solutions to our two simultaneous equations. If we wish to suppress
all of this output that came out after we ran that command I can type in options = optimset('display',
'off');. When I press enter this will define this options and then I am going to re-input
my equation and I am going to add options at the end. If you do it in this way you can
guaranty that when you press enter it is not going to spit out all of this stuff. Next
matlab can determine the maximum or minimum of a function in one variable. For example
let's say we wanted to find the minimum of this variable in the interval 0 to 5. The
first thing I have done is set up a function of y = fn(x). Using the "fminbnd" function
in matlab I can determine an "x" minimum with "fn" as the function and I put the "@" sign
because it is a handle. Then we are going between 0 and 5. This will calculate the minimum
of "fn" between0 and 5. When I press go it tells me that the minimum occurs at 3.93.
If I wanted to actually determine what that minimum was I could just do fn(min) and that
tells me the minimum of the function between 0 and 5 is about -19. Next you might be interested
in finding the maximum or minimum of two variables. Here we have a function of "s" and "t". We
might want to find out what the maximum "y" value is in a certain interval. We will have
"s" between 0 and 10 and then "t" between 0 and 10. I have a rough plot of this. What
we see is there is a maximum here in the purple. the first thing I have done is set up this
function in the editor as "fopt". Again we are going to put in a vector here so "x" is
actually going to be a vector. You see that "y" which is the output depends upon "x1",
the first component of that vector, and "x2". This is going to be our "s" and our "t". "s"
corresponds to "x1 and "t" corresponds to "x2". Another things you will notice is that
I put a negative here in front of the entire equation. There is not way to calculate the
maximum of a function in matlab. What you have to do is calculate the minimum of the
negative of that function. We are putting negative here to make that the negative function
and then we are going to find the minimum of this function which is the maximum of the
initial function. It is almost like a double negative. Similar to the "fsolve" function
I am going to declare some options here by putting a couple of different things in there.
I am saying that the display is off and algorithm is active-set. Next the "fmincon" is the function
we are going to use to find the minimum of an equation of two variables subject to constraints.
That is where the "con" comes in. If I take a close look at this I have initial estimates
for my "s" and "t". There is a bunch of stuff in here that is optional. Then towards the
end we the constraints on "s" and "t". When I run this and type in "xmax" which is the
output of our "fmincon". "xmax" is going to be 4.49 and 6.20. That corresponds to the
"s" and "t" values that actually provide the maximum of our function or the minimum of
our negative function. Then my max which is actually the second output of the "fmincon"
function is actually -647. Keep in mind that is the minimum if the function so the maximum
of the original function is just going to be the negative of this. The maximum is674
as an "s" value of 4.49 and a "t" of 6.205. The last type of solving technique in matlab
that I am going to talk about is numerical integration. That is estimating the area under
the curve. For example here we have cos(x)*cosh(x) - 1. We want to find the area between 0 and
5. Keep in mind that we are going to have a negative area here because the function
is negative for most of this domain. We can use the "quadl" function in matlab. You can
also use the "quad" function. They are fairly similar it is just that they use different
algorithms. I am going to be using the same function of "fn" that I set up originally.
When I type in area = quadl(@fn,0,5) it tell me that the area is about - 30.