Getting Started Current folder window – Atop the current folder window, you can see the address field which tells you where you are currently located. In programming, think of it as your ‘current directory,’ you can move up and down levels or browse for a new location within your operating hard drive. You can only access a script or Matlab-related file of the current folder in which you are in. Command window – Where you can execute commands and create variables for your workspace. You can perform calculations in the command window in a manner similar to the way you perform calculations on a scientific calculator. Workspace – Where you have stored your variables. Also command history, self-explanatory. To start using Matlab, you only need be concerned with the command window for now. Sample. Do a simple arithmetic calculation as an example. Matlab is space-insensitive. Note how if you don’t define a variable for a calculation, the default goes to ans. Note a few arithmetic operators like: + * / ^ ' addition subtraction multiplication division power operator transpose Variables. Start defining and creating variables with numbers or strings (which must be encased in apostrophes) stored in them. Defining variables is case-sensitive and limited to certain strings – for example, you cannot use @#$%^*(). You can suppress the output of a command with the semicolon (;). You can also manually edit your variables by clicking on them in workspace. We are only dealing with single values, i.e. one-array matrices. Demonstrate commands clear and clc. Up arrow allows you to go through your previous commands in case you ever execute a command incorrectly or want to repeat it without the hassle of typing it all over again. Saving. You can save your workspace into a MAT-file. Simply do a CTRL+S to initiate it, and you will be prompted to save it in a location with your input name for it. -- Using Matlab as a calculator – do a few simple arithmetic operations. Mathematical functions Built-in functions. Some are listed: Absolute value: abs(x) Trigonometric functions: sin(x), cos(x), tan(x), hyperbolic Exponential: exp(x) Logarithm: log(x), log10(x) Square root: sqrt(x) where x is the argument that has been previously defined numerically. There are more built-in functions, some which may seem simple like min(x) sign(x), some sophisticated, but Matlab is high-level so it gives you these elementary functions so that you can focus on writing more important codes. Laplace/Inverse Laplace Transforms. Demonstrate Laplace/Inverse Laplace transforms at the end if there is enough time! First we can define the following variables to be symbols: sym x y z Define a function: f = sqrt(x) Laplace transform: F = laplace(f,x) Make the solution look more aesthetically pleasing: pretty(F) Inverse Laplace transform: g = ilaplace(F,x) Numerical representation. Default number representation in Matlab is double precision, meaning it is stored in 64 bits. In Matlab, numbers can be stored in both single- and double-precision. Plotting. Try plotting a few of the functions using plot(x,y). Use x=[0:0.1:10] as the argument (creates a row vector from 0 to 10 with equal spacing of 0.1), or create an argument with n points using the command linspace(x1,x2,n). This will serve as their introduction to creating and using matrices. Complex number representation. We can also create and store complex numbers using the following syntax: z = 1+2i which returns a complex array z with real part 1 and imaginary part 2. You can also solely create imaginary numbers, just add i at the end of the number. Like z=2i And in fact, i (or j) is a predefined variable i=sqrt(-1), the imaginary number, much like a constant pi=3.1415… is. You can always use the help command for more info on what a certain built-in function does. Use the lookfor command with a keyword if you ever need to find another function that you think Matlab should have but you don’t know the command to call it. -- Matrices and Matrix Algebra Matrix representation. Arrays and matrices are used interchangeably; a matrix is just a multidimensional array used to do matrix algebra. Matlab is based on matrix and vector algebra, and even scalars are treated as 1x1 matrices if you’ve noticed. Vectors are classified in two fundamental ways: columns and rows. Row vector: R = [1 2 3 4] which generates a 1 × 4 matrix. We enclose a matrix in brackets, and each element is created by separating them with a space; a comma in place of the space would’ve also done the same thing. A single space is enough, after that Matlab just recognizes all wider spaces as a single space. r = [1,2,3,4] Column vector: c = [1; 2; 3; 4] which generates a 4 × 1 matrix. The semicolons indicate to skip to the next row. Previously defined vectors can be used to define a new vector. For example: A = 0 B = [0 r 5 6 7 8] You can also call upon the element of a vector by specifying its location within the array. b = B(1, 2) Try putting a 3 × 4 matrix together labeled M. Alternatives. There are many other ways to generate matrices without the hassle of inputting all of the desired elements in. You can create a vector with equally spaced elements: v = [1:1:10] which will create a row vector starting from 1 and ending to 10, and the middle number is the increment. If only two numbers are given, the default increment is 1. You can generate a random matrix by using the function rand(m,n) which generates an 𝑚 × 𝑛 matrix or rand(n) which generates a square matrix of size n. R = rand(3,4) generates a 3 × 4 matrix where 3 is the number of rows and 4 is the number of columns. Other alternatives include but are not limited to the following functions: ones(m,n), magic(n), zeros(m,n), eye(m,n). These are a few of the special matrices that can be defined. Some other representations include (:,1), where the colon indicates “all the rows in column 1.” Similarly, (1,:) indicates “all the columns in row 1.” This is helpful if you want to plot using a particular set of data: plot(v(:,1), v(:,2)) Matrix algebra. The algebra works as you learned in elementary math classes. Adding a single value to a matrix yields: B + 10 will add 10 to all the elements in the B vector. Similarly: B – 10 will subtract 10 to all the elements. The following also holds for multiplication (*) and division (/). Now what about arithmetically operating on two or more different matrices? Remember from your crash course in matrix algebra that for adding/subtracting, matrix dimensions must agree! Simply put, A which is an (𝑛 × 𝑛) dimension and B which is an (𝑚 × 𝑚) dimension where n = m. Try it: M + N M – N which should both work. Now try: r + c r – c which shouldn’t work because their dimensions don’t agree. You can extend this criteria to adding/subtracting an infinite number of matrices. Now remember multiplying two different matrices? The criteria for being able to multiply two matrices? (𝑚 × 𝑛) × (𝑛 × 𝑝) where (𝑚 × 𝑛) represents the first matrix and (𝑛 × 𝑝) represents the second matrix into a third matrix (𝑚 × 𝑝). Now try: t = r*c and t should now be a single value array because m = p = 1. Matlab actually has a function called concantenation in which you add two separate matrices and combine them into a larger one. Horizontal concantenation: g = [1 2 3; 4 5 6; 7 8 9; 10 11 12] H = [a, a] Vertical concantenation: V = [a;a] where the pair of square brackets is the concantenation operator. You can also transpose matrices by adding an apostrophe (‘) or single quote to the end of the variable: g’ Built-in functions. We can act upon the matrices we’ve built with the mathematical functions we defined before. The function will act upon all the elements of the matrix. sin(r) cos(c) exp(B) exp(B(1,3)) Now for some examples to try out: One-dimensional variable problem + two-dimensional variable problem (http://www.mathworks.com/company/newsletters/articles/using-matlabs-meshgrid-command-and-array-operators-to-implementone-and-two-variable-functions.html) Problem 4.8 The ideal gas law, 𝑃𝑣 = 𝑅𝑇, describes the behavior of many gases. When solved for v (the specific volume, m^3/kg), the equation can be written: 𝑣= 𝑅𝑇 𝑃 Find the specific volume for air, for temperatures from 100 to 1000 K and for pressures from 100 kPa to 1000 kPa. The value of R for air is 0.2870 kJ/(kg K). In this formulation of the ideal gas law, R is different for every gas. There are other formulations in which R is a constant, and the molecular weight of the gas must be included in the calculation. You’ll learn more about this equation in chemistry classes and thermodynamics classes. Your answer should be a two-dimensional matrix. Solution: 1. 2. 3. 4. Create a mesh grid [P_g,T_g] = meshgrid(P,T) Define a variable P_inv = P_g.^(-1) Solve for v = R*T.*P_inv Plot using surf(P,T,v)
© Copyright 2024