Introduction to MATLAB PDF

Document Details

NeatTulip7918

Uploaded by NeatTulip7918

FEU Alabang, FEU Diliman, FEU Tech

Tags

matlab programming mathematical computation

Summary

This document introduces MATLAB, a software package for high-performance mathematical computation, visualization, and programming, providing an interactive environment and built-in functions for technical computing, graphics, and animations. It's designed for engineers and scientists who want to analyze and design systems and products.

Full Transcript

Modeling and Simulation Modeling and Simulation Know what is Modeling and Simulation Know what is Matlab and it’s function Demonstrate understanding of the basics of MATLAB. Identify built-in functions that can be used in interactive expressions. Demonstrate understanding of co...

Modeling and Simulation Modeling and Simulation Know what is Modeling and Simulation Know what is Matlab and it’s function Demonstrate understanding of the basics of MATLAB. Identify built-in functions that can be used in interactive expressions. Demonstrate understanding of concepts on vectors and matrices. Identify operations on vectors and matrices. Use build-in functions to simplify codes involving vectors and matrices. What is Modeling Modeling the act or an instance of making a model the practice or occupation of a person who model objects What is Simulation A simulation model is a mathematical model that calculates the impact of uncertain inputs and decisions we make on outcome that we care about, such as profit and loss, investment returns, etc. A simulation model will include: Model input that are uncertain numbers. uncertain variables Intermediate calculations are required Model outputs that depend on the inputs -- These are uncertain functions. What is Simulation Simulation is imitation of some real thing, or a process. The act of simulating something generally involved representation of certain key characteristics or behaviors of a selected physical or abstract system Simulation involves the use of models to represent real life situation. Simulation is the process of designing a model of a real system and conducting experiments with this model for the purpose of understand the behavior for the operation of the system.. Simulation techniques Simulation techniques can be used to assist management decision-making, where analytical methods are either not available or inappropriate. Typical business problems where simulation could be used to aid management decision making are: inventory control queuing problems production planning Simulation Application Sample AnyLogic simulation software provides the unique ability to use GIS maps within your simulation models. Maps can be applied to model systems like supply chains, logistics networks, and other cases when you need to take into account locations, roads, routes, or regions. Simulation Application Sample GeoFS is a flight simulator with a global environment generated from satellite images and digital geographic data. You can run it for free in your web browser without anything to install, subscribe to worldwide HD aerial images (suitable for VFR flight) or install one of the two available Apps on your mobile device. Leading supplier of VR training solutions to the maritime sector. https://www.morildinteraktiv.no/custom Simulation Application Sample List of 10+ Network Simulation Tools & Softwares: GNS3 (Graphical Network System3) Cisco Packet Tracer Putty Secure CRT Microsoft Visio PRTG Network Monitor WIRESHARK EVE-NG Network Simulator -NS3 SNMP Agent Simulator Introduction to MATLAB What is MATLAB MATLAB is a software package for high-performance mathematical computation, visualization, and programming environment. It provides an interactive environment with hundreds of built-in functions for technical computing, graphics, and animations. MATLAB stands for Matrix Laboratory. MATLAB was written initially to implement a simple approach to matrix software developed by the LINPACK (Linear system package) and EISPACK (Eigen system package) projects. MATLAB is a modern programming language environment, and it has refined data structures, includes built-in editing and debugging tools, and supports object-oriented programming. MATLAB® is a programming platform designed specifically for engineers and scientists to analyze and design systems and products that transform our world. The heart of MATLAB is the MATLAB language, a matrix-based language allowing the most natural expression of computational mathematics. What is MATLAB MATLAB is Multi-paradigm. So, it can work with multiple types of programming approaches, such as Functional, Object-Oriented, and Visual. MATLAB lets you take your ideas from research to production by deploying to enterprise applications and embedded devices, as well as integrating with Simulink® and Model-Based Design. What is MATLAB MATLAB allows several types of tasks, such as manipulations with matrix, algorithm implementation, data, and functions plotting, and can interact with programs written in other programming languages. BASICS OF MATLAB Local Environment Use MATLAB via remote access. Contact your IT administrator. Install a trial version (which is about 5G and takes about 20 minutes to install) through MathWorks website : https://www.mathworks.com/downloads/web_downloads/ https://matlab.mathworks.com/ - online Current Folder Workspace Window Window The workspace where files are to shows all the be saved variables created and/or imported from files. the prompt Command History Window shows commands that have been entered Interactive Commands ▪ demo will bring up MATLAB examples in the Help Browser, which has examples of some of the features of MATLAB ▪ help will explain any function; help help will explain how help works ▪ doc will bring up a documentation page in the Help Browser. NOTE: To exit from MATLAB, either type quit or exit at the prompt, or click on MATLAB, then Quit MATLAB from the menu. MATLAB Variables To store a value in a MATLAB session, or in a program, a variable is used. Examples: In MATLAB environment, every variable is an array or matrix. Rules for Variable Names ▪ The name must begin with a letter of the alphabet followed by letters, digits, and the underscore character (e.g., value_1). ▪ There is a limit to the length of the name; the built-in function namelengthmax tells what this maximum length is (any extra characters are truncated). ▪ MATLAB is case-sensitive. ▪ Although underscore characters are valid in a name, their use can cause problems with some programs that interact with MATLAB, so some * mynum is a 1 x 1 programmers use mixed case instead (e.g., partNumber instead of matrix part_number). ▪ Reserved words, or keywords cannot be used as variable names. ▪ Names of built-in functions can, but should not, be used as variable names. Commands that Relate to Variables Example: ▪ who shows variables that have been defined in this Command Window ▪ whos shows variables that have been defined in this Command Window (this shows more information on the variables, similar to what is in the Workspace Window) ▪ clear clears out all variables so they no longer exist ▪ clear variablename clears out a particular variable ▪ clear variablename1 variablename2... clears out a list of variables (note: separate the names with spaces). Variable Types (Classes) For float or real numbers → single, double For signed integers → int8 – uses eight bits (one bit for sign, seven bits for actual number) int16, int32, int64 For unsigned integers → uint8 – uses eight bits (sign not stored, actual number must only be positive) uint16, uint32, uint64 For single characters or strings → char (Both characters and strings are enclosed in single quotes) For logical values → logical (used to store true/false values) Note: Example: By default, numbers are stored as the type double in MATLAB. To convert from one type to another, type casting is used. Range Indicates the smallest and largest numbers that can be stored in the type The range can be found for any type by passing the name of the type as a string (which means in single quotes) to the functions intmin and intmax. Example: Note: Use the type int32 when an integer type is required The Format Function It can be used to specify the output format of expressions. There are many options, including making the format short (the default) or long Examples: Example: Scientific or Exponential Notation Uses e for the exponent of 10 raised to a power. Operators The following examples show When you run the the use of file, it produces the arithmetic following result − operators c = 30 a = 10; d = -10 b = 20; e = 200 c=a+b f = 0.50000 d=a-b g= 2 e=a*b z = 343 f=a/b g=a\b x = 7; y = 3; z=x^y Common Built-in Functions Special Variables and Constants ^(t) or e(t) performes exponentation 't' sqrt(t) Performs square root operation on variable ‘t’. min(t) Finds minimum value from array ‘t’. max(t) Finds maximum value from array ‘t’. rem(p,q) Gives the remainder after the dividing ‘p’ by ‘q’. abs(t) Returns absolute value for variable ‘t’. sign(t) Returns sign of the variable ‘t’. ceil(t) Returns ceil value for variable ‘t’. floor(t) Returns floor value for variable ‘t’. round(t) Returns nearest integer of variable ‘t’. sin(t) Performs Sin operation on variable ‘t’. cos(t) Performs cosine operation on variable ‘t’. tan(t) Performs tangent operation on variable ‘t’. asin(t) Performs arc sin operation on variable ‘t’ or Inverse of the sin function. acos(t) Performs arc cosine operation on variable ‘t’ or Inverse of the cos function. atan(t) Performs arc tangent operation on variable ‘t’ or Inverse of the tan function. Example 1: How to calculate the area of a circle in Example 2: How to calculate square root expression in MATLAB? MATLAB? b = pi*(r^2) d = sqrt((x^2)+(y^2)) Solution: Take the value of radius ‘r’ variable 4. Put Solution: In the above equation, put the value of ‘x’ and ‘y’ this value in the equation. variables 13 and 8 respectively. Output in MATLAB: Output in MATLAB: >> r=4; >> b= pi*(r^2) >> x=13; y=8; b = 50.2655 >> d= sqrt((x^2)+(y^2)) d = 15.2643 Characters and Encoding MATLAB uses ASCII character set in encoding characters. A character can be converted to its equivalent numerical value and vice-versa. Examples: Operator Precedence Rules: Relational Operators and Expressions Examples: String concatenation Logical Operators s1 = "State"; s2 = "flow"; dest = strcat(s1,s2); Example: Vectors and Matrices Used to store sets of values, all of which are the same type. The dimensions of a matrix are r x c, where r is the number of rows and c is the number of columns. A vector can be either a row vector or a column vector. If a vector has n elements, ❖ A row vector would have the dimensions 1 x n ❖ A column vector would have the dimensions n x 1 A scalar (one value) has the dimensions 1 x 1 Examples: Scalar Column Row Matrix Vector Vector 1x1 1x4 2x3 3x1 Creating Row Vectors Method 1: Both create a 1 x 4 row vector Containing 1, 2, 3 and 4 Method 2: Using the Colon Operator Creates a 1 x 5 row vector Containing 1, 2, 3, 4 and 5 Creates a row vector with all integers from 1 to 9 in steps of 2 Creating Column Vectors One way to create a column vector is to explicitly put the values in square brackets, separated by semicolons. NOTE: There is no direct way to use the colon operator to get a column vector. Any row vector created using any method can be transposed to result in a column vector. (The rows and columns are interchanged. For vectors, transposing a row vector results in a column vector, and transposing a column vector results in a row vector. In MATLAB, the apostrophe is built in as the transpose operator. Creating Matrices There must always be the same number of values in each row. Referring to and Modifying Vector Elements The elements in a vector are numbered sequentially; each element number is called the index, or subscript. In MATLAB, the indices start at 1. newvec = 1:2:15 Referring to and Modifying Matrix Elements To refer to matrix elements, the row and then the column subscripts are given in parentheses (always the row first and then the column). This is called subscripted indexing. Refers to the first and second rows, second and third columns. An individual element in a matrix can be modified by assigning a new value to it. This creates a matrix variable mat and then refers to the value in the second row, third column of mat: Dimensions The length function → returns the number of elements in a vector. The size function → returns the number of rows and columns in a vector or matrix. The variable r stores the first value returned, which is the number of rows. Vector vec has four elements so its The variable c stores the number of columns. length is 4. For a matrix, the length function will return either the number of It is a row vector, so the size is 1 x rows or the number of columns, whichever is largest. 4. Dimensions MATLAB also has a function numel, which returns the total number of elements in any array (vector or matrix) NOTE: For vectors, this is equivalent to the length of the vector. For matrices, it is the product of the number of rows and columns. Use either the length or numel function to determine the number of elements in a vector Use size (and store the result in two variables) for a matrix. Changing Dimensions – Built-in Functions 1) The reshape function → changes the dimensions of a matrix. 3x4 2x6 Changing Dimensions – Built-in Functions 2) The fliplr function → “flips” the matrix from left to right (in other words, the leftmost column, the first column, becomes the last column and so forth) 3) The flipud function → “flips” up to down. 3x4 Changing Dimensions – Built-in Functions 4) The rot90 function → rotates the matrix counterclockwise 90 degrees. Example: The value in the top right corner becomes instead the top left corner and the last column becomes the first row. 3x4 Empty Vectors An empty vector (a vector that stores no values) can be created using empty square brackets Values can then be added to an empty vector by concatenating, or adding, values to the existing vector. Deleting Elements from a Vector Empty vectors can also be used to delete elements from vectors. The elements in this vector are now numbered 1 through 4. Vectors and Matrices as Function Arguments In MATLAB an entire vector or matrix can be passed as an argument to a function. The function will be evaluated on every element. This means that the result will be the same size as the input argument. sine in radians of every element of a vector vec sign of each element in a matrix Vectors and Matrices as Function Arguments More examples: Cumulative Sum Cumulative Product Note: Resulting vector will have the same length as the input vector Vectors and Matrices as Function Arguments For matrices, all of these functions operate on every individual column. If a matrix has dimensions r x c, the result for the min, max, sum, and prod functions will be a 1 x c row vector, as they return the minimum, maximum, sum, or product, respectively, for every column. (each column of the row) Vectors and Matrices as Function Arguments To find a function for every row, instead of every column, one method would be to transpose the matrix. Vectors and Matrices as Function Arguments As columns are the default, they are considered to be the first dimension. Specifying the second dimension as an argument to one of these functions will result in the function operating rowwise. The syntax is slightly different; for the sum and prod functions, this is the second argument, whereas for the min and max functions it must be the third argument and the second argument must be an empty vector. Note the difference in the format of the output with these two methods (transposing results in row vectors whereas specifying the second dimension results in column vectors). (using row data) Scalar Operations on Vectors and Matrices Scalar Multiplication Multiplying every element in a vector or matrix by a scalar (or dividing every element in a vector or a matrix by a scalar). Array Operations on Vectors and Matrices Array operations are operations that are performed on vectors or matrices term by term or element by element. The two arrays (vectors or matrices) must be the same size to begin with. Array Operations on Vectors and Matrices For any operation that is based on multiplication (which means multiplication, division, and exponentiation), a dot must be placed in front of the operator for array operations. Array Operations on Vectors and Matrices Transpose of a Matrix

Use Quizgecko on...
Browser
Browser