Matlab for the Absolute Beginner Johns Hopkins University

Matlab For The Absolute Beginner Johns Hopkins University-Free PDF

  • Date:16 Mar 2020
  • Views:27
  • Downloads:0
  • Pages:18
  • Size:251.03 KB

Share Pdf : Matlab For The Absolute Beginner Johns Hopkins University

Download and Preview : Matlab For The Absolute Beginner Johns Hopkins University


Report CopyRight/DMCA Form For : Matlab For The Absolute Beginner Johns Hopkins University


Transcription:

Modeling and Statistical Analysis Prof Dilip Asthagiri. 1 The Command Window, There are two windows that you can type stuff into in Matlab The editor. and the command window Knowing how to use these windows can make. coding and debugging your programs much less painstaking. As the name suggests the command window is where you enter. instructions for Matlab to perform It is handy to know a few basic. commands so that you can instruct Matlab to perform basic functions. Listed are some functions which I find very handy to make moving. around the Command Window much easier Make sure that you try these. functions out and are accustomed to them 1, The help function is extremely useful when you are. unfamiliar with a Matlab function Typing in help followed. by your query and pressing enter of course will give an. output describing the function and the syntax required to. use it Try typing in help plot I find this particular help. page very helpful for plotting and you will see yourself using. most of the tips listed on this topic,clc clears all input and output from the Command. Window display giving you a clean screen, clear clears all functions and variables from memory. lookfor this can be quite an useful command If all you. remembered was a fragment of command or topic you can. use lookfor For example lookfor plot will list all. occurrences of the string plot within Matlab documentation. shg brings the current figure window forward if you have. plotted a graph, Up button The up button in the command window returns.
your previously entered statement, home moves the cursor to the upper left corner of the. quit quits Matlab although you can always do it the. amateur way, Note that the words that are to be typed into Matlab are in a different font than the explanatory. Modeling and Statistical Analysis Prof Dilip Asthagiri. Note that you can also type in mathematical problems into. Matlab to solve it, The polynomial s3 6x2 72x 27 represented in MATLAB. software reads,p 1 6 72 27, The roots of this polynomial are returned in a column vector. when you enter, Another very useful function of the command window is copying code.
from the editor window and watching variable values change I shall. explore this later in section 12,2 Variables, A Matlab variable is essentially a tag that you assign to a value while that. value remains in memory The tag gives you a way to reference the value. in memory so that your programs can read it operate on it with other. data and save it back to memory Variables will be the essential dynamic. medium which your program will access change and produce data In. the following example the sign shows what I have typed in the. command window,For instance, Now that you have taught Matlab that monkey 8 every time. you type in monkey Matlab will understand that you are talking. about the number 8 until you enter clear which clears the. memory of all variables, Modeling and Statistical Analysis Prof Dilip Asthagiri. I find the use of an analogy very helpful to drive this concept home Take. the above example We first enter monkey 8 Now imagine that the. variable on the left of the equal sign is a little box and that you are storing. the stuff on the right into the variable on the left You can never swap this. order unlike what you might be used to doing in math. Anytime you call the variable thereafter you call for the value stored. inside However you can change the contents of the box by reassigning. the variable putting or taking out stuff from the box For example. monkey monkey 1 Here Matlab will understand that you are. assigning a new variable monkey a value of 9 by using the old value of. monkey which is 8 Reassigning variables like this will come very much in. handy when we deal with for loops, You can also assign a variable to be an array2 We will look at. manipulating arrays in another section, Changing the name of variables is usually redundant but can be done if.
you type it in the following format new name monkey Now new name. will have a value of 8, Note that Matlab will throw back the value of the variable unless you end. the line with a semi colon Always remember to do this especially. when working on lengthy pieces of code, Also remember that Matlab variable names must begin with a letter. which may be followed by any combination of letters digits and. underscores Matlab distinguishes between uppercase and lowercase. characters so A and a are not the same variable, When naming a variable make sure you are not using a name that is. already used as a function name either one of your own functions or one. of the functions in the Matlab language If you define a variable with a. function name you will not be able to call that function until you either. remove the variable from memory with the clear function For example. if you enter the following command disp 5 you will not be able to use. the Matlab disp function until you clear the variable with clear disp. 3 Editor window, The Editor window is very different from the command window because. here you can make all the mistakes you want and Matlab will never shout. at you It is best to code in the editor window and run the program in the. command window Avoid coding in the command window as it will be a. huge pain to write programs like that,A series of values or a matrix.
Modeling and Statistical Analysis Prof Dilip Asthagiri. The advantage of coding in the editor window is that Matlab tells you. when you make a mistake indicating it by an orange or a red line An. orange line means that you can run the program but Matlab is unhappy. because of your bad coding practice and the red line means that the. program cannot be run because of an error in that line I will try and. explore common sources of error in Matlab in a later section. You also necessarily have to make sure that you have saved your code in. the same directory as what Matlab is looking in when running the code in. the command window I would recommend creating a Matlab folder in a. recognizable region of your hard drive and saving all m read as dot M. files files there When obtaining codes from other people you could just. save it in this directory and run the file in the command window We will. explore running m files in the command window in a later example. If you have a very clear code in your mind you can actually type it all out. using notepad in your email portal or even write it out on a piece of. paper The only reason you actually need Matlab is to see how it all works. and to get your data,4 How to write a program, To make our lives easy we can write a program with user defined. variables This means you can use it like how you use excel In excel you. feed in a formula in a cell and change the input in your input cell and. the program spits out an output, You can do the same thing with Matlab and this section will teach you. To do this we need to make use of the editor window. Problem Add 2 numbers input 1 and input 2,Type the following in the editor window. function solution exmp 4 inp1 inp2,solution inp1 inp2. For the program to work you have to make sure you save the program as. exmp 4 m in the directory that the command window is running in You. can see where Matlab is looking for the m files under the current. directory text field in Matlab, Once you do that you can go to the command window and type in.
solution exmp 4 1 2 or solution exmp 4 124 268 to get. your answer A new variable solution will now appear in your. Workspace window This variable contains your answer. Modeling and Statistical Analysis Prof Dilip Asthagiri. When running programs in the command window you can change the. variable name and it will just replace solution For instance A. exmp 4 inp1 inp2 will give you the same result but your answer will. be saved under the variable A, Alternatively you can just type exmp 4 inp1 inp2 for Matlab to spit out. the answer given that you add the following line in your m file. disp solution Now displaying the solution has become a function of. your code and you don t have to call for the answer Try to accustom. yourself with these techniques because you will be using them for every. code you write See Exercise 1, It is good coding practice and also a good way to avoid orange lines to. preset values for the variables you are planning to use Matlab doesn t like. it but at times will perform the program without complaining when you. show it variables that it has never seen before You have to say what the. initial value of the variable is If it is an array it is good practice to set up. an array of zeros of the required dimension,6 Commenting. Commenting your code is a very important yet undervalued practice You. can always type a sign on your work in the Editor window also possible. in the command window and Matlab will ignore whatever that comes. after that Note that you have to type in the sign for every new line of. comment you insert This is a great way to keep track of what you are. doing and it makes the lives of those who are looking at your code that. much easier like your TAs, You will have to use arrays extensively in your projects and it would be. helpful if you were familiar with the different commands to manipulate. arrays There is a comprehensive list of tricks in the following website. http blinkdagger com matlab matlab tips and tricks on. manipulating 1 d arrays vectors, I would strongly advice you to play around with them before attempting.
to work on the projects, Just to make this article complete I have included a few basic array. manipulation techniques here 3, In addition to doing operations on single numbers Matlab allows us to. perform operations on arrays There are several ways to create an array I. have listed examples in the bullet points, I have modified the following material from http physics gac edu huber matlab mtlabfun htm. Modeling and Statistical Analysis Prof Dilip Asthagiri. Explicitly listing terms,o array 1 4 9 16,Colon notation. o array 1 5 creates an array with elements from 1 to 5. using intervals of 1, o array 1 2 10 creates an array with elements from 1 to.
20 using intervals of 2,Using the zeros and ones commands. o array zeros 1 3,o array ones 1 5, We can do addition subtraction multiplication and division of an array. and a value using the operators, Try this Create an array T for example T 1 4 9 16 And try out. the following functions in the command window, To operate on each element of the array individually you have to put a. period in front of the operator, To demonstrate this try the following example in the command window.
Now observe the results for the following, And now try to perform any one of those functions without the period It. doesn t work because without the period Matlab tries to perform a matrix. operation The dimensions of x and y are inappropriate for such a. A row vector can be turned into a column vector using the transpose. operator Using the definition of t above note what happens for the. command t and t,We can also extract and modify elements of a list. Modeling and Statistical Analysis Prof Dilip Asthagiri. Selecting an element of a list use x 3,We can set an element using x 3 100. To select a range of an array use x 3 5 or x 3 or x 5. See Exercise 5,8 for loops, for loops will repeat a series of statements a specific number of times. They help us by making Matlab do all the repetitive work for you Let s. solve a simple Matlab problem, Problem We want to find the sum of numbers 0 to 50 So.
we want to find 0 1 2 3 49 50 We can use,the loop construct here. Solution an example of coding in the command window. for i 1 50, Note that I have preset the variable a to be 0 before the loop begins. This means that on the first iteration4 a 0 5 So the new value of. variable a is given by a 0 i where i refers to the iteration. number so our first iteration number is 1 because we have assigned the. for loop to run such that i increases by one for each iteration starting. from 1 So Matlab will add 0 1 put it in the variable a and reach end. The loop will make Matlab go back to the start of the loop and once again. perform a a i just that this time a on the right 1 and i 2. for the second iteration This goes on 50 times and you get your answer. of 1275 Note that if you put the preset inside the for loop. for i 1 50, I will use the term iteration to refer to the loop number First iteration will refer to the first. repetition, Note carefully the position of the variable and the position of the value assigned to that variable. Modeling and Statistical Analysis Prof Dilip Asthagiri. Then we have for every iteration a i And we will end up with a 50. as the last iteration number, The easiest way to add these numbers would be of course.
9 if constructs, The if construct is a simple yet very useful function in coding Let s look. at another problem, Problem Identify if a number is below 25 or between 25 and 75 or. function exmp 7 inp,disp less than 25,elseif 25 inp inp 75. disp between 25 and 75,disp above 75, Although this could be an insultingly simple example there are a few. important points to note The elseif within the if construct will enable. you to add another condition in case the first one isn t s. Matlab for the Absolute Beginner By Arvind Ravichandran 7 12 11 Do not worry about your difficulties in Mathematics I can assure you mine are still greater Albert Einstein When first introduced to Matlab with no previous programming experience I myself was a bit nervy about using the software to solve and

Related Books