Docsity

MATLAB - Numerical Analysis - Lecture Notes | MTH 654, Study notes of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
MATLAB NOTES
Matlab designed for numerical computing.
Strongly oriented towards use of arrays, one and two di-
mensional.
Excellent graphics that are easy to use.
Powerful interactive facilities; and programs can also be
writteninit.
It is a procedural language, not an object-oriented lan-
guage.
It has facilities for working with both Fortran and C lan-
guage programs.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download MATLAB - Numerical Analysis - Lecture Notes | MTH 654 and more Study notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

MATLAB NOTES

Matlab designed for numerical computing.

Strongly oriented towards use of arrays, one and two di- mensional.

Excellent graphics that are easy to use.

Powerful interactive facilities; and programs can also be written in it.

It is a procedural language, not an object-oriented lan- guage.

It has facilities for working with both Fortran and C lan- guage programs.

USING MATLAB

At the prompt in Unix, type Matlab. Or use the pull down menu accessed with the right button.

Run the Demo program (simply type demo).

To seek help on any command, simply type

help command

To seek information on Matlab commands that involve a given word in their description, type

lookfor word

Look at the various online manuals available thru the course web page.

The default output to the screen is to have 4 digits to the right of the decimal point. To control the formatting of output to the screen, use the command format. The default formatting is obtained using

format short

To obtain the full accuracy available in a number, you can use

format long

The commands

format short e format long e

will use ‘scientific notation’ for the output. Other format options are also available.

MATLAB works very efficiently with arrays, and many tasks are best done with arrays. For example, plot sin x and cos x on the interval 0 ≤ x ≤ 10.

t = 0:.1:10; x = cos(t); y = sin(t); plot(t,x,t,y)

The statement

t = a:h:b;

with h > 0 creates a row vector of the form

t = [a, a + h, a + 2h,... ]

giving all values a + jh that are ≤ b.

When h is omitted, it is assumed to be 1. Thus

n = 1:

creates the row vector

n = [1, 2 , 3 , 4 , 5]

ARRAY OPERATIONS

Addition: Do componentwise addition.

A = [1, 2; 3, -2; -6, 1]; B = [2, 3; -3, 2; 2, -2]; C = A + B;

results in the answer

C =

 

 

Multiplication by a constant: Multiply the constant times each component of the array.

D = 2*A;

results in the answer

D =

 ^26 −^44 − 12 2

 

Matrix multiplication: This has the standard meaning.

E = [1, -2; 2, -1; -3, 2]; F = [2, -1, 3; -1, 2, 3]; G = E*F;

results in the answer

G =

 

 

" 2 − 1 3 − 1 2 3

=

 

 

A nonstandard notation:

H = 3 + F;

results in the computation

H = 3

" 1 1 1 1 1 1

" 2 − 1 3 − 1 2 3

=

" 5 2 6 2 5 6

OTHER COMMANDS

clear: To remove the current variables from use.

cls: To clear the output screen.

help command_name: Brief description of command_name.

help sqrt

results in the output

SQRT Square root.

SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive.

Special arrays:

A = zeros(2,3)

produces an array with 2 rows and 3 columns, with all components set to zero, " 0 0 0 0 0 0

B = ones(2,3)

produces an array with 2 rows and 3 columns, with all components set to 1, " 1 1 1 1 1 1

eye(3) results in the 3 × 3 identity matrix,  

 

SCRIPT FILES

A list of interactive commands can be stored as a script file. For example, store

t = 0:.1:10; x = cos(t); y = sin(t); plot(t,x,t,y)

with the file name plot_trig.m. Then to run the program, give the command

plot_trig

The variables used in the script file will be stored locally, and parameters given locally are available for use by the script file.

FUNCTIONS

To create a function, we proceed similarly, but now there are input and output parameters. Consider a function for evaluating the polynomial

p(x) = a 1 + a 2 x + a 3 x^2 + · · · + an xn−^1

MATLAB does not allow zero subscripts for arrays. The following function would be stored under the name polyeval.m. The coefficients

n aj

o are given to the function in the ar- ray named coeff, and the polynomial is to be evaluated at all of the components of the array x.