Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to MATLAB: A Sophisticated Scientific Computing Tool, Lecture notes of Matlab skills

An introduction to MATLAB, a high-level programming language and graphing calculator commonly used in scientific computing. It covers the basics of MATLAB, including arithmetic operations, programming syntax, and variable usage. The document also includes examples and exercises to help readers better understand the concepts.

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

anuprabha
anuprabha šŸ‡ŗšŸ‡ø

4.4

(18)

237 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to MATLAB
•Very sophisticated ā€œgraphing calculatorā€
•High-level programming language for sci-
entific computing
•Similar to PYTHON, with more built-in BUT
not open-source
•vs. c or FORTRAN, many built-in com-
mands, less-complex syntax
•vs. c or FORTRAN, Interpreted language
(code translated during run), with some pre-
compiled functions
•IF used carefully, good balance of speed
and ease of use
Our introduction follows, in part, section 1
of the excellent ā€œAn introduction to Matlab for
dynamic modelingā€ by Guckenheimer and Ell-
ner:
www.cam.cornell.edu/ ∼
dmb/DMBsupplements.html, as well as mate-
rial from Prof. Mark Goldman, UC Davis
Launch matlab!
Command window (graphing calculator) mode
>> 1+3
ans =
4
Assigning values to variables
>> a=1+3
a =
4
Displaying values
>> a
a =
4
Suppressing display of output in MATLAB
>> a=1+3;
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Introduction to MATLAB: A Sophisticated Scientific Computing Tool and more Lecture notes Matlab skills in PDF only on Docsity!

Introduction to MATLAB

  • Very sophisticated ā€œgraphing calculatorā€
  • High-level programming language for sci- entific computing
  • Similar to PYTHON, with more built-in BUT not open-source
  • vs. c or FORTRAN, many built-in com- mands, less-complex syntax
  • vs. c or FORTRAN, Interpreted language (code translated during run), with some pre- compiled functions
  • IF used carefully, good balance of speed and ease of use

Our introduction follows, in part, section 1 of the excellent ā€œAn introduction to Matlab for dynamic modelingā€ by Guckenheimer and Ell- ner: www.cam.cornell.edu/ ∼ dmb/DMBsupplements.html, as well as mate- rial from Prof. Mark Goldman, UC Davis

Launch matlab! Command window (graphing calculator) mode

1+ ans = 4

Assigning values to variables

a=1+ a = 4 Displaying values a a = 4 Suppressing display of output in MATLAB a=1+3;

BASIC OPERATIONS:

Arithmetic

/ (divide)

  • (multiply) ˆ (exponent) abs(x) (absolute value) cos(x), sin(x), tan(x) exp(x) exponential function eˆx log(x) log to base e sqrt(x) square root

The latter are built-in functions

a = sqrt(5) a = 1. a=2 ; b=4 ; c=aˆb c = 16

There are also some built-in variables

pi ans = 3. cos(pi) ans = -

Variable names:

a1= my_favorite_variable_number_2=

VALID: Letter followed by letters, numbers, un- derscore character. NOTE! capitalization matters. A 6 = a !!

NOT VALID:

my_favorite_variable#2= ??? my_favorite_variable#2= 2a= Error: Unexpected MATLAB expression.

And using periods gives a different, more advanced type of variable (data structure) that we won’t discuss now

my.favorite.variable.name= my = favorite: [1x1 struct]

ORDER OF OPERATIONS:

PEMDAS

parenthesis exponentiation multiplication division addition subtraction Say we want: a = 2 , b = 4 , c = (^) a+ab

a=1 ; b=4 ; c=a/a+b c = 5

a=1 ; b=4 ; c=a/(a+b) c = 0. !! When in doubt, put lots of parentheses !!

CHECK: What do you get for the below, and why?

a=1 ; b=4 ; c =0 ; d=a/0+b d=a/(0+b) d=-aˆ d=(-a)ˆ

Representation of numbers in scientific computing: finite precision Standard: IEEE floating point arithmetic. Important feature – finite precision: approx 16 significant digits

Display more digits:

a=0. a =

format long a a = 0.

Roundoff error:

a=4/3 ; b=a-1 ; c = (3*b)-

c =

-2.220446049250313e-

OVERFLOW AND UNDERFLOW:

Maximum number: ā‰ˆ 10308 Minimum number: ā‰ˆ 10 āˆ’^308 Overflow:

a=10ˆ a = Inf

Underflow

a=10ˆ- a = 0

Another special number: not defined

ans = NaN

VECTORS

A vector value is just a list of scalar values. Arranged horizontally into a row vector:

~x = (6 12 5). (1) Or vertically into a column vector:

~x =

In MATLAB, row vector:

x=[6 12 5] x = 6 12 5

OR

x=[6 ,12 ,5] x = 6 12 5

In MATLAB, column vector:

x=[6 ; 12 ; 5]

x =

6 12 5

The transpose operation switches between row and column vectors. This is given by dot prime in MATLAB. That is, in MATLAB:

y=x.’

Exercise 0.1 : Have MATLAB compute the values of

  1. Make a list numbers spaced by 0.2, between a minimum value of 1 and a maximum value of 20. Assign that list to the variable name myvector.
  2. use help to look up the linspace command, and repeat the previous command using linspace
  3. pick out the 4th value of myvector and assign it to the variable name fourthelement

TWO BASIC OPERATIONS ON VECTORS:

Multiplication by scalar

c~x =

x 1 x 2 x 3

cx 1 cx 2 cx 3

; that is, xj → c xj

Matlab *

x=[1;2;3] ; 3*x

ans = 3 6 9

Addition of two vectors

~x + ~y =

x 1 + y 1 x 2 + y 2 x 3 + y 3

Subtraction similar

Works the same for row and column vectors

Matlab + and -

x=[1;2] ; y=[0;-2]; z=x+y z = 1 0 z+ [2 2] ??? Error using ==> plus

Matrix dimensions must agree: add row + row or col + col

CAUTION! Multiplication of vectors does NOT work the same way ... more later (matrix- vec- tor multiplication)

FUNDAMENTAL CONCEPT: Matrix-vector multiplication

( A 1 , 1 A 1 , 2 A 2 , 1 A 2 , 2

x 1 x 2

= x 1

A 1 , 1

A 2 , 1

  • x 2

A 1 , 2

A 2 , 2

e.g. (^) ( 1 1 1 2

In general,  

a 1 Ā· Ā· Ā· an | Ā· Ā· Ā· |

x 1 ... xn

j

xj

aj |

Exercise 0.3 : Compute the below by hand ...

  • (^) ( 2 āˆ’ 3 0 1

MATLAB * operator

A=[1 1 ; 1 2] ; A*[1 ; 2]

ans =

3 5

In y = Ax, A must have same number of columns as x has rows. Nonsense:

( 1 1 1 2

>> A=[1 1 ; 1 2] ; A*[1 ; 2 ; 4]

??? Error using ==> mtimes Inner matrix dimensions must agree.

Exercise 0.4 :

  • Check your answers to the hand calcula- tions from the previous exercise, using MAT- LAB.

PLOTTING

  • Plotting is MATLAB is great, and best learne just by doing ...
  • Write a program plot_a_sin_wave.m tha plots sin(x) from x = āˆ’Ļ€ to x = Ļ€. HINT: type doc plot!

The for loop

Use for repeated operations. Basic structure: MATLAB CODE

for n= 1: disp(n) end

  • Use edit to code this into a program myloop.m, save it and run it!

COMPONENTS OF THE FOR LOOP:

n loop variable 1:3 loop vector disp(n) or print(n) command

Code starts with n equal to first element in loop vector runs command advances n to next element repeats quits when have covered all elements of loop vector

In more detail:

  • in any for loop, we define a list of numbers, here 1 through 9. think of this as the ā€loop vector.ā€ The loop vector can be any list of numbers – it does not have to be ā€œintegers counting up.ā€
  • the loop variable, n, starts at the first num- ber in the list. it is set equal to that value
  • the commands (here, just printing the value of n to the screen) are run
  • then when end is reached, n is reset to the NEXT number in the list, the commands are run, and the process is repeated
  • it terminates when all entries of the loop vector have been used.

Run and describe what happens for these examples:

for p=[4 6 67 -1] disp(p) end

for k=1:2: disp(k) end

a= for k=1: a=a+k end