Introduction to MATLAB for Engineering Students, Essays (university) of Matlab skills

intrModuction to Matlab is a very simple software if studied properly so here is the introductry matlab pdf

Typology: Essays (university)

2018/2019

Uploaded on 09/17/2019

anshagr2512
anshagr2512 🇮🇳

1 document

1 / 74

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
INTRODUCTION TO MATLAB FOR
ENGINEERING STUDENTS
David Houcque
Northwestern University
(version 1.2, August 2005)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

Partial preview of the text

Download Introduction to MATLAB for Engineering Students and more Essays (university) Matlab skills in PDF only on Docsity!

INTRODUCTION TO MATLAB FOR

ENGINEERING STUDENTS

David Houcque

Northwestern University

(version 1.2, August 2005)

Contents

  • 1 Tutorial lessons
    • 1.1 Introduction
    • 1.2 Basic features
    • 1.3 A minimum MATLAB session
      • 1.3.1 Starting MATLAB
      • 1.3.2 Using MATLAB as a calculator
      • 1.3.3 Quitting MATLAB
    • 1.4 Getting started
      • 1.4.1 Creating MATLAB variables
      • 1.4.2 Overwriting variable
      • 1.4.3 Error messages
      • 1.4.4 Making corrections
      • 1.4.5 Controlling the hierarchy of operations or precedence
      • 1.4.6 Controlling the appearance of floating point number
      • 1.4.7 Managing the workspace
      • 1.4.8 Keeping track of your work session
      • 1.4.9 Entering multiple statements per line
      • 1.4.10 Miscellaneous commands
      • 1.4.11 Getting help
    • 1.5 Exercises
  • 2 Tutorial lessons
    • 2.1 Mathematical functions
      • 2.1.1 Examples
    • 2.2 Basic plotting
      • 2.2.1 overview
      • 2.2.2 Creating simple plots
      • 2.2.3 Adding titles, axis labels, and annotations
      • 2.2.4 Multiple data sets in one plot
      • 2.2.5 Specifying line styles and colors
    • 2.3 Exercises
    • 2.4 Introduction
    • 2.5 Matrix generation
      • 2.5.1 Entering a vector
      • 2.5.2 Entering a matrix
      • 2.5.3 Matrix indexing
      • 2.5.4 Colon operator
      • 2.5.5 Linear spacing
      • 2.5.6 Colon operator in a matrix
      • 2.5.7 Creating a sub-matrix
      • 2.5.8 Deleting row or column
      • 2.5.9 Dimension
      • 2.5.10 Continuation
      • 2.5.11 Transposing a matrix
      • 2.5.12 Concatenating matrices
      • 2.5.13 Matrix generators
      • 2.5.14 Special matrices
    • 2.6 Exercises
  • 3 Array operations and Linear equations
    • 3.1 Array operations
      • 3.1.1 Matrix arithmetic operations
      • 3.1.2 Array arithmetic operations
    • 3.2 Solving linear equations
      • 3.2.1 Matrix inverse
      • 3.2.2 Matrix functions
    • 3.3 Exercises
  • 4 Introduction to programming in MATLAB
    • 4.1 Introduction
    • 4.2 M-File Scripts
      • 4.2.1 Examples
      • 4.2.2 Script side-effects
    • 4.3 M-File functions
      • 4.3.1 Anatomy of a M-File function
      • 4.3.2 Input and output arguments
    • 4.4 Input to a script file
    • 4.5 Output commands
    • 4.6 Exercises
  • 5 Control flow and operators
    • 5.1 Introduction
    • 5.2 Control flow
      • 5.2.1 The ‘‘if...end’’ structure
      • 5.2.2 Relational and logical operators
      • 5.2.3 The ‘‘for...end’’ loop
      • 5.2.4 The ‘‘while...end’’ loop
      • 5.2.5 Other flow structures
      • 5.2.6 Operator precedence
    • 5.3 Saving output to a file
    • 5.4 Exercises
  • 6 Debugging M-files
    • 6.1 Introduction
    • 6.2 Debugging process
      • 6.2.1 Preparing for debugging
      • 6.2.2 Setting breakpoints
      • 6.2.3 Running with breakpoints
      • 6.2.4 Examining values
      • 6.2.5 Correcting and ending debugging
      • 6.2.6 Ending debugging
      • 6.2.7 Correcting an M-file
  • A Summary of commands
  • B Release notes for Release 14 with Service Pack
    • B.1 Summary of changes
    • B.2 Other changes
    • B.3 Further details
  • C Main characteristics of MATLAB
    • C.1 History
    • C.2 Strengths
    • C.3 Weaknesses
    • C.4 Competition
  • 1.1 Basic arithmetic operators List of Tables
  • 1.2 Hierarchy of arithmetic operations
  • 2.1 Elementary functions
  • 2.2 Predefined constant values
  • 2.3 Attributes for plot
  • 2.4 Elementary matrices
  • 2.5 Special matrices
  • 3.1 Array operators
  • 3.2 Summary of matrix and array operations
  • 3.3 Matrix functions
  • 4.1 Anatomy of a M-File function
  • 4.2 Difference between scripts and functions
  • 4.3 Example of input and output arguments
  • 4.4 disp and fprintf commands
  • 5.1 Relational and logical operators
  • 5.2 Operator precedence
  • A.1 Arithmetic operators and special characters
  • A.2 Array operators
  • A.3 Relational and logical operators
  • A.4 Managing workspace and file commands
  • A.5 Predefined variables and math constants
  • A.6 Elementary matrices and arrays
  • A.7 Arrays and Matrices: Basic information
  • A.8 Arrays and Matrices: operations and manipulation
  • A.9 Arrays and Matrices: matrix analysis and linear equations

List of Figures

1.1 The graphical interface to the MATLAB workspace.............. 3 2.1 Plot for the vectors x and y........................... 15 2.2 Plot of the Sine function............................. 16 2.3 Typical example of multiple plots........................ 17

vii

Acknowledgements

I would like to thank Dean Stephen Carr for his constant support. I am grateful to a number of people who offered helpful advice and comments. I want to thank the EA1 instructors (Fall Quarter 2004), in particular Randy Freeman, Jorge Nocedal, and Allen Taflove for their helpful reviews on some specific parts of the document. I also want to thank Malcomb MacIver, EA3 Honors instructor (Spring 2005) for helping me to better understand the animation of system dynamics using MATLAB. I am particularly indebted to the many students (340 or so) who have used these materials, and have communicated their comments and suggestions. Finally, I want to thank IT personnel for helping setting up the classes and other computer related work: Rebecca Swierz, Jesse Becker, Rick Mazec, Alan Wolff, Ken Kalan, Mike Vilches, and Daniel Lee.

About the author

David Houcque has more than 25 years’ experience in the modeling and simulation of struc- tures and solid continua including 14 years in industry. In industry, he has been working as R&D engineer in the fields of nuclear engineering, oil rig platform offshore design, oil reser- voir engineering, and steel industry. All of these include working in different international environments: Germany, France, Norway, and United Arab Emirates. Among other things, he has a combined background experience: scientific computing and engineering expertise. He earned his academic degrees from Europe and the United States. Here at Northwestern University, he is working under the supervision of Professor Brian Moran, a world-renowned expert in fracture mechanics, to investigate the integrity assess- ment of the aging highway bridges under severe operating conditions and corrosion.

ix

Chapter 1

Tutorial lessons 1

1.1 Introduction

The tutorials are independent of the rest of the document. The primarily objective is to help you learn quickly the first steps. The emphasis here is “learning by doing”. Therefore, the best way to learn is by trying it yourself. Working through the examples will give you a feel for the way that MATLAB operates. In this introduction we will describe how MATLAB handles simple numerical expressions and mathematical formulas. The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide easy access to matrix software developed by the LINPACK (linear system package) and EISPACK (Eigen system package) projects. MATLAB [1] is a high-performance language for technical computing. It integrates computation, visualization, and programming environment. Furthermore, MATLAB is a modern programming language environment: it has sophisticated data structures, contains built-in editing and debugging tools, and supports object-oriented programming. These factors make MATLAB an excellent tool for teaching and research. MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for solving technical problems. MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. The software package has been commercially available since 1984 and is now considered as a standard tool at most universities and industries worldwide. It has powerful built-in routines that enable a very wide variety of computations. It also has easy to use graphics commands that make the visualization of results immediately available. Specific applications are collected in packages referred to as toolbox. There are toolboxes for signal processing, symbolic computation, control theory, simulation, optimiza- tion, and several other fields of applied science and engineering. In addition to the MATLAB documentation which is mostly available on-line, we would

Figure 1.1: The graphical interface to the MATLAB workspace

When MATLAB is started for the first time, the screen looks like the one that shown in the Figure 1.1. This illustration also shows the default configuration of the MATLAB desktop. You can customize the arrangement of tools and documents to suit your needs. Now, we are interested in doing some simple calculations. We will assume that you have sufficient understanding of your computer under which MATLAB is being run. You are now faced with the MATLAB desktop on your computer, which contains the prompt (>>) in the Command Window. Usually, there are 2 types of prompt:

for full version EDU> for educational version

Note: To simplify the notation, we will use this prompt, >>, as a standard prompt sign, though our MATLAB version is for educational purpose.

1.3.2 Using MATLAB as a calculator

As an example of a simple interactive calculation, just type the expression you want to evaluate. Let’s start at the very beginning. For example, let’s suppose you want to calculate the expression, 1 + 2 × 3. You type it at the prompt command (>>) as follows,

1+2* ans = 7 You will have noticed that if you do not specify an output variable, MATLAB uses a default variable ans, short for answer, to store the results of the current calculation. Note that the variable ans is created (or overwritten, if it is already existed). To avoid this, you may assign a value to a variable or output argument name. For example,

x = 1+2* x = 7 will result in x being given the value 1 + 2 × 3 = 7. This variable name can always be used to refer to the results of the previous computations. Therefore, computing 4x will result in

4*x ans =

Before we conclude this minimum session, Table 1.1 gives the partial list of arithmetic operators.

4

1.4.2 Overwriting variable

Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the intermediate results, you can suppress the numerical output by putting a semicolon (;) at the end of the line. Then the sequence of commands looks like this:

t = 5; t = t+ t = 6

1.4.3 Error messages

If we enter an expression incorrectly, MATLAB will return an error message. For example, in the following, we left out the multiplication sign, *, in the following expression

x = 10; 5x ??? 5x | Error: Unexpected MATLAB expression.

1.4.4 Making corrections

To make corrections, we can, of course retype the expressions. But if the expression is lengthy, we make more mistakes by typing a second time. A previously typed command can be recalled with the up-arrow key ↑. When the command is displayed at the command prompt, it can be modified if needed and executed.

1.4.5 Controlling the hierarchy of operations or precedence

Let’s consider the previous arithmetic operation, but now we will include parentheses. For example, 1 + 2 × 3 will become (1 + 2) × 3

(1+2)* ans = 9

and, from previous example

ans = 7

By adding parentheses, these two expressions give different results: 9 and 7. The order in which MATLAB performs arithmetic operations is exactly that taught in high school algebra courses. Exponentiations are done first, followed by multiplications and divisions, and finally by additions and subtractions. However, the standard order of precedence of arithmetic operations can be changed by inserting parentheses. For example, the result of 1+2×3 is quite different than the similar expression with parentheses (1+2)×3. The results are 7 and 9 respectively. Parentheses can always be used to overrule priority, and their use is recommended in some complex expressions to avoid ambiguity. Therefore, to make the evaluation of expressions unambiguous, MATLAB has estab- lished a series of rules. The order in which the arithmetic operations are evaluated is given in Table 1.2. MATLAB arithmetic operators obey the same precedence rules as those in

Table 1.2: Hierarchy of arithmetic operations Precedence Mathematical operations First The contents of all parentheses are evaluated first, starting from the innermost parentheses and working outward. Second All exponentials are evaluated, working from left to right Third All multiplications and divisions are evaluated, working from left to right Fourth All additions and subtractions are evaluated, starting from left to right

most computer programs. For operators of equal precedence, evaluation is from left to right. Now, consider another example:

1 2 + 3^2 +

5 ×^

In MATLAB, it becomes

1/(2+3^2)+4/5*6/ ans =

or, if parentheses are missing,

1/2+3^2+4/5*6/ ans =

7

clear The command clear or clear all removes all variables from the workspace. This frees up system memory. In order to display a list of the variables currently in the memory, type

who

while, whos will give more details which include size, space allocation, and class of the variables.

1.4.8 Keeping track of your work session

It is possible to keep track of everything done during a MATLAB session with the diary command.

diary

or give a name to a created file,

diary FileName

where FileName could be any arbitrary name you choose. The function diary is useful if you want to save a complete MATLAB session. They save all input and output as they appear in the MATLAB window. When you want to stop the recording, enter diary off. If you want to start recording again, enter diary on. The file that is created is a simple text file. It can be opened by an editor or a word processing program and edited to remove extraneous material, or to add your comments. You can use the function type to view the diary file or you can edit in a text editor or print. This command is useful, for example in the process of preparing a homework or lab submission.

1.4.9 Entering multiple statements per line

It is possible to enter multiple statements per line. Use commas (,) or semicolons (;) to enter more than one statement at once. Commas (,) allow multiple statements per line without suppressing output.

a=7; b=cos(a), c=cosh(a) b =

c =

9

1.4.10 Miscellaneous commands

Here are few additional useful commands:

  • To clear the Command Window, type clc
  • To abort a MATLAB computation, type ctrl-c
  • To continue a line, type...

1.4.11 Getting help

To view the online documentation, select MATLAB Help from Help menu or MATLAB Help directly in the Command Window. The preferred method is to use the Help Browser. The Help Browser can be started by selecting the? icon from the desktop toolbar. On the other hand, information about any command is available by typing

help Command

Another way to get help is to use the lookfor command. The lookfor command differs from the help command. The help command searches for an exact function name match, while the lookfor command searches the quick summary information in each function for a match. For example, suppose that we were looking for a function to take the inverse of a matrix. Since MATLAB does not have a function named inverse, the command help inverse will produce nothing. On the other hand, the command lookfor inverse will produce detailed information, which includes the function of interest, inv.

lookfor inverse

Note - At this particular time of our study, it is important to emphasize one main point. Because MATLAB is a huge program; it is impossible to cover all the details of each function one by one. However, we will give you information how to get help. Here are some examples:

  • Use on-line help to request info on a specific function

    help sqrt

  • In the current version (MATLAB version 7), the doc function opens the on-line version of the help manual. This is very helpful for more complex commands. >> doc plot