MATLAB Tutorial 1 - Intro and Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys this is Jake I'm one of your TAS for 3eo for this year and based on some feedback from the last couple of years of 304 I've decided to put together a couple of short tutorial videos with the objective of showing you guys some of the basics of MATLAB and some of the key features that we use in this course and on top of that hopefully you make it look a little less daunting it a little more accessible so that you guys don't mind using it and you don't get the factor when we say you have to submit on that loved file so to that end this first video is meant just to be a very brief introduction to opening MATLAB and starting a new script doing things like inserting comments we get into a little more the fundamental stuff like matrix math and loops and plotting and that sort of stuff in a couple other tutorials but with this tutorial along with all the other ones please don't feel like you have to watch it if you watched the first 60 seconds here and you immediately think that this is not going to be helpful then by all means don't feel obliged to stick around so to MATLAB we go so I've already got it open here so you just want to open up MATLAB from wherever directory it happens to be in and you're gonna get a screen that looks like this so I'm using the 2013 version of MATLAB if you don't have it it might look a little different it's got this cool ribbon thing up at the top here now like Microsoft Word does or Excel but it still functions in exactly the same way so the first thing that we're going to do is make an M file or a new script and now this is really important so as a matter of fact there's already a variable over here called important so if I call that up it says never use the command line and that is 100% true so you might sometimes use it just to test out some functions or to try to figure out how you want to calculate something but generally speaking all the work that you do should be in a script and the reasons for doing so are numerous and I'll get into that in a second so what we're going to do for this tutorial is start a new script so up here in the top left corner also by pressing ctrl n you can start a new script so when I click on that I get this blank text file kind of window it's called untitled 3 in this case because I haven't saved it yet you've got a bunch of different options up here and basically this is where you're going to do all of your coding so if you went through second year and you tried to do things in the command line and every time you screwed up you got a bunch of errors and then you had to go back and change it I can understand how that would be immensely frustrating and you would never want to do that in a million years so the benefits of having an EM file the first of all is that it allows you to open close save or adjust and edit your work which is critical so if you've got some assignment hint for 3eo 4 or project and you've got you know say it's a hundred lines of code so you know you've got some stuff that goes on for a while and you tried to do all that in the command window what would never work but the beautiful thing about the script is that you can open it you can save it work on it later if there's a mistake it'll tell you where it is and you can try to find it and fix it so that's another benefit is that it's easy to find mistakes another great thing about a script is that it lets you copy and paste chunks of your own work to use elsewhere so say I have this fancy plot that I like it's like a format and it's takes up ten to fifteen lines of code to make it look all pretty and I used it for assignment one well if I need to make another plot and assignment two why would I write that whole thing out again I'll just open up my old script file copy and paste and then change how I call the variable and I'll get the exact same graph just with the new data which saves me a whole bunch of time it also lets you experiment with different options lets you find the different ways of calculating the same thing if you want it also has a cool function library up here where you can browse through and see all the different functions that you can use while at the same time giving you just an idea of what the syntax is for all of them it also lets you assign variables and make easy changes so say that you go through a whole thing calculating some parameters with something called pressure and then at the end of it all you realize that you know you accidentally typed in the wrong pressure originally well as long as you use the variable throughout you can just change it in the script and it will change everywhere else kind of like in an Excel spreadsheet if you're referring to one cell and you're gonna avoid having to retype or change all those different numbers so aside from that it lets you use your own functions and write your own functions which we're going to get into in a later thing and it's also great for troubleshooting so with that we're gonna get ready by going into some of the basic commands the first thing that I want to talk about is the comment so a comment is a line of code in a script or an EM file that doesn't actually serve a purpose so we insert a comment by starting with a percent sign and all comments will come up as green text and you'll notice as soon as I type that in I got a star up there that means that my current work is unsaved it's a warning from MATLAB so I can write a comment up there say it is like that and when I run this code this line will not be read in so that the reasons that you would have comments are numerous it helps you describe what you're doing to somebody else that might be looking at your work ta hint who's trying to figure out what you're trying to calculate and wants to give you part marks and then also you know co-workers or yourself even if you come back to this at a different time and you say I can't remember what this is well if you have an appropriate comment then hopefully you'll be able to figure it out so you always want to have a lot of proper comments in your code so there's a couple of different ways to do it you can just start it with a percent sign on top of that you can have a line like this and then if you click on the line and press ctrl R it will insert that comment also you can click this button right here now on top of that if you want to uncomment something it's ctrl T or you can click this uncomment button right here so after that we're going to introduce the three most important lines in every single MATLAB code so I'm going to insert a new comment always insert these lines and then what I'm going to do is you know I like to block things off kind of like this plus you know one of the cool things about comments is that you can make your code look amazing just by adding cool structured comments and having everything lined up so I'm going to copy and paste that which is another great thing about the EM file and I'm going to put in the three lines first one oops these clear variables now what this does is it clears all the variables in the current workspace that have a number assigned to them and like just rewrites them all to be blank which is useful at the beginning of every code because if you don't put that in sometimes things get overwritten and you'll get an error or erroneous answers the other thing close all that's gonna close all external windows like figures that you might have open so the last thing you want to do is have an assignment where it generates five figures and then after that you don't close them and you run the script like five times and then you've got 25 windows opened up everywhere that's never good and the last thing is CL see what CL C does is it's going to clear the command window which is this area over here so if I were to type in CLC you'll see everything disappears and we go back there if I type clear variables you'll notice that my important is going to disappear and there you go so we're always going to start with these three lines right here and now for those of you who are wondering what the semicolon does is it suppresses the output of an assignment of a variable so if I said X equals two I get x equals two and it kind of looks stupid but if I say x equals two with a semicolon nothing will happen however X is still over here if I said y equals x times two with a semicolon Y will be there and I can still call it up but it's not going to print the output when I actually input the command which is very useful in terms of not having garbage output over here so after all this we're just gonna do a really quick example just a very basic example inserting a couple of comments and do a couple of quick calculations and that'll be the end of this video after we talk about how to save things in whatever so what I'm gonna do insert this here we're gonna calculate the number of moles in the volume of gas using the ideal gas law just like that so first thing we're gonna do is define the units and then here we go I'm gonna section off this define units area get myself a couple of lines and we want to use PV equals NRT the ideal gas law so we want isolate for n so let's say that you know in this case the pressure is 15 and then over here I'm going to say pressure and maybe I will give it the unit bars and then these vertical bars don't mean anything I'm just kind of putting them in there to denote what the units are so here's V is nine and we'll call this the volume obviously and let's call that in meters cubed and say T for temperature let's call it 365 Kelvin why not and then over here is temperature and then over here we'll call that Kelvin and now we need the gas constant so I looked it up because I'm not that smart to memorize that sort of thing and we got R right there is 8.314 times 10 to the minus 5 I'm gonna move all these over just using the tab there and this is the gas constant and this units to be appropriate with the rest of the code or meters cubed bar and then that's per mole Kelvin so those are the units of that so then I'm gonna delete these extra lines there and now define the unit's so then I'm gonna go down to the next line I'll say perform calculation perhaps and now there are a couple of ways to do this well really only one way but I mean the way that you input it could be different so we'll do it the normal way that you might imagine so N equals P times V divided by and then in brackets cuz everything is divided by its R times T and I'm gonna put the semicolon there but another way to do it if you don't want to use brackets so one of the biggest complaints I have about people's code is that they use too many brackets and it's almost impossible for people to figure out what you're doing so one way to avoid that is to know that just like in a calculator everything is done according to bed mass so another way to do this would be P times V divided by R and then divided by T because that's not going to be like R divided by T like 1 over R over T but instead it's just gonna divide by R and then it's gonna divide by T and you'll see that I get this little warning here this is warning me I don't have a semicolon that's what that means so I'm gonna put a semicolon there so this guy uses brackets and this guy does not so then I'm just going to report the outputs of course this kind of thing you could do in a calculator but whatever so nothing I've done so far actually prints anything out to the command window for me to see the answer it'll calculate the answer it'll come up over here in the workspace but I want to actually print out those outputs so one command is the disk or display I can display n and it will say and equals kind of like it says y equals right here another way for me to do it is to use the F print F command so this is a very useful one so this is how it works it's a it's a function so it's gonna have an input in brackets and the input is in single quotes you type what you want the command window to say so I'm gonna say the number of moles these conditions is and then we want the number to come up right there now what this is going to be is is I'm not actually going to type in something like say n or let's do n 2 in this case because we've already used n because then it's just going to write n 2 because it's inside these single quotes so it's typing out a string so what instead I'm going to do is I'm going to do a percent sign which is kind of like a placeholder in this F printf thing and then I'm going to do a G which means general tso percent G is a placeholder for a number that I'm going to pass in so after this single quote I'm going to put a comma and I'm going to put in n 2 right there and now it knows because the order I put the percent G you say I had several numbers the order that I put the numbers outside that the quotes it knows to put that number in that place does that make makes sense right and then the last thing I'm gonna do here is do slash n which puts us on to a new line if I didn't do this this little double arrow thing would be at the end of my line of text so I'm gonna put in that slash n so that the arrow jumps down so this whole thing should work and should give me exactly what I need so the only thing we have left to do now is to save it and run it so let's go save save as and let's find a suitable location I think I'll put it on my desktop right now I'm gonna call it t1 so a couple of things that I want to point out when saving your file you can't save a MATLAB file with a number and as the first character so if you were to do 3eo 41 that wouldn't work another thing that you can't do is you can't have a space so I have to use an underscore instead you can have a number in it you just can't have the number as the first character so I'm gonna save that as t1 I'm gonna save it so you'll notice right over here our current folder t1 doesn't show up and that's because my current directory from MATLAB is not on the desktop so if I wanted I could change the directory to the desktop in order to see this function or another way to do it is if I go to run this code which will literally run this code from lines 1 to 32 in this case it'll prompt it'll warm me that I'm not in the current folder so what I can do is just say change folder and it'll automatically point itself to the desktop so it's able to run this code now keep in mind that if you want to run several codes all in the same directory say you've got some functions make sure that they're all saved in the same place or else it won't know what to do so I'll click change folder and you can see I'm back on to the desktop and the number of moles of this condition is 4444 or 48.4 3 all right so you can see we're on my desktop I've got some softball and sports files here lots of fun and all of those variables that I calculated are in fact over here and have been defined and they've all got a number I think you can yep you can see them right here they've all got a number associated with them so you can see that N and n 2 even though they were calculated a little differently they end up giving you the exact same number okay so that's the end of this video that went a little longer than I thought but thanks for watching and stay tuned for the second version where we do a little bit of matrix math and do some identifying sizes and that sort of thing so take care
Info
Channel: Numerical Methods ChE McMaster
Views: 50,099
Rating: 4.9537573 out of 5
Keywords:
Id: upD2lwX2bS0
Channel Id: undefined
Length: 18min 26sec (1106 seconds)
Published: Tue Aug 27 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.