An Introduction to MATLAB Version 1.1 Department of Mathematical Sciences, Univ

An Introduction to MATLAB Version 1.1 Department of Mathematical Sciences, University of Bath 0 Introduction 0.1 What is MATLAB? MATLAB is a programming language, just like C, Python, and many others. MATLAB also refers to the editor you will be using to write scripts and functions in this language. If you’ve already learnt a programming language, you might find it easy to transition to MATLAB. However, if you have never tried programming before, some of the concepts may seem foreign and a little daunting. The purpose of this guide is to attempt to bridge the gap for those who have no prior knowledge of coding and allow them to take the first steps towards learning a programming language. 0.2 About this guide This guide has been written by student volunteers, and is constantly in the process of being edited and rewritten to improve it and make it more accessible to students. Because of this, there may be some mistakes and room for improvement. If you would like to help with the writing of this guide or offer suggestions and corrections, please email a.spence@bath.ac.uk. 1 Getting started with MATLAB 1.1 The Interface Once you open MATLAB, you will be presented with an interface like the one below. ① Command Window: This is the main workspace where you will type your commands. The prompt symbol (>>) shows that MATLAB is ready for you to type a command. ② Current Folder: Once you start writing and saving functions, you will need to be in the correct folder in order to use a function. Double-click on a folder to open it. If you are in the correct folder, the file names will be in black; if you are not in that folder, the file names will appear in grey with a faded icon beside them. ③ Workspace: Once you begin using variables, MATLAB will show you which variables are stored by saving their values in this panel. If you want more information about a variable that is stored, double-click on its name and a new panel will open, showing its details. ④ Command History: This is a list of the previous commands you have entered into the command window. If you want to use a command again without typing it all out, you can double-click on a command in this panel to run it. ⑤ Current Location: Here you can check which folder you are currently in. ⑥ Tools: These are your options, which you can explore later. ① ② ③ ④ ⑤ ⑥ 1.2 Basic computation in the command window Although MATLAB can be used for writing complex functions, it can also act as a simple calculator. In this section, you can familiarise yourself with the basic command window and its capabilities by typing simple commands. Arithmetic MATLAB can perform basic operations, just like a calculator. Try typing each of these into the command window, and hit enter after each calculation: 2+9 13-4 3*4 55/11 MATLAB will return the answer to the calculation, like so: ans = 11 MATLAB also has ‘built-in’ functions. Try some of these: exp(4) log(23) sqrt(64) abs(-3) There are many more built-in MATLAB functions you can use when writing your own functions, but you will learn these over time. 1.3 Variables A core concept in programming is the use of variables. A variable is a name (a string of characters, beginning with a letter) which is assigned a specific value using the = symbol. You can name your variable whatever you want – it won’t affect the way it functions, but it is a good idea to name important variables something that will help you remember what it is later on. Here is an example, where the variable named “a” is assigned the value “4”: a = 4 It’s as simple as that; just type the above code into the command window (it will return a = 4 again because we have not suppressed the output, but we will learn about that later). Now, the variable “a” has the number “4” associated with it. Any time you write the letter “a” in the command window, MATLAB will read it as “4” instead. For example, typing sqrt(a)will return ans = 2, because MATLAB reads the command as sqrt(4). You can change the number assigned to the variable “a” by typing something else. For example, try a = 1 Now, MATLAB has forgotten the value “4” and replaced it with “1” instead. You can check this by looking at the workspace. MATLAB has a few built-in variables such as pi. Try the command cos(2*pi). You can assign a variable name to almost anything in MATLAB, even complex functions and commands, for example myNum = sin(4*pi/3) + abs(2*a) Notice that the variable myNum refers to other variables, pi and a. If you change a, the current value of myNum will not change. You will have to re-assign the variable myNum in order to change its value. It is important to know that capitalisation of variables matters. mynum is not the same as myNum. Also note that if you make a typo when calling a variable, MATLAB won’t know! It will simply think you’re trying to access a variable that you have not yet defined, and give an error message. 1.4 Suppressing outputs Until now, every time you create a new variable, MATLAB will return the value of the variable in the command window right after you’ve defined it. This is known as “printing” the output. This doesn’t do any harm, but it is slightly unnecessary. When you write functions, you won’t want MATLAB to print hundreds of values every time it assigns a new value to a variable! In order to suppress an output, simply type a semicolon at the end of the line. For example, try this: x = 23; It still stores the variable, but it doesn’t parrot back its value. Remember your semicolons, and your command window will be a much more peaceful place, with no unnecessary outputs! 2 Matrices MATLAB, as I’m sure you’ll hear again, is the “matrix laboratory”, so the majority of techniques you’ve learnt so far for matrices can also be used in MATLAB. 2.1 Creating a matrix To be able to start using those techniques, you need to tell MATLAB exactly what the matrix looks like. Here’s how to do it. Suppose you wanted to create the matrix: [ ] Then you should type the following into the workspace in MATLAB: The “A =” is simply assigning the matrix to the variable name “A”, as we learnt earlier. The “*“ tells MATLAB that what you’re typing is going to be a matrix. If you use a different kind of bracket, you won’t get a matrix out when you press enter. The commas separating the numbers tell the computer that the two numbers either side of the comma are different elements of the matrix. If you omitted the comma between the 4 and the 3, you’d get an error telling you that you didn’t put the right number of elements in the matrix. The semi colon after the 2 means that you are starting a new row of the matrix, you should do this every time you want to start a new row, so if you want n rows in your matrix, you should have n-1 semi colons between the square brackets. Finally, closing the square bracket means you’ve finished adding all of the elements to your matrix. Now just press enter to see it displayed on the screen. 2.2 Common errors 1) Using the wrong type of bracket 2) Trying to enter objects other than numbers as elements 3) Unbalanced numbers of elements on different rows 2.3 Operations with matrices Suppose you have two matrices, A and B, and you want to add them together. Simply type A+B and press enter, and MATLAB will display their element-wise sum. Note that instead of creating A and B separately, you could just create them in the same line, using the notation as above. For example if you wanted to do the sum: [ ] [ ] You could either type Or In exactly the same way, you can multiply two matrices together by changing the “+” symbol for the “*” symbol. (Since MATLAB knows that the objects are matrices, it will perform matrix multiplication in this case rather than element-wise multiplication. If you want to perform element-wise multiplication on matrices, you must use the operation “.*”) If you want to perform a certain operation to every element of a matrix A, for example add 3 to each element, just type A+3. Or to multiply every element by 2, type A*2. To access a certain element of a matrix, all you need to do is type the name of the matrix, and then in round brackets immediately next to it type the row the element is on, then the column it’s in. The two numbers should be separated by a comma. So if you had the matrix uploads/Finance/ matlab-guide-complete.pdf

  • 34
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager
  • Détails
  • Publié le Jul 03, 2022
  • Catégorie Business / Finance
  • Langue French
  • Taille du fichier 0.7169MB