Download presentation
Presentation is loading. Please wait.
Published byHomer McKenzie Modified over 9 years ago
1
Intro to Matlab Rogelio Long September 3, 2013
2
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
3
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
4
An expensive calculator Basic numerical operations: +,-,*,/,^ Don’t have to declare a data type Inf Nan Complex ex. -13 + 23.52i
5
Some useful functions Sin, cos, tan, acos, asin, atan Sqrt, exp,log,log10 Floor, ceil, sign, rem,mod Sum, max, min find
6
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
7
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)
8
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)
9
Logicals true =1 False =0 ==,~=,>, =,<= And & Or |
10
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)
11
Size of a matrix/array Size(A) (outputs [num row, num columns]) Length(a) (outputs length of an array)
12
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)
13
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
14
Formats format short “ “long “ “ short e “ “ long e
15
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)
16
loops for i=1:2:11 …. end while test is true …. End if true …. else …. end Break (ends the deepest loop you are in)
17
Printing output disp(X) disp(‘some words’) fprintf(‘some words %5.3d \n’, X)
18
Plots x = linspace(0,2*pi, 100) y = sin(x) plot(x,y) title(‘something”) xlabel(‘x’) ylabel(‘y’)
19
Plots color -y,m,c,r,g,b,w,b line styles -,o,x,+,-,*,:,-.,-- plot(…….,‘LineWidth’,number…….,’) grid hold on hold off
20
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
21
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)
22
Other stuff diary on diary off diary name (sets a name to the diary file) Help feature demo
23
Exercise fibonacci Create script that calls a function
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.