AND MAPLE MS-ESPM 2014 MODULE 1 2 MATLAB Engineering Problem Solving with MATLAB and MAPLE Module Content Details 1 Starting with MATLAB MATLAB Windows, Working in the Command Window Using MATLAB as an Engineering Calculator Display Format 2 Basic Mathematics Arithmetic, Algebra, Solving Equations, User Defined Functions 3 Vector and Array Creating Vector and Matrix, Matrix Operations Addressing Elements 4 Script Files and Programming Creating, Saving and Running a Script File Input and Output, Functions 5 2 D Graphics Plot of Data and Function, Multiple Plots, Formatting Plots Pie, Histogram and Polar Plots 6 Curve Fitting and Inetrpolation Polynomials, Roots, Addition, Multiplication of Polynomials Division and Derivatives of Polynomials Curve Fitting and Interpolation 7 3 D Plots Line, Mesh and Surface Plots, View Command 8 Applications Solving Equation, Finding a Minimum or Maximum of a Function Differentiation and Integration Ordinary Differential Equation, Partial Differential Equations 9 Introduction to MAPLE Basic Mathematics with MAPLE (Algebra, Trigonometry, Calculus) Simple Plots 10 Application Example for MAPLE Ordinary Differential Equation and Partial Differential Equations 11 Introduction to SIMULINK Basic Blocks of Simulink Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics. 3 MATLAB 1. Introduction As the name suggests, Matlab is especially designed for matrix computations: solving systems of linear equations, computing eigenvalues and eigenvectors, factoring matrices, and so forth. In addition, it has a variety of graphical capabilities, and can be extended through programs written in its own programming language. Many such programs come with the system; a number of these extend Matlab's capabilities to nonlinear problems, such as the solution of initial value problems for ordinary differential equations. 4 MATLAB Matlab is designed to solve problems numerically, that is, in finite- precision arithmetic. Therefore it produces approximate rather than exact solutions, and should not be confused with a symbolic computation system (SCS) such as Mathematica or Maple. It should be understood that this does not make Matlab better or worse than an SCS; it is a tool designed for different tasks and is therefore not directly comparable. 2. MATLAB Windows Default Windows Other Windows Command Window Help Window Current Directory Window Editor Window Workspace Window Figure Window Command History Window 5 MATLAB 3. Working in the Command Window Click in the Command Window to make it active. When a window becomes active, its title bar darkens. It is also likely that your cursor will change from outline form to solid or from light to dark, or it may simply appear. Now you can begin entering commands. Several Commands can be types in the same line. This is done by typing a comma between the commands. When the Enter key is pressed the commands are executed in order from left to right. It is not possible to go back to a previous line in the command window, make a correction and re-execute the command. A previously typed command can be recalled to the command prompt with the up-arrow key (). When the command is displayed at the command prompt, it can be modified if needed and executed. The down-arrow key () can be used to move down the previously typed commands. 6 MATLAB To enter a statement that is too long to be typed in one line, use three periods, , followed by Enter. You can suppress output to the screen by adding a semicolon (;) after the statement. When the symbol % is typed in the beginning of a line, the line is designated as a comment. clc Command The clc command clears the Command Window. You can also place cursor on the Command window and press the right button of the mouse. Aborting To interrupt a running program press simultaneously the Ctrl-c keys. 7 MATLAB 4. Saving and Reloading Work To save your current workspace select Save Workspace asfrom the File menu. Chose a name for your file, e.g. filename.mat and next click on Save. Remember that the file you just created must be located in MATLAB's search path. To load contents of the file named filename into MATLAB's workspace type load filename in the Command Window. You can also select open from the File menu. 5. Arithmetic Operations of Scalars You can use MATLAB to do arithmetic as you would do with a calculator. You can use + to add, - to subtract, * to multiply, / or \ to divide, and ^ to exponentiation. 8 MATLAB Order of Precedence First Parentheses. For nested parentheses, inner most ate executed first. Second Exponentiation Third Multiplication, Division (equal precedence) Fourth Addition and Subtraction Using MATLAB as a Calculator This can be done by typing a mathematical expression in the Command Window and pressing Enter. 9 MATLAB 6. Display Formats FORMAT Set output format. FORMAT with no inputs sets the output format to the default appropriate for the class of the variable. For float variables, the default is FORMAT SHORT. FORMAT SHORT Scaled fixed point format with 5 digits. FORMAT LONG Scaled fixed point format with 15 digits for double and 7 digits for single. FORMAT SHORT E Floating point format with 5 digits. FORMAT LONG E Floating point format with 15 digits for double and 7 digits for single. FORMAT SHORT G Best of fixed or floating point format with 5 digits. 10 MATLAB FORMAT LONG G Best of fixed or floating point format with 15 digits for double and 7 digits for single. FORMAT SHORT ENG Engineering format that has at least 5 digits and a power that is a multiple of three FORMAT LONG ENG Engineering format that has exactly 16 significant digits and a power that is a multiple of three. FORMAT HEX Hexadecimal format. FORMAT + The symbols +, - and blank are printed for positive, negative and zero elements. Imaginary parts are ignored. FORMAT BANK Fixed format for dollars and cents. 11 MATLAB FORMAT RAT Approximation by ratio of small integers. Numbers with a large numerator or large denominator are replaced by *. FORMAT COMPACT Suppresses extra line-feeds. FORMAT LOOSE Puts the extra line-feeds back in. 7. Elementary Mathematical Functions sqrt: square root function sin, cos, tan: trigonometric functions sind, cosd, tand: trigonometric functions when angle is in degree asin, acos, atan: inverse trigonometric functions sinh, cosh, tanh: hyperbolic functions asinh, acosh, atanh: inverse hyperbolic functions 12 MATLAB exp, log, log10: exponential functions real, imag, conj: real, imaginary parts of a complex number, and the complex conjugate number abs, angle: absolute value of a complex number, and the phase angle of a complex number round: rounding to the nearest integer floor: rounding to the smallest integer ceil: rounding to the largest integer fix: rounding to the smallest integer if positive and to the largest integer if negative sign: 1 if positive, 0 if zero, and 1, if negative mod, rem: remainders upon division factorial: factorial of an integer number factor: factorize an integer number into prime numbers 13 MATLAB 8. Variables and Assignments A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical value. Once a variable is assigned a numerical value, it an be used in mathematical expressions, in functions, and in any MATLAB statements and commands. Variable names can contain letters, digits and the underscore character. Variable names must begin with a letter. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For example AA, Aa, aA and aa are four different variables. Avoid using the names of a built-in-function for a variable. 14 MATLAB 9. Predefined Variables A number of frequently used variables are already defined when MATLAB is started. ans A variable that has the value of the last expression that was not assigned to a specific variables. pi The number . eps The smallest difference between two numbers (2 -52 ). inf Used for infinity. i, j Defined as (-1). NaN Not a number 15 MATLAB 10. Managing Variables clear Removes all variables from the memory. Clear x, y, z Removes only variables x, y, and z from the memory. who Displays a list of the variables currently in the memory. whos Displays a list of the variables currently in the memory and their size together with information about their bytes and class. 11. Errors in Input If you make an error in an input line, MATLAB will beep and print an error message. Note that MATLAB places a marker (a vertical line segment) at the place where it thinks the error might be; however, the actual error may have occurred earlier or later in the expression.