Docsity

introduction to matlab application to electrical engineering, Notas de estudo de Cultura

Pré-visualização parcial do texto

Baixe introduction to matlab application to electrical engineering e outras Notas de estudo em PDF para Cultura, somente na Docsity!

Introduction to Matlab:

Application to Electrical

Engineering

Houssem Rafik El Hana Bouchekara

Umm El Qura University

(version 1, Februray 2011)

Contents

  • 1 CHAPTER
    • 1.1 TUTORIAL LESSONS
      • 1.1.1 Introduction
    • 1.2 STARTING AND QUITTING MATLAB
      • 1.2.1 Starting MATLAB
      • 1.2.2 Quitting MATLAB
    • 1.3 MATLAB DESKTOP..................................................................................................................................
    • 1.4 DESKTOP TOOLS
      • 1.4.1 Command Window
      • 1.4.2 Command History
      • 1.4.3 Launch Pad
      • 1.4.4 Help Browser
      • 1.4.5 Current Directory Browser
      • 1.4.6 Workspace Browser......................................................................................................................
      • 1.4.7 Editor/Debugger
    • 1.5 GETTING STARTED
      • 1.5.1 Using MATLAB as a calculator......................................................................................................
      • 1.5.2 Creating MATLAB variables
      • 1.5.3 Overwriting variable
      • 1.5.4 Error messages
      • 1.5.5 Making corrections
      • 1.5.6 Controlling the hierarchy of operations or precedence
      • 1.5.7 Controlling the appearance of floating point number..................................................................
      • 1.5.8 Managing the workspace
      • 1.5.9 Keeping track of your work session
      • 1.5.10 Entering multiple statements per line
      • 1.5.11 Miscellaneous commands
      • 1.5.12 Getting help
    • 1.6 EXERCISES.............................................................................................................................................
  • 2 CHAPTER
    • 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.2.6 Copy/Paste Figures
      • 2.2.7 Saving Figures
    • 2.3 EXERCISES.............................................................................................................................................
    • 2.4 ANIMATIONS
      • 2.4.1 Erase Mode Method
      • 2.4.2 Creating Movies
    • 2.5 WORKING WITH MATRICES
      • 2.5.1 Introduction
      • 2.5.2 Matrix generation
    • 2.6 EXERCISES.............................................................................................................................................
  • 3 CHAPTER 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 CHAPTER 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/OUTPUT COMMANDS....................................................................................................................
    • 4.5 EXERCISES.............................................................................................................................................
  • 5 CHAPTER 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 CHAPTER 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
  • 7 APPENDIX A: SUMMARY OF COMMANDS
  • 8 APPENDIX C: MAIN CHARACTERISTICS OF MATLAB.............................................................................
    • 8.1 HISTORY
    • 8.2 STRENGTHS
    • 8.3 WEAKNESSES
    • 8.4 COMPETITION
  • 9 BIBLIOGRAPHY

Matlab is an interactive system for doing numerical computations. The aim of this book is to help the student to be familiar with Matlab. The emphasis here is "learning by doing".

1 Chapter 1

1.1 Tutorial lessons 1

1.1.1 Introduction

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.

The basic building block in MATLAB is the matrix. The fundamental data type is the array. Vectors, scalars, real and complex matrices are all automatically handled as special cases of basic arrays. The built- in functions are optimized for vector operations. Thus, vectorized commands or codes run much faster in MATLAB ( vectorization is a way of computing in which an operation is performed simultaneously on a list of numbers rather than sequentially on each member of the list).

A nice thing to realize is that MATLAB is primarily a numerical computation package, although with the ' Symbolic ' Toolbox it can do also symbolic algebra. Mathematica, Maple, and Macsyma are primarily symbolic algebra packages. MATLAB's ease of use is its best feature since you can have more learning with less effort, while the computer algebra systems have a steeper learning curve.

In mathematical computations, especially those that utilize vectors and matrices, MATLAB is better in terms of ease of use, availability of built-in functions, ease of programming, and speed. MATLAB's popularity today has forced such packages as Macsyma and Mathematica to provide extensions for files in MATLAB's format.

There are numerous prepared commands for 2D and 3D graphics as well as for animation. The user is not limited to the built-in functions; he can write his own functions in MATLAB language. Once written, these functions work just like the internal functions. MATLAB's language is designed to be easy to learn and use.

The many built-in functions provide excellent tools for linear algebra, signal processing, data analysis, optimization, solution of ordinary differential equations (ODEs), and many other types of scientific operations.

There are also several optional 'toolboxes' available which are collections of functions written for special applications such as ' Image Processing ', 'Statistics', 'Neural Networks', etc.

The software package has been commercially available since 1984 and is now considered as a standard tool at most universities and industries worldwide.

1.2 Starting and Quitting MATLAB

1.2.1 Starting MATLAB

On a Microsoft Windows platform, to start MATLAB, double-click the MATLAB shortcut icon on your Windows desktop.

On Linux, to start MATLAB, type matlab at the operating system prompt.

After starting MATLAB, the MATLAB desktop opens – see “MATLAB Desktop”.

You can change the directory in which MATLAB starts, define startup options including running a script upon startup, and reduce startup time in some situations.

1.2.2 Quitting MATLAB

To end your MATLAB session, select Exit MATLAB from the File menu in the desktop, or type quit in the Command Window. To execute specified functions each time MATLAB quits, such as saving the workspace, you can create and run a finish.m script.

1.3 MATLAB Desktop

When you start MATLAB, the MATLAB desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB.

The first time MATLAB starts, the desktop appears as shown in the following illustration, although your Launch Pad may contain different entries.

Figure 2: Customization

1.4 Desktop Tools

This section provides an introduction to MATLAB’s desktop tools. You can also use MATLAB functions to perform most of the features found in the desktop tools. The tools are:

- Command Window”. - Command History. - Launch Pad - Help Browser. - Current Directory Browser. - Workspace Browser. - Array Editor. - Editor/Debugger.

1.4.1 Command Window

Use the Command Window to enter variables and run functions and M-files.

Figure 3: Command Window.

1.4.2 Command History

Lines you enter in the Command Window are logged in the Command History window. In the Command History, you can view previously used functions, and copy and execute selected lines.

Figure 4: Command History.

To save the input and output from a MATLAB session to a file, use the diary function.

Figure 6: Help Browser

1.4.5 Current Directory Browser

MATLAB file operations use the current directory and the search path as reference points. Any file you want to run must either be in the current directory or on the search path.

A quick way to view or change the current directory is by using the Current Directory field in the desktop toolbar as shown below.

Figure 7: Current Directory Browser.

To search for, view, open, and make changes to MATLAB-related directories and files, use the MATLAB Current Directory browser. Alternatively, you can use the functions dir, cd, and delete.

Figure 8: Current Directory Browser.

Search Path

To determine how to execute functions you call, MATLAB uses a search path to find M-files and other MATLAB-related files, which are organized in directories on your file system. Any file you want to run in MATLAB must reside in the current directory or in a directory that is on the search path. By default, the files supplied with MATLAB and MathWorks toolboxes are included in the search path.

To see which directories are on the search path or to change the search path, select Set Path from the File menu in the desktop, and use the Set Path dialog box. Alternatively, you can use the path function to view the search path, addpath to add directories to the path, and rmpath to remove directories from the path.

1.4.6 Workspace Browser

The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces.

To view the workspace and information about each variable, use the Workspace browser, or use the functions who and whos.

To delete variables from the workspace, select the variable and select Delete from the Edit menu. Alternatively, use the clear function.

1.4.7 Editor/Debugger

Use the Editor/Debugger to create and debug M-files, which are programs you write to run MATLAB functions. The Editor/Debugger provides a graphical user interface for basic text editing, as well as for M-file debugging.

Figure 11: Editor/Debugger.

You can use any text editor to create M-files, such as Emacs, and can use preferences (accessible from the desktop File menu) to specify that editor as the default. If you use another editor, you can still use the MATLAB Editor/ Debugger for debugging, or you can use debugging functions, such as dbstop, which sets a breakpoint.

If you just need to view the contents of an M-file, you can display it in the Command Window by using the type function.

1.5 Getting started

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.5.1 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, 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. 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 commonly used MATLAB operators and special characters used to solve many engineering and science problems.

1.5.3 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.5.4 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.5.5 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.5.6 Controlling the hierarchy of operations or precedence

Let’s consider the previous arithmetic operation, but now we will include parentheses. For example, will become

(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 is quite different than the similar expression with parentheses. 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 2Table 2. MATLAB arithmetic operators obey the same precedence rules as those in

Table 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:

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 =