+ All Categories
Home > Documents > L01_MATLAB1

L01_MATLAB1

Date post: 28-Oct-2014
Category:
Upload: uddanikku
View: 48 times
Download: 2 times
Share this document with a friend
Popular Tags:
15
Numerical Methods for Civil Engineers Numerical Methods for Civil Engineers Welcome to ภาคการศึกษาที3 ปการศึกษา 2548 Instructor: Mongkol JIRAVACHARADET Instructor: Mongkol JIRAVACHARADET School of Civil Engineering Institute of Engineering Suranaree University of Technology Lecture 1 - MATLAB Introduction Textbook Textbook Applied Numerical Methods Applied Numerical Methods With MATLAB for Engineers With MATLAB for Engineers and Scientists and Scientists STEVEN C. CHAPRA STEVEN C. CHAPRA McGraw-Hill International Edition
Transcript
Page 1: L01_MATLAB1

Numerical Methods for Civil EngineersNumerical Methods for Civil Engineers

Welcome to

ภาคการศึกษาท่ี 3 ปการศึกษา 2548

Instructor: Mongkol JIRAVACHARADETInstructor: Mongkol JIRAVACHARADET

School of Civil Engineering

Institute of Engineering

Suranaree University of Technology

Lecture 1 - MATLABIntroduction

TextbookTextbookApplied Numerical MethodsApplied Numerical MethodsWith MATLAB for EngineersWith MATLAB for Engineersand Scientistsand Scientists

STEVEN C. CHAPRASTEVEN C. CHAPRA

McGraw-Hill International Edition

Page 2: L01_MATLAB1

ReferencesNumerical Methods For Engineers with Personal Computer Applications,(Third Edition) by Chapra, S.C. and R.P. Canale, McGraw-Hill, 1998

Numerical Methods with MATLAB : Implementations and Applications,Gerald W. Recktenwald, Prentice-Hall, 2000

The Matlab 7 Handbook , Mathwork Inc.

KEEP THESE BOOKS! They are excellent career references

(at least for a while)

An Introduction to Numerical Methods : A MATLAB Approach,Abdelwahab Kharab and Ronald B. Guenther, Chapman & Hall/CRC, 2002

Numerical Methods using MATLAB,John H. Mathews and Kurtis D. Fink, Prentice-Hall, 2004

Topics Covered

• Introduction to Matlab

• Approximations and Errors

• Roots of Equations

• Linear Systems

• Curve Fitting

• Interpolation

• Numerical Integration

• Ordinary Differential Equations

• Optimization

Page 3: L01_MATLAB1

Conduct of Course

Homework/Projects/Quizzes 30 %

Midterm Exam 30 %

Final Exam 40 %

Grading Policy

100 - 90Final Score Grade

A89 - 85 B+84 - 80 B79 - 75 C+74 - 70 C69 - 65 D+64 - 60 D59 - 0 F

Page 4: L01_MATLAB1

WARNINGS !!!1) Participation expected, check by quizzes

2) Study in groups but submit work on your own

3) No Copying of Matlab code

4) Submit Homework at the beginning of class

5) Late homework with penalty 30%

6) No make up quizzes or exams

MATLABThe Language of Technical Computing

The latest version Matlab 7.3 R2006b

www.mathworks.com

ATLAB เปนโปรแกรมที่รวมการคํานวณทางคณิตศาสตร การแสดงผล และภาษาที่มีประสิทธิภาพเพ่ือสรางสภาวะแวดลอมที่ยืดหยุนสําหรับการคํานวณทางเทคนิค ดวยสถาปตยกรรมแบบเปด ทําใหเปนการงายที่จะใช MATLAB ในการสํารวจขอมูล สรางอัลกอริธึม หรือสรางเคร่ืองมือคํานวณตามที่ผูใชตองการ

M

Page 5: L01_MATLAB1

MATLAB = MATrix LABoratory- Math and computation

- Algorithm development

- Modeling, Simulation, and Prototyping

- Many toolboxes for solving problems:

Control System Toolbox

Signal Processing Toolbox

System Identification Toolbox

Neural Network Toolbox

Statistics Toolbox

Optimization Toolbox

Partial Diff. Equation Toolbox

Symbolic Math Toolbox

- Data analysis, exploration, and visualization

- Scientific and Engineering Graphics

http://www.math.utah.edu/lab/ms/matlab/matlab.html

http://www.mathworks.com/

http://www.math.mtu.edu/~msgocken/intro/intro.html

MATLAB Educational Sites

http://www.eece.maine.edu/mm/matweb.html

Page 6: L01_MATLAB1

- Getting started

- Basic Arithmetic

- Built-in Functions

- Built-in Variables

- Vector & Matrix

MATLAB 1

The MATLAB System

The MATLAB system consists of five main parts:

Development Environment: set of tools and facilities that help you use MATLAB functions and files. Many of these tools are graphical user interfaces. It includes the MATLAB desktop and Command Window, a command history, an editor and dedugger, and browsers for viewing help, the workspace, files, and the search path.

The MALAB Mathematical Function Library: a vast collection of computational algorithms.

The MATLAB Language: This is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and objected-oriented programming features.

Graphics: MATLAB has extensive facilities 2-D and 3-D data visualization, animation, and presentation graphics.

The MATLAB Application Program Interface (API): allows you to write C and Fortran programs that interact with MATLAB.

Page 7: L01_MATLAB1

Getting Started

MATLAB Desktop

Getting Started

Command Window

Command prompt >> DEMO

Page 8: L01_MATLAB1

Getting Started

Editor/Debugger

Basic Arithmetic

Last-line editing

Calculator functions work as you'd expect:

>>(1+4)*3

ans =

15+ and - are addition, / is division, * is multiplication, ^ is an exponent.

>> 5*5*5>> 5^(-2.5)>> 3*(23+14.7-4/6)/3.5

Up Arrow

Page 9: L01_MATLAB1

>> 2 + 6 - 4ans =

4

The value in ans can be recalled:

>> ans/2

ans = 2

Assign value to a variable:

>> a = 5

>> b = 6

>> c = b/a

>> a=2;>> A=3;>> 2*a>> 2*A

Upper & Lower Case

Several commands in the same line:>>x=2;y=6+x,x=y+7

… For too long command:>> Num_Apples = 10;>> Num_Orange = 25;>> Num_Pears = 12;>> Num_Fruit=Num_Apples+Num_Orange...+ Num_Pears

sin(x), cos(x), tan(x),

sqrt(x), log(x), log10(x),

asin(x), acos(x), atan(x)

Built-in Functions

>> sin(pi/4)ans =

0.7071

>> pians =

3.1416

The output of each command can be suppress by using semicolon ;

>> x = 5;>> y = sqrt(59);>> z = log(y) + x^0.25z =

3.5341

Page 10: L01_MATLAB1

The commas allow more than one commandon a line:

>> a = 5; b = sin(a), c = sinh(a)b =-0.9589

c = 74.2099

MATLAB Variables is created whenever it appearson the left-hand of “ = “

>> t = 5;>> t = t + 2t =

7

Format

>> pi>> format long>> pi>> format short>> format bank>> format short e>> format long e>> format compact>> format loose>> format

Any variable appearing on the right-hand side of“ = “ must already be defined.

>> x = 2*z??? Undefined function or variable ‘z’

Use long variable names is better to rememberand understandable for others

>> radius = 5.2;>> area = pi*radius^2;

>> who>> whos>> clear

Page 11: L01_MATLAB1

Built-in VariablesUse by MATLAB, Should not be assigned to other values

Variable Meaning

ans value of an expression when not assigned to variable

eps floating-point precision

i, j unit imaginary numbers, i = j =pi π = 3.14159265 . . .

realmax largest positive floating-point number

realmin smallest positive floating-point number

Inf ∞, a number larger than realmax, result of 1/0

NaN not a number (0/0)

1−

>> x = 0;>> 5/x>> x/x

>> help log

>> lookfor cosineOn-line Help:

Matrices and Magic SquaresMatrices and Magic Squares

MATLAB allows you to work with entire matrices quickly and easily.

In MATLAB, a matrix is a rectangular array of numbers.

scalars = 1-by-1 matrices

vectors = one row or column matrices

Renaissance engraving Melencolia I by the German artist and amateurmathematician Albrecht Dürer.

The matrices operations in MATLAB are designed to be as natural as possible.

it is usually best to think of everything as a matrix.

Page 12: L01_MATLAB1

Entering MatricesEntering Matrices

Start by entering matrix as a list of its elements.

You only have to follow a few basic conventions:

• Separate the elements of a row with blanks or commas.

• Use a semicolon, ; , to indicate the end of each row.

• Surround the entire list of elements with square brackets, [ ] .

You can enter matrices into MATLAB in several different ways:

• Enter an explicit list of elements.

• Load matrices from external data files.

• Generate matrices using built-in functions.

• Create matrices with your own functions in M-files.

To enter matrix, simply type in the Command Window

>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

Row vector >> A = [ 2 3 5 7 11]Column vector >> A = [ 2; 3; 5; 7; 11]Transposition >> At = A’

Row and Column VectorsRow and Column Vectors

>> A / B>> A ./ B>> A .^ 2>> odd = 1:2:11>> even = 2:2:12>> natural = 1:6>> angle = 0:pi/10:pi;>> sin(angle)

>> A = [ 3 5 7 9 11 ]>> A(3)>> length(A)>> clear(A)>> B = [ 2 4 6 8 10 ]>> A + B>> A - B>> A * B>> A .* B

Arrays OperationsArrays Operations

Page 13: L01_MATLAB1

Generate matrices using builtGenerate matrices using built--in functionsin functions

>> A = zeros(4) >> A = zeros(3,4)>> A = ones(4)>> A = eye(4)>> A = magic(4)

>> S = A + B>> D = A - B>> A*B>> C = [ 10 11; 12 13; 14 15];>> A*C>> A^2>> L = log10(A)

>> [m , n] = size(A)>> det(A)>> inv(A)>> v = [1 2 3];>> A = diag(v)>> B = diag([1 2 1 2])>> w = diag(B)

Elementary Matrix OperationsElementary Matrix Operations

SubscriptsSubscripts

The element in row i and column j of A is denoted by A(i , j)

A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]MATLAB displays the matrix you just entered.

A =16 3 2 135 10 11 89 6 7 124 15 14 1

For example, A(4,2) is the number in the fourth row and second column.

>> A(4,2)ans =

15

Page 14: L01_MATLAB1

>> [A B]

>> size(ans)

>> [A ; B]

SubmatrixSubmatrix

JuxtapositionJuxtaposition

>> A(1,:)

>> A(:,2)

>> A(3:4,1:2)

>> B = [9 8 2 5 ; 4 5 6 7 ; 2 1 3 4]

The Colon OperatorThe Colon Operator

>> 1:10The colon, :, is one of the most important MATLAB operators.

To obtain nonunit spacing, specify an increment.

For example,

>> 100:-7:50and

>> 0:pi/4:pi

linspace function creates row vectors with equally spaced elements.

>> u = linspace(0.0,0.25,5)>> v = linspace(0,9,4)’>> x = linspace(0,pi/6,6*pi);

>> s = sin(x);>> c = cos(x);

>> t = tan(x);

>> [x’ s’ c’ t’]

LinspaceLinspace

Page 15: L01_MATLAB1

Workspace BrowserWorkspace 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.

Example 2.1 Transportation route analysisExample 2.1 Transportation route analysis

The following table gives data for the distance travel along five truck routes and thecorresponding time required to traveled each route. Use the data to compute the average speed required to drive each route. Find the route that has the highest average speed.

7.510.19.18.210.3Time (hrs)

370530490440560Distance (miles)

54321

Solution:>>d = [560, 440, 490, 530, 370]>>t = [10.3, 8.2, 9.1, 10.1, 7.5]>>speed = d./tspeed =

54.3689 53.6585 53.8462 52.4752 49.3333>>[highest_speed, route] = max(speed)highest_speed =

54.3689route =

1


Recommended