MATLAB: High-Level Programming for Math and Data Analysis, Slides of Computational Physics

An introduction to matlab, a powerful numerical computing environment and high-level programming language. Learn about its capabilities, applications, and features such as matrix and vector-based methods, mathematical functions, and integration with other languages. Discover how to use matlab for mathematics, computation, algorithm development, data acquisition, modeling, simulation, data analysis, and scientific graphics.

Typology: Slides

2011/2012

Uploaded on 08/12/2012

laniban
laniban 🇮🇳

4

(1)

78 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Preview
docsity.com
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

Partial preview of the text

Download MATLAB: High-Level Programming for Math and Data Analysis and more Slides Computational Physics in PDF only on Docsity!

Preview

What is MATLAB?

It is a numerical computing environment ;

A high-level programming language;

Matrix and vector-based methods;

It can be used for:

  • Mathematics and computation
  • Easy vector and matrix entering and manipulation
  • Plotting of information
  • Implementation of algorithms
  • Interfacing with programs in other languages

Introduction

MATLAB system:

Integrative Development Environment (IDE) MATLAB Mathematical Function Library

  • Including Bassel functions and fast Fourier Transformation MATLAB Language
  • high-level matrix/array language
  • object-oriented Graphics
  • display vectors and matrices as graphs API
  • C/Fortran program can interact with MATHLAB

Introduction--Desktop

Introduction -- Desktop

Editor/Debugger:

Enter a matrix

  • 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, [ ].
  • To enter a matrix, simply type in the Command Window

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 13 5 10 11 8 9 6 7 12 4 15 14 1

  • This matrix matches the numbers.

Transpose

The transpose operation is denoted by an apostrophe or single quote, '. It flips a matrix about its main diagonal and it turns a row vector into a column vector.

So

A'

produces

ans = 16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1

sum

and

sum(A')'

produces a column vector containing the row sums

ans =

Matrix

If you try to use the value of an element outside of the matrix, it is an error: t = A(4,5) Index exceeds matrix dimensions.

On the other hand, if you store a value in an element outside of the matrix, the size increases to accommodate the newcomer: X = A; X(4,5) = 17 X = 16 3 2 13 0 5 10 11 8 0 9 6 7 12 0 4 15 14 1 17

The Colon Operator

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

It occurs in several different forms. The expression 1:

is a row vector containing the integers from 1 to 10:

1 2 3 4 5 6 7 8 9 10

To obtain non-unit spacing, specify an increment. For example,

100:-7:

is

100 93 86 79 72 65 58 51

and

0:pi/4:pi

is

0 0.7854 1.5708 2.3562 3.

Variable name

MATLAB does not require any type declarations or dimension statements. When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. For example, num_students = 25

creates a 1-by-1 matrix named num_students and stores the value 25 in its single element. To view the matrix assigned to any variable, simply enter the variable name.

Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters A and a are not the same variable.

Numbers

MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are 3 -99 0. 9.6397238 1.60210e-20 6.02252e 1i -3.14159j 3e5i

All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite range of roughly 10-308^ to 10+308.

Functions

MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin. Taking the square root or logarithm of a negative number is not an error; the appropriate complex result is produced automatically. MATLAB also provides many more advanced mathematical functions, including Bessel and gamma functions. Most of these functions accept complex arguments. For a list of the elementary mathematical functions, type

help elfun

For a list of more advanced mathematical and matrix functions, type

help specfun

help elmat

Some of the functions, like sqrt and sin, are built in.

Built-in functions are part of the MATLAB core so they are very efficient, but the computational details are not readily accessible. Other functions, like gamma and sinh, are implemented in M-files.

Functions: Examples of Expressions

Here are a few examples, and the resulting values: rho = (1+sqrt(5))/ rho =

a = abs(3+4i) a = 5 z = sqrt(besselk(4/3,rho-i)) z = 0.3730+ 0.3214i huge = exp(log(realmax)) huge = 1.7977e+ toobig = pi*huge toobig = Inf docsity.com