An introduction to MATLAB for new and advanced...

19
http://user.uni-frankfurt.de/~shahraki/ Teaching > Lecture notes An introduction to MATLAB for new and advanced users An introduction to MATLAB for new and advanced users

Transcript of An introduction to MATLAB for new and advanced...

Page 1: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

http://user.uni-frankfurt.de/~shahraki/

Teaching > Lecture notes

An introduction to MATLABfor new and advanced users

An introduction to MATLAB for new and advanced users

Page 2: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Grading Guidelines

An introduction to MATLAB for new and advanced users

Grading is based on home work/programming project

assignments, a final project, and class participation.

Minimum 80% attendance and minimum 60% marks are

necessary to clear the course

Page 3: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

References

An introduction to MATLAB for new and advanced users

http://user.uni-frankfurt.de/~shahraki/textbooks.htm

Page 4: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Linux Essentials

An introduction to MATLAB for new and advanced users

● echo hello world● ls● man ls ● hostname● date● uname -a● uptime● who am i● who● id● w● time sleep 5● history

● last● w● top ● echo $SHELL● echo {con,pre}{sent,fer}{s,ed}● passwd● clear● cal 2012● top● locate passwd● df, df -h● du -sh .

●cp: copy files (will normally overwrite!)–cp filea fileb

●rm: remove files (for real!)–rm goneforever.dat–rm -i goneforever.dat

●mkdir: make directories–mkdir new_dir/

●cd: change directories (cd ..; cd -; cd ~)●pwd: print current directory

Emacs .profilesetenv SHELL /bin/bash exec /bin/bash --loginSource .profile

Page 5: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Getting Started

Creating Arrays

Mathematical Operations with Arrays

Using Script Files and Managing Data

Two-Dimensional Plots

Programming in MATLAB

User-Defined Functions and Function Files

Polynomials, Curve Fitting, and Interpolation

Applications in Numerical Analysis

Three-Dimensional Plots

Symbolic Math

Contents

An introduction to MATLAB for new and advanced users

Page 6: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

What is MATLAB

Stands for MATrix LABoratory (because its basic data element is a matrix)

MATLAB is a powerful language for technical computing

High level language W…High-Level Language…

It can be used for math computations, modeling and simulations, data analysis andprocessing, visualization and graphics, and algorithm development

The standard MATLAB program has tools (functions) that can be used to solve commonproblems.

In addition, MATLAB has optional toolboxes that are collections of specialized programsdesigned to solve specific types of problems (e.g, signal processing, symbolic calculations).

An introduction to MATLAB for new and advanced users

Page 7: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Computer Languages

An introduction to MATLAB for new and advanced users

High level languages

Matlab (Octave)Mathematica for symbolic mathPython for programming and scripting

Pros:• rapid prototyping, convenient abstractions• convenient debugging• easy access to visualization (key for validation)

Cons:• interpreted at run time, can be slow• may require paying license fees

Low level languages

C, Fortran 77, Fortran 90/95C++, Java

Pros:• freely available compilers and development tools• fast, particularly C, and Fortran• numerous libraries and code fragments available

Cons:• Need to compile• “As is”, no standard interface to plotting• More hands-on & detail-oriented work required, e.g. memory allocation

Wiki: is a programming language with strong

abstraction from the details of the computer…. simpler and more understandable

Page 8: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

MATLAB Desktop

An introduction to MATLAB for new and advanced users

• Command Window

• Current Directory

WorkspaceDouble click on a variableto see it in the Array Editor

Command History

Page 9: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

MATLAB Help

An introduction to MATLAB for new and advanced users

Page 10: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Basic Elements

An introduction to MATLAB for new and advanced users

Some comments about the command window

Write “;” at the end of a line If you don’t want that the intermediate calculus is written in the window while the program is running

Write “%” at the beginning of a line to write a comment in the program

Write “…” at the end of a line if you are writing a very long statement and you want to continue in the next line

Commands can be retrieved with arrow up / arrow down keys ↓↑

Moving around the command line is possible with left / right arrow keys → ←. Go to thebeginning of the line with (Home) and to the end with (End).Esc deletes the whole line.

A program can be stopped with Ctrl+c

The clc command (type clc and press Enter) clears the Command Window.

Page 11: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Arithmetic Operations

An introduction to MATLAB for new and advanced users

Order of Precedence

Page 12: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Display Format

An introduction to MATLAB for new and advanced users

MATLAB has several other formats for displaying numbers.help format

Page 13: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Elementary Functions

An introduction to MATLAB for new and advanced users

MATLAB has a very large library of built-in functions. A function has a name and an argument in parentheses. For example, the function that calculates the square root of a number is sqrt(x)

Page 14: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

Defining Scaler Varilebels

An introduction to MATLAB for new and advanced users

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 can be used in mathematical expressions, in functions, and in any MATLAB statements and commands.

= sign is called the assignment operator.

The left-hand side of theassignment operator caninclude only one variablename.

The right-hand side can be a number, ora computable expression that can includenumbers and/or variables that werepreviously assigned numerical values.

illustrates the difference between theassignment operator and the equal sign.If in this statement the = sign meantequal, the value Of x would be 6(solving the equation for x).

Page 15: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

An introduction to MATLAB for new and advanced users

Several assignments can be typed in the same line. The assignments must be separated with a comma

Defining Scaler Varilebels

Once a variable is defined it can be used as an argument in functions.

Page 16: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

An introduction to MATLAB for new and advanced users

Rules About Variable Names

A variable can be named according to the following rules:

• Must begin with a letter. • Can contain letters, digits, and the underscore character.• Cannot contain punctuation characters (e.g., period, comma, semicolon).• MATLAB is case sensitive• No spaces are allowed between characters (use the underscore where a space is desired). • Avoid using the name of a built-in function for a variable (i.e., avoid using cos, sin, exp, sqrt, etc.).

There are 20 words, called keywords, that are reserved by MATLAB for various purposes and cannot be used as variable names.

break case catch classdef continue else elseifend for function global if otherwise parforpersistent return spmd switch try while

Page 17: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

An introduction to MATLAB for new and advanced users

Useful Commonds

Page 18: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

An introduction to MATLAB for new and advanced users

Script Files

So far all the commands were typed in the Command Window and were executed whenthe Enter key was pressed. Although every MATLAB command can be executed in thisway, using the Command Window to execute a series of commands—especially if they arerelated to each other (a program)—is not convenient and may be difficult or evenimpossible.The commands in the Command Window cannot be saved and Executed again.

A different (better) way of executing commands with MATLAB is first to create a file witha list of commands (program), save it, and then run (execute) the file.

Script files are also called M-files because the extension .m is used when they are saved.

Page 19: An introduction to MATLAB for new and advanced usersuser.uni-frankfurt.de/~shahraki/index_htm_files/Matlab-Lecture1.pdf · An introduction to MATLAB for new and advanced users echo

An introduction to MATLAB for new and advanced users

Examples

A trigonometric identity is given by:EX.1

Verify that the identity is correct by calculating each side of the equation, substituting � = �/5

EX.2Four circles are placed as shown in the figure. At each point where two circles are in contact they are tangent to each other. Determine the distance between the centers C2 and C4 .The radii of the circles are:

�� = 16, �� = 6.5, �� = 12 ��� �� = 9.5 ��