Example of matlab

Matlab program

00:04
show you how to create and run a basic MATLAB program so if we open up MATLAB
00:11
we'll see a big panel in the middle this is called the command window the command
00:16
window is used to execute commands one by one I'll show you what I mean we can
00:21
create a vector X that goes from 1 to 10 and when I hit enter this vector is
00:27
created in our workspace or I can create a vector Y which is equal to the square
00:33
root of x and when I hit enter this vector is also created or I can even
00:39
create a plot from these two vectors and when I hit enter this figure pops up on
00:45
the front of my screen so as we can see the command window is great if you want
00:50
to execute commands line by line but what if you want to execute multiple
00:55
commands at a time well you can do this by creating a live script let's close
01:02
out of this figure and I'll show you two ways to accomplish this the first way is
01:07
to click new and then live script and when you do this you'll see the live
01:12
editor window pop up on top of the command window here you can type
01:17
multiple commands without executing them right away
01:20
but let's say you already typed a bunch of code in the command window and you
01:25
want to preserve it for when you create your live script how do you do that
01:29
first go to your command history and highlight all the commands you want to
01:34
include in your script I'll pick the 3 that we just typed then right click
01:39
these commands and hit create live script you'll notice that just like
01:44
before the live editor pops up on top of the command window but this time it
01:49
already has the commands that we highlighted now to run this script I
01:54
first have to save my file by going to save save as and giving it a relevant
02:00
name then I'll go to the top here where it says run all and hit that button when
02:07
I do this all of the commands in the script are executed we can see in our
02:11
workspace that 2 vector X&Y were created and right next to our
02:16
code we see the figure that was generated from the plot command based on
02:21
your preference you can choose whether to have these outputs on the side of
02:25
your code or in line with your code these two buttons up here will let you
02:30
switch between the modes I'll switch to in line mode so you can see what it
02:34
looks like okay so the program we wrote so far is pretty simple I want to go
02:39
through a few tasks that you may use while writing something more complex
02:43
task number one is adding comments comments will help make your code
02:48
understandable typically they'll describe what your code is doing you can
02:53
add a comment by typing a percent sign and then typing your comment like this
02:57
another way to organize your code is to add sections to it what do I mean by
03:02
this well when using live scripts in MATLAB
03:06
you don't have to run the entire file you have the option to run your code
03:10
section by section sections will also visually split up your code making it
03:16
easier to read and comprehend to add a section I'll type two percent signs
03:21
followed by a space and the name of my section when I hit enter we'll see a
03:27
visual break in the code that separates the section I just created let's put a
03:32
couple of commands here to show you how to run a specific section of code I'll
03:36
create a vector X with values 1 to 100 and I'll create another vector Y with
03:42
values 101 to 200 and then I'll plot X comma Y and make the plot red now
03:49
instead of hitting run all like I did before
03:52
I'll highlight the section I want to run and hit run section we can see the
03:57
figure that corresponds to the section pop-up next to the code we just type and
04:01
if we click on one of these plot commands we can see that the
04:05
corresponding figure is highlighted this is a really useful feature for
04:09
navigating through your results now we've talked a little bit about
04:14
formatting our code but let's talk about some common tasks and constructs used in
04:19
programming the first thing that we'll talk about is called indexing indexing
04:25
is used to take a subset of or matrix that we already created for
04:30
example let's look at one of the vectors we created earlier why if we open the
04:36
variable editor we can see that each element in Y takes on a different value
04:40
now let's say we want to access the fifth element in this vector we can type
04:46
Y and then in parentheses five to get this number or if we want the seventh
04:52
element we'll type Y parentheses seven close parentheses here's some practical
04:58
examples of tasks that we can do with indexing we can create a new variable a
05:03
which is equal to the fifth element in Y a equals y of five now our workspace
05:10
shows that a is equal to the same value as Y of five or we can change the fifth
05:17
element of Y to be 100 we'll type Y of five equals 100 enter and we can see
05:25
that after executing this command the fifth value of Y has changed okay one
05:31
last example let's say we want to change the first five values of Y to be the
05:36
numbers six through ten we can type Y of 1 colon 5 equals and then in square
05:45
brackets 6 7 8 9 10 when we hit enter we can see that the first 5 values of Y
05:53
have changed accordingly so this covers some of the basics of
05:58
indexing and the next thing I want to talk about is something called the
06:02
if-else statement you'll use an if-else statement if you want to execute a set
06:07
of commands only if a certain expression is true this will be more clear with an
06:12
example let's start by writing a statement that takes in a user input the
06:17
line of code I just typed is going to ask for input from the user and convert
06:22
their answer to a string before proceeding I'll show you what I mean if
06:26
we run this section it asks us did you learn something in the command window
06:32
whatever we type as our answer will be stored in the variable user in as a
06:37
string now that we have our user input will create an if-else statement that
06:43
determines what to do based on that user's input so I'll type it all out and
06:48
then we can go through it starting from the top if the user types Y then the
06:54
first expression user in equals Y is true and will display hurray
07:00
but if the user does not type Y and types n instead then the second
07:07
expression is true and will display oh man finally if the user types anything
07:14
that's not Y or n will display please reread the directions
07:19
so let's actually run the program for the three cases
07:23
I'll hit run section and answer the question with Y for yes the live editor
07:28
executes the code and the if section and displays hurray I'll run it again and
07:34
this time I'll answer the question with n for no this time the live editor
07:40
executes the code in the else if statement and displays oh man I'll run
07:46
it one last time and I'll type something different as my answer now the live
07:51
editor executes the code in the else section and displays please reread the
07:56
directions now one last thing it's important to note that you can only have
08:01
one if and one else in each construct but you can have multiple elsif
08:06
statements for example I can add in the following section now if I run this
08:13
section and respond with Q the third expression is true and the live editor
08:19
displays why did you type Q the last construct that we'll talk about is
08:25
called loops specifically we're going to talk about four loops loops are used to
08:32
repeat a set of commands within your code so I have a function that will help
08:36
us illustrate when this comes in handy the function is called random walk and
08:41
I'll give you a visual explanation of how it works there's a person who's
08:46
standing a certain number of steps away from his home let's just call him LeBron
08:50
in the function LeBron flips a coin and if it
08:54
heads he takes one step towards his home and if it lands tails he takes one step
09:00
away from his home the question is how many coin flips will it take LeBron to
09:06
get back to his home so the input for the function is going to be his starting
09:11
position and when we run the function it automatically simulates his coin flips
09:16
until he reaches his house so the output for the function will be the number of
09:21
coin flips that it took him to get home for this example let's say LeBron always
09:27
starts 3 steps from his house so the input for the function will be 3 if I
09:33
hit run we can see that it took 15 coin flips to get back home let's run it a
09:39
couple more times and see what results we get okay notice how every time the
09:44
result is different and at this point you might be curious if we ran this a
09:49
hundred times what would be the average number of coin flips that it would take
09:54
LeBron to get home or maybe you're just wondering what the entire distribution
09:59
would look like instead of rewriting the same command 100 times we can create a
10:05
loop in this case a for loop so let me show you what I mean I'll type it out
10:10
and then I'll explain what's going on alright so what does all of this mean
10:15
well our first statement is for I equals 1 : 100 in this case I is called an
10:24
index variable it's responsible for counting the number of iterations we go
10:29
through in our loop so in this case we'll iterate through the loop 100 times
10:35
and I will take on values 1 2 3 4 5 6 etc all the way up until 100 everything
10:44
contained between the 4 and the end is going to be repeated 100 times in this
10:51
case our function is within our loop so this is what will be repeated
10:55
I'll walk us through a couple of iterations when we first enter the loop
11:00
I is equal to 1 and then we hit our statement num flips of I is equal to
11:07
random walk of 3 this is going to create a variable
11:11
called num flips remember that AI is equal to one so when we have I in
11:18
parentheses we are indexing into the first element of num flips we're going
11:24
to set that value equal to the output of random walk of three we reach the end of
11:30
our loop so we go back to the top and this time I is equal to two then we'll
11:35
hit our function again this time it will index into the second element of num
11:41
flips and set that value equal to the output of our function we hit the end of
11:47
the loop so we go back to the top and this time I is equal to three again will
11:53
execute the random walk function and this time the result is stored in the
11:57
third element of the vector we keep going through this loop until I is equal
12:02
to 100 and each time we go through it we add one more element to our vector
12:08
num flips so after 100 iterations we'll have a vector that has 100 elements in
12:14
it so let's go ahead and run this and take a look at the results in the
12:18
variable editor here's our vector with all 100 elements now let's do some basic
12:25
analysis one thing we might want to do is to check the mean of the data we'll
12:31
go back to the command window and use the mean command whoa that seems kind of
12:36
high you might be curious to see why it's so high so let's plot the number of
12:42
flips that it took on each trial just as a sanity check okay we can see that some
12:49
trials took thousands of flips until LeBron got back home so that's
12:53
definitely skewing our mean to the higher side since we have several large
12:58
outliers in our distribution the median may give a better understanding of the
13:02
data central tendency so let's type median of num flips and see what we get
13:08
okay that's a little bit closer to what we want so once we know what kind of
13:13
analysis we want to do we can then take those lines and add them to the live
13:18
editor and then go ahead and save the program
13:22
so that wraps up this video on creating a basic MATLAB program the examples in
13:28
this video we're pretty simple to illustrate the logic but these basic
13:32
principles are applicable for everything from analyzing big data all the way to
13:37
creating a graphical user interface and the best way to get familiar with
13:41
writing MATLAB programs is to practice if you get stuck there's plenty of
13:46
resources on MathWorks comm and you can post your questions in MATLAB answers as
13:51
well thanks for tuning in and I'll see you guys in another video

Comments