0:17

So today we're going to talk about using

Â MATLAB for data manipulation and data analysis,

Â and the core of today's lecture is

Â going to be an example of MATLAB array arithmetic.

Â So I talked a little bit in the first lecture about differences

Â between vectors and arrays, and maybe a lot of that seemed really abstract.

Â Today we're going to show you an example of

Â how we can use MATLAB array arithmetic, and hopefully

Â some of these abstract concepts will become more

Â concrete when we use MATLAB to actually do something.

Â And with this example it's going to show us is, how we can plot results.

Â I's going to show us some prin, some general principles of programming logic.

Â We're going to learn in this example how to manipulate

Â arrays and we're going to also learn this concept called for loop.

Â Once we finish this, I'm going to, we're going to discuss in

Â a little bit more detail some strategies for using for loops.

Â 1:05

So the example that we're going to show is we're

Â going to try to find a, a biologically relevant example.

Â And what we're going to plot is with the is bound receptor as a function of ligand.

Â So the idea here is that you have a receptor, which

Â we call R, that can bind to a ligand called L.

Â So when the ligand and the receptor

Â come together you form a ligand receptor complex.

Â These two come together with an association constant called

Â K plus, and then they come apart with the disassociation constant called K minus.

Â Now this is, this problem has been well studied in a lot of biology and you

Â know, through a lot of through a lot

Â of biochemistry, and people have analyzed it mathematically.

Â And they've determined that in steady-state, you can

Â calculate the bound receptor using the following formula.

Â LR is equal to the R tot times the ligand times KD over the ligand.

Â Where R tot in this case refers to the total number

Â of receptors that you have, and this constant, KD refers to.

Â Is, is defined as the, the off-rate K minus divided by the on-rate K plus.

Â 2:12

So, what we want to do is, we just want to plot this equation here.

Â We want to plot ligand receptor complex versus

Â ligand level for a range of different Kds.

Â So what we're going to do now is we're going to move out of Powerpoint.

Â We're going to move into the MATLAB programming environment.

Â And I'm going to show you how you can plot ligand

Â receptor complex versus ligand for a range of different Kds.

Â Different ways that we can do this and it's going to illustrate the [INAUDIBLE].

Â This slide here shows what we're going to

Â get after we've produced these plots using MATLAB.

Â Again, the equation we're plotting is ligand receptor complex here.

Â As a function of, of free ligand, and also a function of the parameters Rtot,

Â the total amount of receptors, and Kd, which

Â is the association constant for ligand receptor binding.

Â We're going to plot this equation for Rtot equals 20 nanamolar.

Â Ligand is going to vary between zero and two hundred micromolar.

Â And then we're going to plott it for

Â five different values of association constant or the Kd.

Â Ten, 30, 50, 70, and 90 micromolar.

Â 3:18

This plot down here shows what we're going to get.

Â Ligand receptor complex on the Y-axis.

Â Ligand on the X-axis.

Â And you can see five plots, of course

Â binding through the five different values of a Kd.

Â Those of you who are familiar with biochemistry

Â and cell biology, probably already know that the

Â black curve here that rises the most quickly,

Â corresponds to the lowest value of the Kd.

Â And the magenta curve down here.

Â Which rises the most slowly corresponds to the highest value of the Kd.

Â With the others in between.

Â 3:50

So what we want to do now is

Â we want to move to the MATLAB programming environment.

Â And again, we want to exploit MATLAB's functionality to generate this plot.

Â And, in doing so, this is going to illustrate some

Â of the principles of MATLAB programming that we've been discussing.

Â [BLANK_AUDIO]

Â What we're going to do now is we're going to

Â open the MATLAB programming environment, and we're going to demonstrate

Â how you can, I can use the MATLAB programming

Â environment to perform some simple calculations, and generates some plots.

Â So there's two things you can notice here on the desktop of this computer.

Â One is that the, there's a little MATLAB icon down here.

Â We've successfully installed program.

Â And the second we have a folder here on the

Â desktop, called MATLAB code that has four files in it.

Â 4:36

These are called Plot LR.

Â LR for lygand receptor complex, remember that's what we're

Â going to plot, and then version one, two, three and four.

Â So now I'll demonstrate how we can, how we can use

Â MATLAB programming environment to generate the plots that we just showed.

Â 4:57

And there we are.

Â Now, these files we have in here, Plot LR version one,

Â two, three, and four, these are what are called MATLAB scripts.

Â So we want to open these up, and be able to look at them using MATLAB's editor.

Â So we can go to Open, and, or sorry we can go

Â to New, New Script, and you'll see that this editor pops up.

Â This is the MATLAB, script editor that you

Â will use to, to modify your scripts in MATLAB.

Â And now we can take these four files that we have stored on the,

Â on this folder that's in the desktop, and we can drag them into the

Â 5:56

And, and the cleanest one.

Â And in going through these step by

Â step, we're going to illustrate some, some principles

Â that are useful and illustrate some strategies

Â for for using MATLAB for scientific computations.

Â So if you, if you're familiar with programming and you see the first

Â couple of ones and you say, why did he do it that way?

Â He shouldn't have done it that way.

Â That's, that's correct.

Â That's the whole idea.

Â 7:05

For KD equals 70, and for KD equals 90.

Â So we did that, we were able to generate these five just by copying and pasting the

Â text from the MATLAB editor into the, what this,

Â which, which known as the command line down here.

Â That's not always the best way to do it.

Â A better way to do it, is to when you have a, a

Â MATLAB script that you want to run, is to press the Run button here.

Â And you'll see something that pops up.

Â This will say, we're not in this, in the correct folder.

Â This particular folder that you are in is not part of the path.

Â Then what you want to do is you want to change folder.

Â 7:41

And if you look back here on the MATLAB command line,

Â you'll see now we're in the

Â in the correct folder: Users\Coursera\Desktop\MATLAB Code.

Â And if we're down here at the command window,

Â we can type ls, which is the command to list

Â all the files that are in, in the current

Â folder, and you'll see that our four scripts are here.

Â Plot_lr_v1.m, version two, version three and version four.

Â All dot m, the dot m extension in this

Â case tells you that's, that it's a MATLAB file.

Â 8:11

And you can see that it popped up the five windows.

Â Version one, I mean sorry, for Kd equals 90, 70 30, 10.

Â Now one thing you noticed is that you know,

Â each time I ran this, whether I copied and

Â pasted it or whether I, I ran it by

Â pressing by pressing Run, it popped up five windows.

Â Let's do something else.

Â Here's a, here's yet another way to run it.

Â Plot LR version 1.

Â You can just type that at the command line.

Â Because plot LR version one is the name of the script.

Â The way MATLAB interprets this is if you typed the name of the script,

Â in the, the command line, it will execute all the commands there in that script.

Â 8:53

So if you type plot LR version one, again, it will pop up five windows.

Â You notice that every time we run this, it pops up five windows.

Â And, I've been trying to close them in between, but if I hadn't

Â closed them, we would suddenly have 15 plot windows up on the screen.

Â And if you're doing do with this, if you're

Â actively computing using MATLAB and, and putting up plots

Â to generate things, then it's easy to get lost

Â in, in how many different windows you have up.

Â So a helpful command to use is, is Close All.

Â 9:54

We define the total amount of receptor equals 20.

Â Then we define the Kd.

Â The Kd is equal to 10.

Â Now, this line here is really key.

Â Ligand receptor equals receptor total, multiplied by ligand.

Â And then we have a dot divide.

Â And then in the denominator, we have ligand plus Kd.

Â Let's recall what the dot divide means.

Â In the denominator, we have ligand, which has many, many elements in it.

Â In this case, it's in fact 20,001 elements plus Kd.

Â Kd is just the scale.

Â Kd is a number, but it's okay to add a number to to a vector, or to an array.

Â It will just add Kd to every value in, in ligand.

Â But our denominator in this case is going to have 20,001 elements.

Â 11:05

Then we say, make a figure, plot ligand comma ligand receptor comma LR.

Â This is saying, plot ligand on the x axis.

Â Ligand receptor complex in the y axis, that's what we want.

Â We put a title in here, Kd equals ten micromolar.

Â This little underscore here says when we put up

Â Kd equals ten micromolar, make it a, a subscript.

Â And if you looked at the, if you look at the titles on our

Â plots that's a sub Kd is the d is sub-scripted as it should be.

Â Then we say set Kd equals 30.

Â Perform the calculation again.

Â Plot ligand receptor versus ligand receptor complex in a new figure.

Â Do it again for 50, for 70, and for 90.

Â Okay, so this script, it kind of works.

Â Let's run it one more time.

Â [SOUND] Okay.

Â One more, one more piece of piece of programming advice.

Â It's also good to type clear before you, before you run your scripts.

Â That will clear all of your variables.

Â In fact, if you look over here on the work-space, you can see.

Â These are the variables that are currently

Â defined; Kd, ligand ligand receptor complex, and

Â our tot, and when we type clear, all these variables are going to go away.

Â 12:39

And, and that can happen if you don't clear it before you run the script.

Â Maybe it'll run correctly, because you, you

Â did define some variable but when you try

Â to run it later when you haven't defined that variable, it, you'll get an error.

Â So, it's always good to type clear before you before you run your

Â script just to make sure that the script can run on its own.

Â So now what I'm doing is I'm pressing the up arrow.

Â You can see that as I press the up arrow

Â it will scroll through the commands that I previously used.

Â You can also see that the commands I previously used are over here.

Â So if I'm scrolling up I can see plot LR version one, I press Return.

Â And it'll generate these five plots.

Â For Kd equals 90, for Kd equals 70, 50, 30 and 10, et cetera.

Â 13:33

There's a couple of issues with this this particular script as it's run.

Â One is that we have five different plots for different

Â values of Kd and they're all, they're all in different plots.

Â We want to be able to compare how does the curve look for

Â Kd equals ten to how does a curve look for Kd equals 90.

Â And it, right now it doesn't allow us to do that.

Â So that's one problem with the way we did it.

Â And another problem is that we don't have labels on these plots.

Â We have a title.

Â Saying what the Kd is, but we don't actually have labels saying this

Â is what's on the x axis, this is what's on the y axis.

Â 14:25

So now again we, we, let's we close, I think that

Â none of our windows are open but we'll close, we'll press Clear.

Â Now what we want to do instead of version two, we'll type Backspace, and

Â then I'll, instead of version one, I'll type Backspace and I'll type two.

Â Let's do plot LR version two.

Â 14:41

Now that's better, right?

Â Now we have all five of them in different colors, all on the same graph.

Â And we have a, a label of our x axis.

Â This is ligand in units of micromolar.

Â And we have a label on our y axis.

Â This is ligand receptor in units of nanomolar.

Â Let's take a look at script version two to see how we did this.

Â 15:02

Here's version two.

Â First command I have in here is I say colors

Â equals this MATLAB function of repmat, which means repeat matrix.

Â And now I have krgbmc, one col, colon.

Â One comma 300.

Â I, I set this up because sometimes if you have many plots, you

Â want to have them in a, in different colors in a particular order.

Â And this is how MATLAB interprets different colors.

Â K is for black, r is for red, g is for green, b is for magenta,

Â 16:10

So what I've set up here with LR version two is.

Â First time we plot one, I want make it, I want to do it black.

Â Second time I want it to be red.

Â The third time I want it to be green, et cetera.

Â And if you end up with more than six you want it to repeat.

Â You want the seventh one to be black, the eighth one to be red, et cetera.

Â What this repmat will do is will take this

Â string, KRGBMC and it will repeat it 300 times.

Â So if we go to the Command window, and we type colors.

Â 17:19

So in the previous version, in version one, every time

Â we did this calculation, I said make a new figure.

Â And that's why they ended up on new figures.

Â I, I had this figure command.

Â In case you only want to do the figure

Â command once, but we also have to type hold on.

Â That tells MATLAB I'm going to plot more I'm going to do more and

Â I want you to plot it on top of what's already there.

Â 17:41

So now as we go through for Kd equals ten, 30, 50, 70,

Â 90, you see that they all, they all show up on the same plot.

Â That's because we typed hold on, and also

Â because we're not opening a new figure here.

Â The other thing you'll notice is I said plot ligand comma ligand receptor, and

Â I said first time color sub one, sub two, sub three, sub four, sub five.

Â That's how they end up in the five different colors.

Â Black, red, green, blue, and magenta.

Â 18:13

So if we go to our figure, the x label over here tells us this label, right.

Â The y label here tells us this label, and the

Â legend tells us to put a a legend over here.

Â One more thing that you'll notice here that we haven't

Â talked about in the examples we've shown in the lectures.

Â Is this, comma dot dot dot.

Â 18:35

What if you have a really long command?

Â And you want to, and, it's too long to put it all on one line?

Â It's going to stretch way, way across the screen.

Â And you're not going to be able to see it on your screen.

Â You can move you can continue the command on the next line with this dot dot dot.

Â So the way MATLAB interprets that is that okay whatever comes next on

Â the next line is part of the is part of the same command.

Â So version two is clearly a lot better than version one, right?

Â It's better because we can plot all five of them on the same graph.

Â But there's something that you may have noticed about version two

Â that makes it still inadequate for what we want to do.

Â Which is that, I keep typing, if this is the value of Kd and

Â then this is the next value, this is the next value and the next value.

Â And then I have to copy and paste.

Â Calculate ligand receptor complex again, plot it again.

Â Every time we want to do this we have to copy and paste these lines.

Â What if we wanted to not do five values on this.

Â But what if we wanted to do 500.

Â That would get pretty tedious right.

Â We would have to keep copying and pasting it every single time.

Â 20:52

This looks, this is the same.

Â We define color as we define R tot.

Â We design, define ligand the same way.

Â Notice a couple things that are different.

Â One is that, instead of just a variable called Kd

Â that's a scalar, Kd equals ten or Kd equals 50, etc.

Â Now we define a new variable called Kds with an s

Â at the end of it, and how do we define this, 10:20:90.

Â This says start at the number ten.

Â 21:20

Have an increment of 20, and end at the number 90.

Â So Kds will be equal to ten, 30, 50, 70, 90.

Â In fact, we can go back to the Command window, and we can type Kds.

Â And we can see.

Â These are the values.

Â Ten, 30, 50, 70, 90.

Â So now we have a variable called Kds which holds all the different values of Kd.

Â Instead of defining each one by itself.

Â We also type figure and hold on the same way that we did before.

Â But the main thing that's different is this

Â element down here which is called the for loop.

Â 22:09

We are going to talk about for loops at some

Â length, because this is a very important and powerful programming tool.

Â For loops are, are used when you want to repeat some

Â certain calculation, repeat some command over and over and over again.

Â Like I said before when we were discussing version two, it

Â would get really tedious if you wanted to plot 500 of them.

Â Because you would have to keep copying and pasting

Â the same code 500 times and adjusting it each time.

Â 22:41

So the, the way to, define a, a for loop is like this.

Â You say for some variable equals some starting number up to some ending number.

Â And it's often used with a, with a colon like this because.

Â What, what the way MATLAB interprets this is start

Â at the value one, end at the value five.

Â And each time we go through the for loop,

Â there's going to be this variable, i, which is called

Â the index variable of the for loop, which is

Â going to change each time you go through the for loop.

Â So it'll be one, two, three, four, five.

Â Now MATLAB has helpfully highlighted i so we can see how i is used in the for loop.

Â 23:31

This is our way to change Kd each time we go through.

Â Ten, 30, 50, 70, 90 defined by the five values in this variable Kds.

Â This is also a way to plot the five different colors.

Â Before we said color, we manually said plot it

Â in color sub one plot at color sub two etc.

Â This way it will cycle through and the first time it will plot it and the first

Â element of colors the second time it will

Â plot it in the second element of colors etc.

Â 24:17

That is going to be the, the, it's going to hold all of

Â our values for Kd equals ten, Kd equals 30, Kd equals 50, 70, 90.

Â So this figure legend here is, is being defined as we go through the for loop.

Â Remember, you know, the first time through we, we go through the

Â for loop we want to, put this text in our figure legend.

Â Kd equals ten micromolar, the second time through we want to add this.

Â 24:54

This is defined with a, closed bracket at the beginning.

Â And a closed bracket at the end, the same

Â way that we would define a vector of numbers.

Â But in this case, it's defining a vector

Â of, of characters, it's defining a string vector.

Â But it's very similar to what we would do if we were defining a vector of

Â numbers where you'd have a closed bracket at

Â the beginning and a closed bracket at the end.

Â 25:15

The first text we want to have is Kd equals.

Â And the last text we want to have is, is micromolar to indicate the units,

Â but in between we want it to be ten, or 30, or 50, or 70, 90.

Â But, we might want MATLAB to interpret this just as text, right?

Â We don't want MATLAB to interpret ten as equal to the value ten.

Â So, in the middle we've put in this command here.

Â Int 2, at int to str.

Â The way MATLAB interprets this is integer to string.

Â 25:42

So this is taking a number, which in, in this case

Â is the variable Kd, converting it into a string so that

Â MATLAB will interpret this whole thing is a bunch of text

Â to put up on the screen as part of our figure legend.

Â The other thing we've done is we've said is the first time

Â through we want figure legend to be equal to Kd equals ten micromolar.

Â The second time through we want it to be 30, 50, 70, 90, etc.

Â So, we've set figure legend sub i is equal to this text.

Â 26:12

By doing this sub i that's our way of of making sure that

Â the first one will be ten, the second one will be 30, etc.

Â There's one more little twist here that we're not going to go into in depth

Â in this class but this is something MATLAB

Â this is where MATLAB can be pretty powerful.

Â You'll notice that this is not a parenthesis, this is a curly bracket.

Â 26:31

And this is a end curly bracket rather than an end parenthesis.

Â When we want to access the first element of Kds,

Â or the second element of Kds, we just use a parenthesis.

Â Here we're using a curly bracket.

Â This is a special type of object in MATLAB known as a cell array.

Â And, like I said, we are not going to discuss this in depth in the class.

Â This is something that if you want to learn about

Â it, you'll have to read up on, on, on yourself.

Â But cell arrays can be helpful when you don't when each

Â element of the cell array is either of a different type.

Â Or each element of the cell, cell array has a a different has a different size.

Â Actually, let me show you an example of a cell array just

Â so you can get an idea about how these can be powerful.

Â 27:14

We, in the previous lecture, we talked about

Â how you can have a problem with concatenation, right?

Â If you say little a equals 1 to 3,

Â and you say little b equals 5, 3,

Â 2 and then a ; sorry, 5, 3, 2,

Â 8 and then a ;, 2, 1, 9, 23.

Â [SOUND].

Â Forgot to put the equals in there.

Â Now if a is one by three and b is two by four, you can't concatenate these, right?

Â C equals a comma b.

Â You get an error for using horzcat and similarly, if

Â you try to vertically concatenate them, you'll get an error.

Â 28:34

Now if we look at c.

Â The first element of c will be a one by three double.

Â That's the numbers in a.

Â And the second element of c will be a two by four doubled.

Â So cell arrays are a structure that you can use.

Â If you're not, if each element is not going to have the, the same dimensions, or

Â if you could have, you could combine things

Â that are different different types using cell arrays.

Â For instance you could also say, c sub 3 equals string variable.

Â 29:40

So if we look at version three we can see

Â how this is, this is far superior to to version two.

Â Right?

Â We, this for loop is, is very powerful.

Â This really helps our case in terms of not having to

Â type the same text in over and over and over again.

Â But there's a couple, a couple of issues with version three.

Â One is that I define this for loop here for i equals 1 to 5.

Â What if we wanted to add more elements to Kd?

Â Well, we would have to, we'd have to change this number five.

Â Or what if we wanted to have fewer elements of Kd?

Â Well, we'd run through it and get and error

Â because you would have that many elements in, in Kds.

Â There's a second problem with version three that is You know may

Â or may not be a problem depending on on what you need but.

Â What if we wanted to compare let's say you know how much ligand

Â receptor complex do I have at a 100 micro molar of ligand.

Â And I wanted to compare what's my value if Kd equals

Â 10 to what's my to the value of Kd equals 90.

Â Well if we look at our variables in MATLAB,

Â ligand receptor complex is a one by 20,001 double.

Â Ligand is also a one by 20,001 double.

Â So every time we run through the loop, every

Â time we go to a different value of i.

Â We calculated new value of ligand receptor complex, but

Â what happened to the old value of ligand receptor complex?

Â The old value of ligand receptor complex is gone.

Â It's gone forever.

Â So first we calculated for, for ten micromolar.

Â We plot that, but then when we move on and we calculate it

Â for 30 micromolar, whatever we have

Â for ligand receptor complex for ten micromolar.

Â That's all gone.

Â 31:24

So every time we go through, we we calculate a new,

Â value of ligand receptor complex, and the old one has, has disappeared.

Â So if we wanted to, to after the fact, compare these.

Â Compare what's, you know, what's the ratio of the red one to

Â the green one at this point, we wouldn't be able to do it.

Â So now let's move on to plot LR version four,

Â so we can see how we can overcome that limitation.

Â [BLANK_AUDIO]

Â We'll do what we did before, we'll type clear.

Â 32:05

We can see the output's different.

Â But if we look at the code, we'll see how how the code in this case is, is superior.

Â So now let's close version three, and look at version four.

Â Two things here that are, that are much different with two

Â things that are different in version four compared with version three.

Â One is how do we define our for loop.

Â Remember, with version three, we manually said for i equals 1 to

Â 5, which is fine when Kds has exactly five elements in it,

Â but if you want to increase it to more than five, you're going to

Â have to go in and, and change that in your for loop.

Â Well, we defined Kds up here.

Â What we want is we want, we want to run this

Â for loop for as many values as we have of Kd.

Â The number of values we have of Kd is defined, is determined by its length.

Â So if we set our, start our for loop at one

Â and end our for loop at length of Kds, that's going

Â to be a way to adjust as we add more elements

Â in Kds or subtract more elements to get this to work.

Â 33:25

So, what we want to do now is keep all of them.

Â And these are all stored in this value called LR_all.

Â We say LR_all i comma colon equals ligand receptor complex.

Â That's going to say the first time through when i is equal to

Â one, we want this to define the first row of ligand receptor complex.

Â The second time through, we want it to define the second row, etc.

Â So now if we go back to our MATLAB Command

Â window and we look at how ligand receptor all is defined.

Â This is a five by 20,001 double, which

Â means that it's five rows, it has 20,001 columns.

Â And first row it will be all the values for the for Kd equals ten.

Â The second time through will be all the values for Kd equals 30 etc.

Â So this is a way to keep keep track of everything.

Â So now if we wanted it to perform more

Â calculations on LR_all, we would have all of our

Â 34:46

First of all, we learned some of the rules of, of plotting in MATLAB.

Â We learned how to plot in different colors.

Â We learned that when you type figure, a new figure pops up.

Â We learned that you type the command hold on if

Â you want to plot several different outputs on the same figure.

Â And we also learned how to add a legend.

Â Which is the little box over here that tells

Â us which plots correspond to which value of Kd.

Â And how to put on a label for the x axis, and a label for the y axis.

Â 35:25

the, the equation is ligand times total receptor divided by ligand plus Kd.

Â But ligand in this case has many, many different values.

Â It corresponds to all the values from zero to 200 micro, micromolar.

Â So the numerator in this case, ligand times Rtot is is a vector.

Â And the denominator ligand plus KD in this case is also a vector.

Â And therefore, we had to perform an array arithmetic.

Â We had to a dot divide rather than a regular

Â divide because the computation we wanted to perform is many different

Â values of ligand receptor complex for the corresponding value of the,

Â of the numerator divided by the corresponding value of the denominator.

Â So that, this illustrates and I, An instance where

Â you need to use array arithmetic, rather than standard arithmetic.

Â 36:28

We also learned a really important

Â principle of, of programming logic, and we're

Â going to talk a little bit more about this in the next couple of slides.

Â But for loops are something that you can use when you need to

Â perform several calc when you need

Â to perform a series of calculations repeatedly.

Â And then finally we learned this illustrated some of the ways

Â that you can manipulate arrays and you can access parts of arrays.

Â For instance, when we wanted to create an array called multi-dimensional array

Â called LR_all, that held all of

Â the different values for ligand receptor complex.

Â If we wanted to access one row of, of LR_all, we could type in

Â something like this to assign the current value of ligand receptor

Â complex to one row of the multidimensional array, LR_all.

Â So now, now let's talk a little bit more about for loops.

Â These are one of the most commonly used and powerful programming tactics.

Â And these are used to repeat certain calculations several times.

Â This, for loops are one of the things

Â that makes MATLAB so powerful, because you don't

Â have to keep telling it to do the same thing over and over and over again.

Â You don't have to keep clicking.

Â You can just tell it how many times you want it to, to do something.

Â 37:35

So the generic structure of a for loop is like this.

Â You say for, some variable equals, some sequence of numbers.

Â Then within the for loop you have a bunch

Â of MATLAB commands, those are going to get repeated each time.

Â And then when the for loop is over, you

Â type end in order to indicate that it's over.

Â So for example you can say for i equals 1 to 5,

Â output equals 4 times i raised to the second power plus 13.

Â What this will do is it'll calculate and display five values

Â of the variable output according to the formula that you've entered here.

Â 38:18

Further more each time you go through the loop the index variable will be different.

Â The index variable what I, what I mean by the index variable is the

Â variable that It's changed each time, each time you go through the for loop.

Â So, for this example here, the index variable is what we're calling i.

Â That's a very typical you can use whatever variable you want in a for loop but

Â i is a pretty typical value that, that

Â programmers often use when they're writing for loops.

Â And the fact that the index variable's

Â going to be different each time can be exploited,

Â and this is a way you can make each trip through the loop slightly different.

Â So, something that we'll do when we're actually implementing

Â a dynamical models is we might say for i

Â equals 1 to 5, there can be some parameter that can be defined as a function of i.

Â So maybe the first time through the loop it'll be equal to two, the second time

Â through the loop it'll be equal to 4, the third time it'll be equal to six, etc.

Â And then you can run the model with all these different values of this parameter.

Â So the fact that you have this variable called i which is different each time.

Â Is a way that you can make something else be different each time.

Â And that's a way you, you know you, it's very rare

Â that you want to repeat exactly the same thing a hundred times.

Â What'll happen is you might want to repeat something a

Â hundred times but each time through you analyze a different file.

Â Or you, you change something, and by the

Â fact that your index variable changes each time, that's

Â a way to make it a little bit different each time you go through the for loop.

Â Right.

Â So, parameter, in this case, will take on values two, four, six, eight, ten.

Â Here, we see a second example of how one can set up a for loop.

Â In the previous example we, we took, the for loop

Â just went from the number one to the number five.

Â So, our for loop in that case just said, for i equals 1 to 5.

Â But what if you want to have several different values, several different values

Â of your index variable in your for loop, but they're un, unequally spaced?

Â I mean, what if for instance, the values you wanted to test in some sort simulation

Â or in performing some calculation were one, three, 13, 22, and 300.

Â 40:16

You can set those values in a vector.

Â In this case, I'm calling values to test.

Â So this is going to form a one by four array.

Â And then you can say four i equals 1.

Â And now you could say four here, or i equals 1 to 4.

Â But in that case it wouldn't be very flexible, because then if you wanted to

Â add more values to test, or remove values

Â to test, in the dimensions of this changed.

Â Then your for loop wouldn't automatically adjust.

Â But in this case you can say for i equals 1 up to the length of the values to test.

Â Now if you changed the dimensions of values to test your, your for loop

Â would automatically adjust, and then you could

Â say parameter equals values to test sub i.

Â So the first time through the loop, parameter would

Â be equal to one first element of this vector,

Â second time through the loop it would be equal

Â to 13 the second value of this factor, etc.

Â So this is the way you can set up a for loop.

Â 41:09

But have your index variable have the

Â have whatever value whatever number you're performing cal,

Â calculations on can be different each time and

Â it doesn't necessarily have to be equally spaced.

Â [BLANK_AUDIO]

Â So in summary, MATLAB can be used for performing calculations.

Â It can be used for plotting.

Â It can be used for lots of different applications.

Â 41:58

If you're working with an array A, that has dimensions 100 by four,

Â and you also have a vector time, that has dimensions 100 by one.

Â So each column in A, represents a

Â different variable that was measured in your experiment.

Â And each row of A represents a

Â corresponding time point in the vector times.

Â So in other words the first element of time represents

Â time point number one and then you have four values

Â of a, your first row of A represent the the

Â four variables that you're measuring at that particular moment in time.

Â 42:42

And then you say, for i equals 1 to

Â 4, plot time comma A, parenthesis i end parenthesis, end.

Â So, this is not going to produce the desired result.

Â So now I'll give you a few seconds to think

Â about why, and then we'll, then we'll discuss why it

Â doesn't give us the desired result, and how we would

Â fix this to plot what we want it to plot.

Â [SOUND].

Â So the answer.

Â There's two reasons it does not work.

Â One is that, the idea is that you want to plot the

Â first column, the second column, the third column, and the fourth column.

Â But in this example we gave here, we did not instruct MATLAB to

Â take the first column, the second column, the third column, and the fourth column.

Â We're just telling it to take one element.

Â If you say a, comma.

Â 43:28

A parentheses, i end parentheses, this is saying take the first element

Â of a, the second element and the third element, and the fourth element.

Â So it's only instructed to plot a single element of a.

Â And then there's a second problem here.

Â It says that MATLAB has not been instructed to

Â plot in a different color each time through the loop.

Â It's going to plot the first element of a in blue,

Â and it's going to plot the second element of a in blue.

Â And then the third one and then the fourth one.

Â They're all going to be in blue, but we want to plot these in different colors.

Â So how can we modify this simple for loop to get it to do what we want it to do?

Â We see this example here.

Â The correct lines to paste in the Command window would be as follows.

Â 44:03

Again, we define the colors, black, red, green, and blue.

Â And what we say for i equals 1 or 4, we say plot time.

Â A colon, comma, i.

Â That would be the first column of A, the first time through the loop.

Â The second column of A, the second time through the loop, etc.

Â And then we want to plot each one in colors sub i.

Â