intro to matlab

23
Intro to Matlab Rogelio Long September 3, 2013

Upload: kera

Post on 22-Jan-2016

58 views

Category:

Documents


0 download

DESCRIPTION

Intro to Matlab. Rogelio Long September 3, 2013. How to access MyDesktop. https://mydesktop.utep.edu/vpn/index.html Log in with your utep id and password Proceed/Install Citrix(wait for the DL)/Continue. Why use Matlab ?. Pros Easy to learn - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Intro to  Matlab

Intro to Matlab

Rogelio LongSeptember 3, 2013

Page 2: Intro to  Matlab

How to access MyDesktop

• https://mydesktop.utep.edu/vpn/index.html• Log in with your utep id and password• Proceed/Install Citrix(wait for the

DL)/Continue

Page 3: Intro to  Matlab

Why use Matlab?

Pros• Easy to learn• Many built in functions that you may

otherwise have to find libraries for• Easy to programCons• Not free

Page 4: Intro to  Matlab

An expensive calculator

• Basic numerical operations: +,-,*,/,^

• Don’t have to declare a data type• Inf• Nan• Complex ex. -13 + 23.52i

Page 5: Intro to  Matlab

Some useful functions

• Sin, cos, tan, acos, asin, atan• Sqrt, exp,log,log10• Floor, ceil, sign, rem,mod• Sum, max, min• find

Page 6: Intro to  Matlab

Vectors

• a =[2 12 5] row vector• b =[3+ 2 5]• c =[A 2*B]• d= [5;3;7] column vector• e = 1:4• f=2:2:8

Page 7: Intro to  Matlab

Matrices

• A = [2,4;5,1]• B = [1:4;5:8]• C = zeros(3,3) (all zeros)• D = ones(4,2) (all ones)• E = rand(2,2) (random number between 0-1)• F = eye(3) (identity matrix)

Page 8: Intro to  Matlab

Extracting a value from a Matrix/Vector

• a(2)=?• c(1:2:5)=?• A(1,2) = ? (outputs the value in row 1 column

2)• A(3) = ? (outputs the value at index 3 column

major)• A(2,:) = ? (outputs row 2)• A(:,2) = ? (outputs column 2)

Page 9: Intro to  Matlab

Logicals

• true =1• False =0• ==,~=,>,<,>=,<=• And &• Or |

Page 10: Intro to  Matlab

Matrix operations

• A’ (Transpose )• A^-1, inv(A) (Inverse )• eig(A) (Eigen value )• norm(A) (Norm of a matrix )• diag(A) (extracts the diagonal of a matrix)• diag(a) (turns a array into a diagonal matrix)• diag(diag(A))(turns the diagonal of a matrix

into a diagonal matrix)

Page 11: Intro to  Matlab

Size of a matrix/array

• Size(A) (outputs [num row, num columns])

• Length(a) (outputs length of an array)

Page 12: Intro to  Matlab

Neat matrix building

• A = [1:3;4:6;7:9]• B = [diag(diag(A)) A;A' rand(3,3)]• B(4,4)=12• B(8,9)=3 (set a value out of bounds for matrix

B)• C = [B b] (adds array b to matrix B)

Page 13: Intro to  Matlab

Matrix/vector arithmetic

• A+B• A-B• A*B (assuming A & B could bemultiplied)• A/B (this is like A*inv(B))• A.*B• A./B• A^x• A.^x• Ax = b x = inv(A)*b or A\b

Page 14: Intro to  Matlab

Formats

• format short • “ “long• “ “ short e• “ “ long e

Page 15: Intro to  Matlab

Creating a script/function

• File/new/script• F5 save&run

• File/new/function• function [ output_args ] = foo( input_args )• foo(x) (how to call a function)

Page 16: Intro to  Matlab

loops

• for i=1:2:11 …. end• while test is true …. End• if true …. else …. end• Break (ends the deepest loop you are in)

Page 17: Intro to  Matlab

Printing output

• disp(X)• disp(‘some words’)• fprintf(‘some words %5.3d \n’, X)

Page 18: Intro to  Matlab

Plots

• x = linspace(0,2*pi, 100)• y = sin(x)• plot(x,y)• title(‘something”)• xlabel(‘x’)• ylabel(‘y’)

Page 19: Intro to  Matlab

Plots

• color -y,m,c,r,g,b,w,b• line styles -,o,x,+,-,*,:,-.,-- • plot(…….,‘LineWidth’,number……. ,’)• grid• hold on• hold off

Page 20: Intro to  Matlab

Viewing a matrix

• Imagesc(B); colorbar• Axis equal tight

• xa = linspace(-1,1,100);• ya = linspace(-1,1,100);• [X,Y] = meshgrid(xa,ya);• A = (X.^2 + Y.^2)<=0.75

Page 21: Intro to  Matlab

3D plots

• [X,Y] = meshgrid(-2:.1:2,-2:.1:2);• Z = sqrt(X.^2 + Y.^2)• Z = -X.*Y.*exp(-2*(X.^2+Y.^2));

• Mesh(X,Y,Z)• Contour(X,Y,Z)

Page 22: Intro to  Matlab

Other stuff

• diary on• diary off• diary name (sets a name to the diary file)

• Help feature

• demo

Page 23: Intro to  Matlab

Exercise

• fibonacci

• Create script that calls a function