1 / 101

MATLAB introduction

MATLAB introduction. Motivation. A fundamental activity of engineering is to describe the world around us using mathematics. We use mathematical models to describe physical systems (modeling). Examples : Flow of water through orifice – 1 st order differential equation

emory
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

  2. Motivation • A fundamental activity of engineering is to describe the world around us using mathematics. We use mathematical models to describe physical systems (modeling). • Examples: • Flow of water through orifice – 1st order differential equation • Free oscillation of a mass on a spring – 2nd order differential equation • Current in electrical circuits (RLC) – 2nd order differential equation • Vibration of circular membrane – Bessel functions • One-dimensional heat flow – partial differential equation (temperature depends on both position and time) • MATLAB provides the tools to solve mathematical models in a programming environment that includes graphing capabilities.

  3. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  4. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  5. Command prompt Start MATLAB by double-clicking on icon or selecting application from the Start menu. The MATLAB desktop will be launched. Command prompt “>>”. Can type commands here (or) write and save your own programs (using m-files). Let’s begin with command prompt. Type the following and hit Enter.

  6. Command prompt The variable “a” is assigned (“=”) the value of the square root (“sqrt”) of 243 and the result is echoed to the screen. To clear the variable, use the “clear a” command. Note that it disappears from the Workspace area. “clear all” removes all variables from the Workspace area.

  7. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  8. Command prompt To clear the text from the Command Window, use “clc”. Next, define an array (or matrix; MATLAB = matrix laboratory). http://en.wikipedia.org/wiki/Matrix_(mathematics) The same 1x5 (row x column) array could be defined as shown. Default step size is 1.

  9. Command prompt Individual elements of arrays are identified by their index (or indices). For a 1xn array, it is only necessary to use the column index since there is just one row. Array size is 1x6 (2 rows, 6 columns). 3rd column in “a” is “7”. Can also access using both indices: (row, column) = (1, 3).

  10. Command prompt For mxn arrays (two-dimensional), we must use both the row and column indices to access individual elements. Array size is 2x6 (2 rows, 6 columns). (row, column) = (1, 3) entry is “7” (row, column) = (2, 5) entry is “15” To define a 2-row array, we used a semicolon to mark the end of first row. Both rows must have same number of columns.

  11. Command prompt An error is generated if the two rows do not have the same number of columns. “CAT” refers to concatenation (or joining arrays). Error because there are 6 columns in the 1st row and 7 columns in the 2nd row.

  12. Command prompt Define array with steps of 0.5 (1x9 array). Previously defined a row array. Can also define a column array (5x1). Semicolon marks the end of each row.

  13. Command prompt Let’s define and add two row arrays. Square each element of the “a” array. The “.” tells MATLAB to perform the squaring operation “^2” term-by-term. Add the corresponding elements. The first element of “a” is added to first element of “b”.

  14. Command prompt Let’s define and add two other arrays. Dimensions are: (rows, columns) = (1, 5) Use the transpose operator (single quote) to convert row array into column array. Dimensions are now: (5, 1) Oops! Arrays must have the same dimensions to be added. You can learn more in a Linear Algebra course.

  15. Command prompt MATLAB has many functions that complete specific tasks. My favorite is “why”. Not very useful, but fun!

  16. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  17. Command prompt Let’s define an equation. As an example, consider the volume, V, of a circular cylinder. h Use a radius, r, of 8 and a height, h, of 15. r • is defined in MATLAB using “pi”. Use “*” for multiplication and “^” for power. V = 3015.9 (MATLAB uses scientific notation; value following “e” gives the power of 10).

  18. Command prompt Given the volume, V, calculate the radius. Used the “sqrt” function. Original “r” value is overwritten (same value in this case). Note that when using a semi-colon to end a statement (“r = 8;”), the assignment is not echoed to the Command Window.

  19. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  20. Command prompt Can describe polynomials (often used to fit experimental data) by defining an array of the polynomial coefficients (highest power to lowest). For a second-order polynomial, we can use the quadratic equation to determine the two roots (x values where y(x) = 0). MATLAB has a function “roots” that can be used to find the roots of nth-order polynomials. Consider the example: Let’s complete the same example using MATLAB.

  21. Command prompt Use indices of array to call specific value; a = y(1), b = y(2), c = y(3). Used the “roots” command to find two values of x where y(x) = 0. Same result as quadratic equation.

  22. Command prompt Can also separate elements of row array using commas rather than spaces. The roots are: ( )

  23. Command prompt The roots are: Can factor this polynomial to check the result:

  24. Command prompt Let’s look more closely at accessing individual elements of arrays using indices. Step size is -2. The “.^3” gives the term-by-term cube. The first element of “a” is 10. The first element of “b” is:

  25. Command prompt If the “.^” operator is not used, we get an error. This is because the “^3” is attempting to perform: “a*a*a”. The “^” operator requires square arrays; this means that the number of rows and columns must be equal (mxm arrays).

  26. Command prompt We can also access ranges of elements. This lists the 5th to 8th elements of “a” and “b”.

  27. Command prompt The “find” function is used to identify the indices (not values) of particular elements. It can be used with the relational operators: >, >=, <, <=, ==, ~=. The “;” suppresses output to Command Window for “a”. Find indices of all “b” values > 0. This is the first 5 elements of “b”. Show “b” values with indices of 1 through 5.

  28. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  29. Command prompt Consider the cosine function: First, define x and then calculate y. Range is -2 to 2. Step size in x is: Trigonometric functions require inputs in radians ( rad = 180 deg), not degrees, for MATLAB. In this case, it would be better to graph the data to see the result. Use the “plot” function to graph (x, y) data.

  30. Command prompt Figure window Individual points are connected by line segments. The x axis limits are set between -2 and 2 using “xlim”. Let’s plot only the points next; no line segments. Set using “xlabel”.

  31. Command prompt Many figure windows can be opened at the same time (1, 2, 3, …).

  32. Command prompt Now only the (x, y) points are shown – red circles are specified by “ro”. We might like to have higher resolution in x values so the points are closer together and the cosine function is “smoother” when graphed. x step size

  33. Command prompt Step size in x is now: Graph looks more continuous.

  34. Command prompt Can plot multiple curves in the same graph. Used blue squares for y1 (larger step size in x1) and red line for y2. Added “legend” to identify two different curves on single plot.

  35. Command prompt Type “help plot” to learn more. ‘bs’ = blue square ‘r^’ = red triangle (pointing up) ‘k:’ = black dotted line

  36. Command prompt Plot “a + b”. Let’s use the plotting function to verify identities. x step size

  37. Command prompt Use “exp” function for exponential. Let’s again use the plotting function to verify identities. (a + b)/2 Plot (a + b)/2 and compare to cosh.

  38. Command prompt (x, y) = (0, 3) 51st y element Find the maximum value of y using “max”.

  39. Command prompt Roots are x = 0.866, -0.866.

  40. Command prompt (x, y) = (0, 3) Find minimum using “min”.

  41. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  42. Command prompt Can use MATLAB to solve systems of linear equations. Write in vector-matrix form. Vector-matrix form can be represented compactly as: Ax = b Can determine x using the inverse of the A matrix: x = A-1b Perform this operation in MATLAB.

  43. Command prompt Define the “A” matrix. Note the use of the semicolon to give the 2nd row. The “inv” function calculates the inverse of a square matrix (the number of rows is equal to the number of columns). This is more Linear Algebra. Check this result.

  44. Outline • Examine basic commands typed at the command prompt • Arrays • Equations • Polynomials • Plotting • Systems of equations • M-files • Decision-making • Loops • Polynomial fitting

  45. M-file Rather than typing at the command prompt (>>), can write a program to execute series of commands. This is an m-file in MATLAB. Click to begin new m-file. The Editor screen is launched. We can now type commands in a single program (saved as _____.m) and execute this new program when we are ready.

  46. M-file Let’s write a program (tank.m) to solve the following problem. A water tank consists of a cylindrical base of radius r and height h and has a hemispherical top (also radius r). The tank is to be constructed to hold V = 500 m3 of fluid when filled. The surface area of the cylindrical part is 2rh and its volume is r2h. The surface area of the hemispherical top is 2r2 and its volume is 2r3/3. The cost to construct the cylindrical part of the tank is $300/m2 of surface area; the hemispherical part costs $400/m2. Plot the cost versus r for 2  r  10 m and determine the radius that results in the minimum cost. Compute the corresponding height h. r h

  47. M-file Click to execute the m-file. This also saves the program. “h” is an array. Must use “.” for term-by-term power and division. Same for “C”. Find the index of “C” where it is equal to its minimum value. Determine “r”, “h”, and “C” at this index. Plot “C” as function of “r”. Define step/range for r. “%” for comments. Note that you need to have your m-file in the current directory to execute it.

  48. M-file (4.92, 91394) The minimum cost is $91394 for a radius of 4.92 m and height of 3.2949 m.

  49. M-file The aorta is the largest artery in the body, originating from the left ventricle of the heart and bringing oxygenated blood to all parts of the body in the systemic circulation. The aorta extends down to the abdomen, where it branches off into two smaller arteries. The blood pressure in the aorta during systole (the period following the closure of the heart’s aortic valve) can be described using: where t is time in seconds and y(t) is the pressure difference across the aortic valve, normalized by a constant reference pressure (y is unitless). http://en.wikipedia.org/wiki/Aorta

  50. M-file 100 steps per cycle Use “.*” for element-by-element multiplication because t is an array. This is an oscillating function (sine wave) that decays exponentially. We must decide on step size to plot the function. Oscillating frequency is 9.7 rad/s. This is: cycles/s (Hz). The time for one full cycle (or period) is: s.

More Related