MATLAB Files -- Scripts and Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to matlab programming for numerical computations course we are in module 1 introduction to matlab programming this is lecture 1 dash 4 where we are going to cover ah using files in matlab so matlab files come in 2 types scripts and functions and that is what we are going to cover in this particular lecture okay ah we have done this already ah a few times in the few in the previous lecture in order to start editor in matlab we need to type edit followed by the file name when we do that the editor opens a file called file name dot m dot m is ah an extension to save state that these are maa ah matlab co code files matlab code files are return in ascii ah so they are basically readable by any text editor matlab provides an excellent editor which is what you should use for typing any matlab file ah indeed you can use say notepad or any other editor as well which is something that i will not recommend you to do okay so do use matlab editor as we have done before in the previous lectures for more help you can go to the matlabs own website for writing a matlab program the link for which is given over here okay so as i said there are 2 types of matlab files script files and function files so what we have done in the the ah previous 2 lectures is we have written small snippets of codes in order to calculate multiple things for example fibonacci series or the ba height of the ball at various times so on and so forth okay all that we have done in those 2 lectures ah we have done it using what is known as matlabs script files so what are the script files so script files are nothing but files that contain sequence of matlab commands and these sequence ah these commands sorry are are executed 1 after other as a they were typed on the command prompt itself so let us look at the files that we had created so this was the the file that we had created for ah ah the trajectory of a ball thrown vertically upwards so these commands are executed 1 after the other when we click on run or we type the name of the file and press enter in the command prompt likewise we can look at fibo using while or fibo using for this these are what are known as script files the difference between script file and a function file is that function file starts with the first line starts with a command called function so that the syntax for a function file is going to be the function space followed by square bracket separated output variables so say out 1 out 2 and so on equal to the name of the function myfunctionname or any name that you choose followed by input variables in 1 in 2 so on and so forth okay keep in mind that the input variables are in circular brackets the output variables are in square brackets having an output variable or an input variable is optional so if you do not have output variable the function is just going to look like function myfuncfunctionname with aah ah input variables which are comma separated input variables are also optional if you have output variables but no input variables you can just say aah out 1 out 2 equal to myfunctionname and let us say you do not have either input variables or output variables you can say myfunc function equal to myfunctionname okay the way matlab functions and scripts work are something that i am going to cover in this particular lecture but at any point of time if you feel the need to write a function of this type you should really not be using a function but what you really instead need is script okay when you save this function okay matlab will ask you to save it with the exact same name as the name of the function so keep this in mind as for as this particular course is concerned the name of your function file will be the same as the name of the function that you are using in that function file okay this is going to work for all the functions that we are going to use in this particular lecture okay i am going to close this particular function file go back to matlab and del delete that file name myfunctionfile i will just click right click over here and click on delete and that will delete that particular file okay so function files are files that taken certain inputs execute a sequence of steps and return the output at the end okay the matlab statements are executed in functions own variable space okay and once we exit that function all the variables used are no longer accessible in matlab let us go back to matlab and take the example of fibonacci using for loop okay and let us execute that particular command i will c clc and i will clear this screen and i will type fibo using for and where i press enter let us look at all the variables that were there we had declared variable n declared variable fibo and variable i when i will run that particular comma va ahh run the particular file all the 3 variables that were used in the script file are also available in the matlab workspace as as can be seen over here the reason is because a script file shares the same workspace as the thing that was used to call that script file because we call that script file from the command prompt the ve variables used in the script file are also available in the workspace okay so script file is nothing but the commands as if they were executed on of the other in the command prompt okay it will do the exact same thing okay let me clear this okay and what i will do this do now is convert this into a function file in order to do that all i need to do is say function fibo using for and save this okay i am just doing this to show you how a function differs from a script this is something you should not be doing you should not be using a function in this particular way because we want in this particular example we want to execute the commands 1 after the other and for this purpose we need to be using scripts okay so let us go to matlab and now type fibo using for again okay when we type this and press enter you see none of the variables are accessible anymore if i type fibo that variable is not accessible because that variable has a scope and that scope of that was in that particular function so when this function file is called n is defined fibo is defined i is defined computations are done when we exit that function n fibo and i are completely gone and no longer available in the workspace if we want to access that in the workspace we need to use input and output arguments with the function okay so that comes brings me to the next aspect scoping of the variables is a scripts shares the variables with the workspace okay where as function uses the variables in its own workspace so the scoping of the variables used in the function they have a local scope the scoping of the variables in the script is ah the scope is the same as the function on the workspace calling that particular script okay and this functions are going to talk through the thee the workspace to the input and the output variable so as i shown earlier the syntax for a function is basically going to be output aah equal to function name and inputs that there is an error over here the function should come before this and not after the equal to sign okay so we wanted to calculate let us say this calculate the factorial ve ve because this is a single purpose thing we are going to write a script in order to calculate the factorial but if you wanted to write a function in order to calculate a function of this sort we are going to write a function so let us go ahead and write a script for calculate calculating the factorial so let us say edit myfact and that is going to be a script okay calculate factorial of n so n equal to let us say 6 fact value equal to 1 thats what we are going to start with for i equal to 1 colon 6 fact value equal to fact value multiplied by i end okay and this is the script because this does not start with aah function call i am going to go to matlab and type myfact okay and all the variables will be accessible in the workspace as we are ah did shown before and fact value is going to be the factorial value 70 okay the other thing that i would want to do is want to make this a bit more the code to be bore more more better i do not want to use for loop in doing this instead what i am going to do is use the array aah function called product so if i give prod 1 colon 6 that is going to calculate the factorial forming okay so i will go over what this particular command does in a bit that is let us execute myfact in matlab and see what let us clear the vartiables first clear all and clc okay so as you can see in the workspace they are no longer any variables i give the aaah script name my fact and press enter i now have the 2 variables fact value and n the fact values value is 720 as we had seen before okay so let us see what the command product does prod let us say 1 to ff 6 is what it is going to do is hm calculate the product of this particular vector the vector 1 to 6 is going to be nothing but 1 2 3 4 5 6 so lets just type that particular thing here that is nothing but 1 2 3 4 5 6 prod which is operated on this is going to do 1 multiplied by 2 multiplied by 3 multiplied by 4 and so on upto 6 so thats what exactly i am going to get as 720 and thats what the script myfact is doing over here okay so thats the script that was the purpose of this script is to run or execute certain commands in matlab command prompt okay now instead if i want to calculate ah a given function and the function that i wanted to calculate was 0 plus c1x plus c2x square upto cnx to the power n for that i am going to u use ah a function file i will call this edit myfunc okay and function result equal to myfunc n okay and what i wanted to do let us go back over here i wanted to calculate c0 plus c1x plus c2x square and so on for c0 equal to 1 and c1 equal to 1 by 1 c2 equal to 1 by 2 c3 equal to 1 by 3 so on and so forth okay so what i will do is i will create a vector of aah the coefficients call lets call this as a vector capital c equal to 1 comma i will put this in square brackets 1 divided by and i have element by element division 1 to n okay so let us go over this once again okay so c i will just say equal to c not which is 1 okay and after that i need to append 1 by 1 1 by 2 1 by 3 and so on so lets define a vector vec equal to 1 to n so our c is just going to be equal to c comma 1 dot slash vec you recall in the 2nd lecture of this module we had introduced the aah element by element operation dot slash so what this is going to do this is going to be do 1 divided by the 1st element of vec 1 divided by 2nd element of vec 1 divided by 3rd element of vec so on and so forth that is going to create a vector so we are going to have a vector 1 followed by 1 by 2 followed by 1 by 3 upto 1 by n so that gives a vector of coefficients so now i have c0 i have c1 i have c2 and so on what are the multiplicands over here is going to be x to the power 0 x to the power 1 x to the power 2 and so on upto x to the power n so for that let us define a vector a so a is just going to be equal to x to the power and for that power i am going use dot caret vec okay so that is what i am going to get over here what i need is the 1st guy is nothing but 1 x to the power 0 is nothing but 1 so i will append that over here so 1 comma x to the power vec okay and my result is going to be result equal to c dot star a okay so the vector c multiplied by the vector a so that is going to be my result and i will end this with end the command end is an optional in a function okay and i am s going to save this as the file myfunc okay you can see over here that this myfunc is actually highlighted the reason is because i have made an error in naming this the file name is my capital func where as name of the function is small func okay aah recall that in matlab the ah all the commands are hmm a case sensitive so i want to have the function name same as my file name so if click on fix it will change the function name to capital f i will just save this and go to matlab and run myfunc myfunc n and lets say result equal to myfunc n where n was equal to 6 okay okay i get error over here that is because i have not defined my x so i need myfunc n comma x over here and i need to call it with 2 arguments n and x so n was 6 and lets say x was 0 point 1 and i call this particular function and i am going to get this result equal to sum result and that is going to be sum of so it is going to be 1 plus ah ah 0 point 1 divided by 1 0 point 1 square divided by 2 0 point 1 cube divided by 3 so on and so forth and that is going to be my result okay so see what have what has happened is while doing this particular function i have made an error that error was i am calculating c0 i am calculating c1x i am calculating c2x square and so on upto cnx to the power of n but i have not summed at up in order to calculate the function f okay i will go in over here and just change this to say sum c star dot a save this and now i will call this clear clc okay and call result equal to myfunc 6 comma 0 point 1 and i am going to get the result over here okay inadvertently what i ended up the show ended up showing over here also is the way we will do debugging you recall that we got 2 errors 1 was an actual error that stem because i had not defined my input variable x at all which basically meant when it came across this particular comman command what happened was it was not able to calculate because x was not defined let me re go back to how i had return earlier and run this again okay i will run this again and i got that error undefined function or variable x on line number 6 i went to that function on this particular line and realize that i had not defined my x so i went ahead and made that change defined at x and i call that particular function using 2 arguments as shown over here the other error that i had done was i was calculating this particular array c star a but not summing that up when i saw that result which resulted not in a single value which was the sum of that uh aah elements of that array but which was just the 6 elements of that array i realized that i needed to add that sum command over here okay so this is also how we do debugging in matlab okay so lets recap when to use functions and when to use scripts and for beginners that is for most of you guys these are the general guidelines that i have for you okay so you want to use scripts when you wanted to make small calculations such as factorial or basic computing as we did in the marks mark list example in the previous ah in a previous lecture or when we wanted to do plotting of some certain functions which we are going to cover in the next lecture that s when we want to use scripts when do you want to use functions let us say we want to calculate the values r a result as a function of variables t y so on and so forth when we want to calculate r as a function of t comma y and so on that is when we want to use functions the second example when we want to use functions is when we want to pa pass them as ah to matlab functions for example in ode solving example that we saw in dhoni hitting the 6 we are calculating ah function using ah ode 45 in matlab and we wanted to pass that f of t comma y as a function to ode 45 that is another example where we are going to use functions this is something that we are going to cover in module 3 and thereafter we wont even cover this in module 2 just giving you a brief overview of when to use functions third example ah when to use function is lets say we want to calculate ah something as a function of temperature let us say we want to calculate for example the saturation pressure of steam at various temperatures or we want to calculate the a let us say diffusivity of something as a function of concentration or lets say we want to calculate something as a function of voltage and current so those properties which we want to calculate as a function of lets say temperature or concentration current or current so on and so forth that is when we will use functions so these are the 3 cases when we use functions for all other examples for all other purposes we are more likely to use scripts rather than function okay so with that i come to the end of that lect of this lecture what we covered in this lecture is the examples of when to use scripts and when to use functions in order to computations scripts is nothing but sequential execution of certain commands as if you wanted to the execute them ah in the calling function or in the calling workspace ah functions are to be used for achieving a specific goal when you want to calculate for example r as a function of t y and so on and so forth okay so these are the main uses of functions and script in matlab we are going to rely heavily on using functions and scripts in this particular set of ah lectures from module 2 onwards okay so with that i come to the end of this particular lecture ah the next lecture is going to be the last lecture in the introduction module the next lecture is going to covered plotting and program output thank and see you in the next lecture
Info
Channel: MATLAB Programming for Numerical Computation
Views: 178,407
Rating: 4.8589511 out of 5
Keywords: Lec, MATLAB, Files, Scripts, and, Functions
Id: 025XHJHeJtA
Channel Id: undefined
Length: 23min 35sec (1415 seconds)
Published: Fri Dec 18 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.