Basics of Programming using MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to module 1 of introduction to matlab programming course ah in this module we are primarily going to go over ah basics of matlab and uh introduction to matlab programming, how the matlab ah is laid out and so on okay so this is the first lecture in this module we will cover this module in approximately 5 lectures the first lecture is on matlab basics now this module overall is going to cover ah after matlab basics, we are going to cover arrays so thats the main ah aspect of working in matlab basically arrays and ah matrices so thats the 2nd lecture in this particular module that we are going to cover there after we are going to look at loops and how to use loops for loops and while loops in order to do ah various ah computations ah that will be followed by ah talking about matlab files matlab scripts and functions and ah ah how to use matlab files which ah will require a lot in the rest of the course and finally will talk about plotting and ah outputs in using matlab okay alright so we will go over how to start a matlab session, the layout of the matlab window matlab editor and so on and so far ah all of these is also available on matlabs website the link for which is given right over here so you can also in addition to this video lecture ah i encourage you to also go to matlab website and look at ah the video and getting started with matlab and that is what we are going to cover in this particular lecture ah so lets go and open ah the matlab so i have just created a shortcut over here for matlab in your case you may have to open it using programme files okay so this is how my matlab window looks like ah when I open matlab ah your matlab window may look slightly different and ah however the main components of the window will still remain the same so this window has several components the first is or the most important 1 is what is known as the command window ah so thats lot of times people just display command window and nothing else thats also fine i like to display my current folder as well as the workspace workspace is nothing but the variables that you currently have in the system defined in the system okay you can move all these things around by just clicking and dragging and as you can see you can drag it at various location so if you drag it and put it on the current folder itself ah you will basically have the 2 current folders and workspace as 2 tabs okay and again you can drag this workspace back over here and you can place it next to the command window and then we can have current folder workspace command window and so on again as i said i like to place the workspace variables and my current folder ah next to each other or below each other to the left hand side and thats what will we will do ah again you can resize all these windows as you find fit okay ah so lets now this is the overall layout of any matlab ah window that you might have what we will do go and do is will start a matlab editor as well in order to do that you give a command edit space followed by the file name so you will call this my first file and i press enter and we will be able to open my first file and this is a blank file in matlab ah editor this is where you can put in all your commands all your ah functions and ah and expressions assignments so on and you can use matlab so thats what ah i just wanted to give an introduction to matlab ah now lets look at the various matlab files that i have already created in my folder so currently in my folder i have 3 matlab files ah ball animation ball trajectory and ball trajectory fun that is ball trajectory function if i go to matlab window i cannot see these 3 files and thats because i am in a different directory okay so i can change my directory using the cd command change directory command or i can go and ah because i have ah used this particular ah been to this particular directory before i can look at that in my history so i will just select it from the his history and now i am able to see all all these files okay so let me just go and open ball trajectory file by double clicking it and this is how my file looks like i will go over ah the various basics of this particular file in the power point again okay okay ah this particular file uses the following example ah this is an example of ah ah the famous cricketer ah current captain of indian team mahendra singh dhoni ah trying to hit a ball for a six ah dhoni hits ball with an initial velocity of 35 metres per second at an angle of 45 degrees and the question that we are asking ourselves i have which will solved in matlab is whether the ball is able to cross the boundary rope that is ah 75 metres from ah where dhoni is standing ah in order for this to be registered as a 6 so we want to find out whether the ball lands before 75 metres or after the 75 metre line and we are going to solve this particular problem using the simple equations that we know of and those equations are shown over here ah the distance ah dx by dt depends on the horizontal velocity dy by dt depends on the vertical velocity ah the acceleration in the horizontal direction is the 1 by the air drag and that in the vertical direction is the 1 by ah the gravitational force okay so these are the equations that we have okay and when we put this in matlab and we plot we are going to get a plot of this nature ah this is how the ball the ball trajectory looks like at the end of every second ah or at the end of every 0.1 seconds i am sorry ah and the ball lands approximately 80 metres from where dhoni is standing so _______ and the matlab code that i had quickly shown you earlier ah we will go over that in a minute and i will show how this matlab code runs in order to run this matlab code what we need to do is type the file name ball trajectory over here and press enter and this is going to run and what i have done is also done as small animation of how the ball moves and lands at approximately 81 metres okay once we have run this file this is what we are able to see ah in the workspace all the workspace variables we are now able to see over here and what we see ah over here is that the function has exited and we have come back to the command prompt okay lets go back to what the matlab code looks like and this is how the matlab code looks like the grin green things that you see over here are basically the commands that we have entered in matlab and i am going to go over the various parts of the matlab the over overall structure of this code looks something like this we first have this input block where we are defining parameters and initial conditions we have the computation block where we are setting up the ordinary differential equations and solving them and finally we have the output output block where we are displaying the results and where we are showing the animation the animation is not going to be covered in this particular course we are going to cover rest of this stuff in this course we will go over the things in the input block computation block and output block the main parts of it so what are the main parts main parts ah been highlighted over here the first 1 is a comment a comment starts with a percentage sign and its coloured green in matlab editor window so the comment that starts with 2 percentage signs are basically they mark ah start and end of a section in matlab this is nothing but ah sectioning just to identify for humans matlab ignores this particular command its just for us to identify ah the code or structure the code in a better way the next thing is that we see over here is an assignment assignment is basically you have a variable and ah a constant is been assigned to that variable instead of a constant we can assign any expression to the variable also this is a mathematical expression 35 multiplied by cos pi by 4 that is the mathematical expression over here we will go over what variable ah declarations and mathematical expressions are in this particular module in lecture 1.2 ah over here what we see is a function this function is a plot function and we are calling this function with certain set of arguments again i am not going to go over this ah in this particular lecture we will go over that later on in this module ah in this particular ah example we have called the function and we are not worried about the outputs of that functions so we are just calling this function as is if you want to capture the outputs of this function we are going to call the function and capture the output as shown over here again these are things that we will go over the matlab basics and introduction to matlab in this particular module ah in the subseq subsequent lectures okay so this again was the overall matlab code we are going to ah uh cover various things that are involved in coding of this matlab in this particular course okay so now let us go over what the basic datatypes of matlab are now the 2 main datatypes in matlab are going to be scalars and matrices or arrays or vectors ah so when we talk about scalars scalars are nothing but a single valued ah variable where as arrays or matrices are going to be ah of size m by n where m is the number of rows and n is the number of columns so we will go over some of these basic ah things in this particular lecture and ah in the subsequent lectures in this module okay the most powerful thing about matlab is that it works with matrices and that is what mat in matlab stands for ah matlab ah again we are going to look at how to assign variables ah how to assign scalars how to assign vectors how to assign arrays and matrices and that is shown over here, a is the scalar b is what is known as rom row vector c is the matrix over here and d is what is known as the column vector so let us go and type these things in matlab and see how that works so lets say a equal to square bracket 1 comma 2 comma 5 if we type this close this square bracket and press enter we will now have a vect ah vector a this vector a has ah 1 row and 3 columns over here and that is shown ah as a row vector as seen over here okay if we type b equal to 1semi colon 2 semi colon 5 we are now instead going to get a column vector so that has 3 rows and 1 single column as shown over here so the commas are going to separate various ah elements in a particular row whereas semicolon is going to separate ah ah various rows so that way we can build a matrix as shown over here 1 comma 2 that means thus this is the first row 3 comma 5 thats the 2nd row comma minus 1 comma 0 that becomes the 3rd row so we have 3 rows and 2 columns in this particular matrix ah and this is what we can see over here so and now let us say we wanted to define ah a scalar so let us say q equal to 5 that becomes a scalar okay as you can see over here every time i type a command aahh the results of the command get displayed ah in matlab window i will just clear this matlab ah window so if we dont want to display then we can basically type sorry we can basically type this a and we can end it with a semicolon so if a line is ended with a semicolon ah then echo is been suppressed as you can see the a ah the variable gets assigned to the ah matrix a however we do not see any result on the screen if you want to see the result on the screen we can just type a press enter and we will be able to see the result another thing we can do is go to this workspace click on double click on a and we will be able to see the contents of this particular variable a okay and ahh you can again close this particular window so that ahh we can get back our command window as before okay so this is how to work with your matlab window let us go over the basic mathematical expressions so we want to look at mathematical ______ expressions that work with scalars as well as mathematical vect expressions that are going to work with vectors and matrices so first we will go over the various scalar operations so let us look at the scalar operations over here the standard scalar operations plus minus multiplication division and power thats carried in addition to that logarithm and exponent ahh are ah the in 1 shown in the second line ah power square root sin cos tan asin acos atan ah remainder round ceiling floor these are the vario various ah scalar operations and we have special variables that are shown to the right hand side so let us look at how these variable operations works so let us say we have 3 plus 4 and that is 7 so matlab has computed the value 7 and is sign it has assign that value to a special variable called a and s so if an expression is calculated and you do not assign it to any variable matlab automatically assigns it to a special variable called a and s okay let us now look at the matrix that we had a so if we were to multiply a with a scalar lets say 2 multiplied by a what is going to happen is that each individual element of a will get multiplied with ah by that particular scalar we have over here and this is going to be the result 2 4 and 10 as we expect the result to be we had shown certain other ah commands that was exp that is exponential so if we give exp of a ah this is what we will get as you can see this is ah exponent has ah acted upon each and individual element of a so we get ah exp or e to the power 1 e to the power 2 and e to the power 5 that because a was 1 2 5 and we get back a row vector as seen in the result over here okay if were to do exp of b note that b was a column vector so if you were to do exp of b we will get a column vector as a result of this so we have this ah the result is going to be ah column vector with 3 rows and 1 single column and because we had not assigned it to any variable the special variable a and s gets assigned that particular value okay so talking about special variables there are certain other special variables for example the value of pi if you type pi and press enter we will get the value of pi likewise ah there are other special variables also that we can see ah in this particular ah ah power point slide so eps is machine precision i is the imaginary unit that is ah square root of minus 1 inf is infinity nan is not a number a and s we have already seen before and the other couple of ah there is 1 more thing that you need to know so let us say if i assign the value pi equal to 4 if i do that what is going to happen is from this point onwards the standard value of pi is going to be overridden by my expression so next time when i calculate say pi by 4, i am not going to get pi by 4 but i am going to get 4 divided by 4 that is 1 that is because i have overridden the value of pi by assigning the value over here if you want to cancel this all i have to do do is clear that variable by using the command clear space pi and i am now able to recover the value of the standard value of the pi again that is 3 point 1416 okay now if i want to clear all the variables in the workspace i have to give the command clear or i have to give the command clear all that means everything in the workspace is going to be cleared i give this command everything in the workspace gets cleared as you can see over here and i can give the command clc to clean the screen and my ah command prompt comes back to the top with completely clean screen as you can see over here okay so let us go back to the basic mathematical expressions we have seen the mathe mathematical expressions for scalars and with that we will come to the end of this particular lecture okay as i said earlier 1 of the most powerful features of matlab is that it is going to be able work with matrices and arrays and that is what we are going to cover in the next lecture in this module that is lecture 1 point 2 okay thank you and see you in the lecture 1 point 2
Info
Channel: MATLAB Programming for Numerical Computation
Views: 903,883
Rating: undefined out of 5
Keywords: Lecture, Basics, of, Programming, using, MATLAB, Mathematical expressions, ball trajectory, MATLAB programming.
Id: O41BWhXFu8E
Channel Id: undefined
Length: 20min 0sec (1200 seconds)
Published: Tue Dec 15 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.