230 likes | 390 Views
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
E N D
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 • Many built in functions that you may otherwise have to find libraries for • Easy to program Cons • Not free
An expensive calculator • Basic numerical operations: +,-,*,/,^ • Don’t have to declare a data type • Inf • Nan • Complex ex. -13 + 23.52i
Some useful functions • Sin, cos, tan, acos, asin, atan • Sqrt, exp,log,log10 • Floor, ceil, sign, rem,mod • Sum, max, min • find
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
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)
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)
Logicals • true =1 • False =0 • ==,~=,>,<,>=,<= • And & • Or |
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)
Size of a matrix/array • Size(A) (outputs [num row, num columns]) • Length(a) (outputs length of an array)
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)
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
Formats • format short • “ “long • “ “ short e • “ “ long e
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)
loops • for i=1:2:11 …. end • while test is true …. End • if true …. else …. end • Break (ends the deepest loop you are in)
Printing output • disp(X) • disp(‘some words’) • fprintf(‘some words %5.3d \n’, X)
Plots • x = linspace(0,2*pi, 100) • y = sin(x) • plot(x,y) • title(‘something”) • xlabel(‘x’) • ylabel(‘y’)
Plots • color -y,m,c,r,g,b,w,b • line styles -,o,x,+,-,*,:,-.,-- • plot(…….,‘LineWidth’,number……. ,’) • grid • hold on • hold off
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
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)
Other stuff • diary on • diary off • diary name (sets a name to the diary file) • Help feature • demo
Exercise • fibonacci • Create script that calls a function