I am going to show you a brief introduction to matlab in this screencast. When you open
up matlab it looks something like this. You have a couple of different windows. The first
is the command window which is where everything goes. That is where we are working primarily.
You have this workspace up here. Once we start defining things that is where the different
variables will go. Then we have a command history. You can see that I did some commands
here earlier. We also have a current folder area. You also see that the current folder
goes up here on the top. When we start working with mfiles and functions it is going to be
imperative that we make sure this is set to the right directory. That is actually a common
error. You make a function or script that you want to run and it doesn't work and one
of the primary reasons for that is because you have not set the directory to where your
function is. In matlab you can type in things like x = 5. What you will notice when I press
enter is a couple of things The first is that matlab command window actually echos what
you put in. You can suppress that. If I want to do y = 6 with a semi colon at the end that
suppresses the echo. What you will notice here one the right in the work space window
is that I have defined that variable "x" and "y". You can double click and that will bring
up the variable editor. Right now it brings up this sheet that looks a lot like excel
where we have the 5 which is the value of "x". If I want to change this I can actually
add some more values and in that case "x" would become a vector. If I close this now
and I go back to the matlab window you will notice up here in the workspace "x" is updates
as a vector and if I type in "x" it says that "x" is 5 4 2. I could create a new variable.
I click on this and it brings up this box. I can assign a value of 7. It does not have
to be a single letter. It can be things like temperature = 300 and so on. Let's work through
a quick engineering example. We have a beam hanging off of a hing on a wall of length
"L". At a distance "x" form the wall we have a cable that is connected to the wall of tension
"T" and length "c". We have a mass "M" on the very end of the beam. We can write a equation
for the tension and what we want to do is determine the position "x" to place this cable
such that we minimize the tension. In this particular example we are going to use these
constants. Back over in matlab I can define these variables. What if we just wanted to
determine what the tension was when "x" equals 2. I can type into here x = 2. Then I can
type in the formula for tension. In this case I do not want to suppress the output because
I want it to provide me with the answer. I press enter and this would be the tension
in Newtons on that cable when x = 2. Recall that what we are trying to do is get the distance
"x" that will minimize the tension. We want to test a bunch of different "x" values. Instead
of using "x" as a single value we can input a vector straight into matlab. I am going
to define "x" is going to be equal to 0.5 : 0.1 : 2.5. What we are doing here is saying
"x" goes from 0.5 to 2.5 in increments of 0.1. If I press enter now I am not going to
suppress the output because I want to show you what this looks like. I am going to press
enter and you will see the values of "x". Now I can go back and input the vector "x"
into our equation. Another neat thing you can do in matlab is just press the up or down
arrows to go back to previous entries into the command prompt. A couple of entries ago
I had this formula. Whenever we have vectors and if we are dividing by a vector or multiplying
by a vector or a function of a vector we actually have to put a period before the mathematical
operation. We also have to do the same thing here after the x because we are doing item
by item. The last thing we need a dot here. If you do not have the dot it is just going
to assume you are doing matrix math. I will press enter. What that does is is create this
vector "T". Whenever you are working with vectors in matlab they are row vectors and
not column vectors. You see we have columns 1 through 6 here. Because we have this 1.0e+03
at the top it means this would be 4000 Newtons. If we are trying to minimize that we can look
through here and say the minimum is somewhere around here. This would be our minimum and
that would correspond to "x" of 2.1. Matlab has some pretty nice plotting tools. We can
do plot(x,T) to plot "T" versus "x". It will actually spit out a plot. We can see the minimum
occurs somewhere around 2.1 which is what we got when we just looked at that tension
vector. "clc" will clear the command window and start everything fresh. You will notice
here on the right we still have the value of everything still stored. If you do want
to erase those values you can just do "clear". Now I am going to show you how to work with
symbolics in matlab. The way I do this is I use "syms" and I am going to declare which
ones are going to be symbols. "M", "g", "L", "c", "x", and "T" are all going to be symbols.
What I am going to do is type in the formula and I press enter. What it does is stores
tension as a function of those symbolic variables. This is not quite what we put in here because
you see we have the square root but it is just converting it to the one half power.
We can then define a new variable of "dTdx" which if you have taken calculus you will
notice is the derivative of tension with respect to "x". We are going to define that as diff(T,x).
You will see that this "diff" is a function in matlab. We are going to tell matllab that
"dTdx" is equal to the derivative of "T" with respect to "x". If we are working in symbolics
when I press enter it is actually going to take the derivative of "T" with respect to
"x" and this is what it comes up with. If I want to find the minimum or when "dTdx"
is equal to 0 I am going to solve(dTdx,x). When I press enter it is going to go through
some calculations and it says the answer has two solutions. One of these solutions makes
sense and the other doesn't. The negative solution does not make sense because you can't
have a negative "x". This would be our minimum. If you recall when we plotted this the minimum
we found was about 2.1. Now I am going to say that c = 3. I can put in the formula and
when I press enter it tells us that the "x" value corresponding to that is 2.12 and again
we got about 2.1 when we did this graphically.