1 / 19

MATLAB Introduction

MATLAB Introduction. MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! It is also a programming language, commands executed line by line

spruill
Download Presentation

MATLAB Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB Introduction • MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! • It is also a programming language, commands executed line by line • MATLAB (matrix laboratory) is designed to work with matrices and vector data- your brain has to think in this way to code MATLAB • This workshop is a blending of my old course for engineering students and a book called MATLAB for beginners: A gentle approach. A complete pdf version of an older edition is available online. http://ivut.iut.ac.ir/content/1059/MATLAB_for_Beginners.pdf

  2. Your MATLAB window gives you clickable access to most of what you will need when using the software. >The command window is where you can type and execute lines of code >Current folder will show you all the files in the current folder (which is displayed above) >Workspace displays all the saved variables, their value and description >along the top, the “APPS” are also referred to in older text as toolboxes >PLOTS allows you to easily navigate the plot functionality, but you can also code this

  3. You can customize this space: >You can add/delete windows on display. >If using the command window to execute code, it may be useful to add the command history “docked” to the display >you can explore preferences (the gear icon) to changes colour and appearance

  4. You can customize how the windows are displayed as well by “grabbing them” with your mouse and moving them to shaded areas that appear

  5. To adjust the current folder (normally defaults to MATLAB program folder, or the last accessed folder), you can click on the browse icon and select the folder of your choice. For today we will use your home directory and create a new folder (right click-new folder) called RDP_MATLAB*** **** Matlab has rules for file and variable naming- Must start with a letter, followed by letters or numbers or underscore, maximum 64 characters (excluding the .m extension), and must not be the same as any MATLAB reserved word. NO spaces, or other characters (ie. -,& etc) >>cd(‘filepath goes here’)

  6. >>help and >>doc • The most important command when learning MATLAB on your own is help • Try typing in your command window “help sin” as shown below and press enter: • Now try “help plot” • This won’t work with any search term, but for built in functions of MATLAB • You can also go straight to Mathworks tutorials by typing in “doc sin”

  7. Start getting to know the command window • Entering scalars and simple operations, try1: >> 2*3+5 What does the command window say? Look at your workspace, what do you see? • Next try something a little more complex2: >> cos(60*pi/180)3 What does the command window say? Look at your workspace, what do you see? • Now look at creating a variable: >> x=4 What does the command window say now, how is it different? Look at your workspace, what do you see? • Now let’s do something with that variable: >> 3/sqrt(2+x) 1 MATLAB order of command is important 2 Spend some time looking at built-in functions like cos, pi, sqrt 3 MATLAB defaults to radians with angles

  8. Getting to know the command window • Try some more operations by copy/paste: 1.) Perform the addition 7+9 2.) Perform subtraction of 16-10 3.) Perform multiplication 2*9 4.) Perform division 12/3 5.) Perform the exponential 3^5 6.) Perform the multiplication of 3*-5 7.) Perform the exponential of (-3)^5 8.) Perform the exponential of -3^5 9.) Perform multiple operations (4.5+1.5)/2 10.) Perform multiple operations (4.5+1.5)/2 –2*(7^4)/100

  9. Getting to know the command window • The Semicolon: In your command window, type: >> y=30; >> z=8; What is different about this display? Look at your workspace again. • Semicolon ends a line of execution. Try typing: >> x=2; y-z; Check out your workspace: • More complex variable manipulation with mutliplication: >> w=4*y+3*z • MATLAB is case sensitive! Try typing the below, (you should get an error!) >> w=4*Y+3*z • To find all the variables used (defined) in a session, type in >>who • Now try giving the variable Y a value: >> Y=20; check your workspace, and try the command again: >> w=4*Y+3*z

  10. Save, clear and clc • Now that you can add variables, how about deleting them? Try: >>clear x , what happened to workspace? • To clean upi your command window, without deleting variables, type: >>clc • What about clearing all variables? Try >>clear all • What about saving variables? Define two variables a=3, b=5 in your command window. Then try typing the following code, and check your current folder. >>save myFile a b • Now, let’s start from scratch, >> clear a b • Now try: >>load myFile

  11. More Arithmetic operations • π – type pi • ε – type eps • Absolute value- abs() • Square root- sqrt() You can find loads more coded versions of operations by looking up Arithmetic operators in the Mathworks help

  12. Variables’ Rules Summary • The following are the MATLAB rules for naming variables: >MATLAB is case sensitive with respect to variable names >Letters and digits allowed >Variable names must start with a letter (not numbers) >You may have up to 31 characters to name a variable >Punctuation signs are not allowed, including spaces >The underscore “_” is allowed but can’t be first >Don’t use built-in variable names such as “ans”

  13. Using built-in functions- • Be aware that the built-in constants (and even functions) can be overwritten by the user. Try the following and make sense of the outcomes: • >> j = 100; • >> j^2 • >> clear j • >> j^2

  14. Types of variables • Scalars, defined as integers, doubles • Characters or Strings • Arrays or Matrices Besides integer values and reals, MATLAB can also hold complex numbers and text, and, all of these different value types can be stored in matrices and vectors. Matrices will be introduced in later sections.

  15. Trying out variables • Remember when we gave variable Y a value: >> Y=20 for w=4*Y+3*z; check your workspace, and try the command again, but this time instead of w=, use: >> the_answer_is=4*Y+3*z • Try defining the following variables: >>cost=100; >>sale_price=120; >>profit=sale_price – cost • Numbers are displayed with four decimals. Try: >> fraction=7/3 • However, MATLAB stores numbers internally with about 16 digits. You can display these by formatting your output. Type: >>format long then try typing >>fraction=7/3 • You can reformat back to four decimal places by >>format short • To look at all the options you can select, try >>help format

  16. Commenting/Annotating Code • MATLAB code should not just be readable by MATLAB itself, but also by human readers. • Useful to annotate your code with comments whilst you are writing. • Express intention of code section, meaning of variables, underlying asusmptions. • The symbol % is used to indicate text which is not intended for MATLAB but for the reader (comments) • Try typing >>% This is a comment to explain to my code

  17. Symbolic Math • You may also use symbolic variables • For examples, defined the variables x and y as follows: >>x=1/2 >> y=2/3 • Then type: >>sym(x) >>sym(y) • Now arithmetic operations can be performed on these symbolic variables. Consider the following: >>z=sym(x-y) • In the above, z is automatically given symbolic variable type (check workspace) • The important thing is to start each operation or calculation with the sym command. • If you want to convert back to a number value you can use the double command. Tyr: >>double(z)

  18. Variables exercises 1. Perform the operation 2*3+7 and store the result in the variable w. 2. Let x = 5.5 and y = -2.6. Calculate the value of the variable z = 2x-3y. 3. In Exercise 2 above, calculate the value of the variable w = 3y – z + x/y. 4. Let r = 6.3 and s = 5.8. Calculate the value of the variable final defined by final = r + s - r*s. 5. In Exercise 4 above, calculate the value of the variable this_is_the_result defined by this_is_the_result = r^2 – s^2. 6. Define the three variable width, Width, and WIDTH equal to 1.5, 2.0, and 4.5, respectively. Are these three variables identical? 7. Write the following comment in MATLAB: This line will not be executed.

  19. Exercises continued 8. Define the values of the variables y1 and y2 equal to 7 and 9 then perform the calculation y3 = y1 – y2/3. (Note: 2 in the formula is a subscript and should not be divided by 3). 9. Perform the operation 2*m – 5. Do you get an error? Why? 10. Define the variable centigrade equal to 28 then calculate the variable fahrenheit defined by fahrenheit= (centigrade*9/5) + 32. 11. Use the format short and format long commands to write the values of 14/9 to four decimals and sixteen digits, respectively. 12. Perform the who command to get a list of the variables stored in this session. 13. Perform the whos command to get a list of the variables stored in this session along with their details. 14. Calculate symbolically the area of a circle of radius 2/3 without obtaining a numerical value. No Units are used in this exercise. 15. In Exercise 14 above, use the double command to obtain the numerical value of the answer.

More Related