MATLAB Workshop Basics Overview

matlab workshop n.w
1 / 21
Embed
Share

Discover the fundamentals of MATLAB in this workshop introduction. Learn about variables, scalar, vectors, and matrices. Understand the motivation behind using MATLAB, how to work with it, and explore debugging options. Enhance your knowledge of scalar variables, special constants, and common functions for scalars. Dive into vector variables and their definitions. Get started on your MATLAB journey today!

  • MATLAB Workshop
  • Basics
  • Variables
  • Debugging
  • Scalars

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. MATLAB WORKSHOP Part 1: Introduction and Basics

  2. Content Introduction and motivation How to work with MATLAB Everything about variables (scalars, vectors, and matrices)

  3. Motivation MATLAB is widely used in industry Solves difficult problems in a simple way.

  4. How to work How to get MATLAB Work from the computer farm Work from Technion remote connection to the computer farm Download the software to your PC for free through the link: http://matlab.Technion.ac.il

  5. Debugging help help svd Documentation Google MathWorks site forum and file exchange Debugging in the following workshops

  6. Scalar variable For variables we use the following rules: Names should be composed of a-z + A-Z + 0-9 + underscore (_). The language is Case Sensitive Scalars can be defined in different notations: Amp = 10 A23_b = 14.5 a23_b = 12e3 (12 103) z= 2-3i; % a complex number

  7. Special constants ans gives the last calculated result i or j gives 1, there is no difference between using i or j Inf infinity (can be with minus), for example:1/0 NaN not a number, for example 0/0 Pi 3.14159

  8. Common functions for scalars Name of the function Action of the function Addition,substraction, multiplication, power +,-,*,^ abs absolute value of complex number exp(x) exponential function log(x) ln(x) real(x) real part of complex number imag(x) Imaginary part of complex number cos(x), sin(x), tan(x) trigonometrical functions acos(x), asin(x), atan(x) Inverse trigonometrical functions factorial(x) Factorial, x!

  9. Vector variables Definition of vectors: Vector can be defined by square brackets and numbers inside, separated by commas or by spaces: a= [1 2 3]; A = [4, 5, 6+8, 7 pi, 1j]; To access the exact value of a member in a vector we must use ordinary brackets: A(1) % 4 A(3) % 14

  10. Vector variables Definition of vectors: The last element of the vector can be retrieved by: A(end) % i More than 1 element can be accessed: A([1 2]) % [4 5] Elements in the vector can be changed: A(1)=0 % [0 5 14 7 pi j] A([3 4])= [5 6] % [0 5 5 6 pi j] A([3 4])=[] % [0, 5, pi, j] A(end+1)= 2.5 % [0, 5, pi, j, 2.5]

  11. Special vectors 1. Syntax: A= Initial Value: Difference: Final Value Define according to the difference between them: A=a:b:c; % A=[a:b:c] will work as well % A = [a, a+b,a+2b, ,c] Examples: A= 1:2:9 % A=[1 3 5 7 9] B= 9:-1:5 % B= [9,8,7,6,5] C=1:2:8.999 % C= [1 3 5 7] C(2:1:4)= 1 % C([2 3 4]) = 1, C=[1 1 1 1] Default difference is 1: D=2:5 % D=[2 3 4 5]

  12. Special vectors Define by number of points Syntax: A= linspace (initial value, final value, number of points): 1. A=linspace(a,c,n); % A= [a, a+(c-a)/(n-1), a+2*(c-a)/(n-1), ,c] Examples: A=linspace(0,3,4) % A=[0 1 2 3] B=linspace(2,8,5) % B=[2 3.5 5 6.5 8]; There is also a possibility for fixed logarithmical interval: Syntax: A= logspace (initial value, final value, number of points): A=logspace(0,2,3) % A=[10^0 10^1 10^2]

  13. Matrices Definition of column vector: ; instead of , A=[3;5;9;7] Definition of the matrix A=[1 2 3; 4 5 6; 7 8 9] Access matrix elements The first index is the row number, the second index is the column number A(1,2) % 2 A(1:3,2) % <=>A([1 2 3],2) [2;5;8] A(1:2,2:3) % A([1 2],[2 3]) [2 3; 5 6] A(:,1) % first column A(2,:) % second row

  14. Matrices Transformation from matrix to column vector B=A(:); Each vector operation, when done with a matrix, refers to the transformed column vector A(6) % 8 A(2:4) % [4 7 2]

  15. Special Matrices Definitions A= zeros(2,3) % a matrix of 0 s with 2 rows and 3 column [0 0 0; 0 0 0] B= zeros(1,3) % [0 0 0] C= ones(3,2) % [1 1; 1 1 ;1 1] D= ones(2) % a matrix of 1 s with 2 rows and 2 columns [1 1; 1 1] S= eye(2) % [1 0 ; 0 1] F= diag([1 2 3]) % [1 0 0; 0 2 0; 0 0 3] Size of the matrices size returns a vector of length in each dimension length returns the length of the longest dimension numel returns the total number of elements size(A) % [2,3] length(B) % 3 numel(C) % 6

  16. Actions with vectors and matrices Operations with scalars x=[1 2 3; 5 5 0] y=x+2 z=x*2 Operations with matrices: - addition, subtraction, matrix multiplication, multiplication on inverse matrix. + - * / a=[1 0; 0 1]; b=[3 3; 3 3]; c=a*b

  17. Actions with vectors and matrices Operations acting element-by-element - multiplication, division and power .* ./ .^ c=a.*b - conjugate transpose, transpose, conjugate . conj() W=[1+2i,2-3i]; Matrix concatenation: C=[A , B]

  18. Common functions for vectors Function Action of the function Function Action of the function cumsum Cumulative sum of the vector sum Sum of all elements find Find elements according to certain conditions prod Product of all elements max Maximum element in the vector sort Sort in the increasing order min Minimum element in the vector diff Returns the vector of differences between neighbor elements mean Average value std Standard deviation from mean norm Returns Euclidian norm of the vector var Variance trapz Numerical integration by trapezoidal method with unit spacing median Statistical median flip Flip the order of the vector For not-needed output use ~: [~ , index] = max(vec) ;

  19. Common functions for matrices Function Action of the function Function Action of the function repmat(A,m,n) Make matrix consisted of matrix A with m rows of matrix A and n columns of matrix A rank Rank of the matrix meshgrid(xv,yv) Replicates vectors xv and yv to create a full grid reshape Reshape the matrix det Determinant of the matrix sub2ind Matrix indices to vector indices eig Eigenvalues of the matrix ind2sub Vector indices to matrix indices inv Inverse matrix circshift(X,K) Circular shift in array X all elements on K positions flipplr Flip columns from left to right flipud Flip rows form up to down

  20. Organized work At the end of the command ; Prevents printing a command in the command line Separates commands Write commands in separate rows Use comments (%) To separate different cells in the code use %% Matrix/vector operations are much more preferable than loops Clear everything, when start new program clear all % deletes all variables close all % closes all open windows clc % clear screen

  21. Organized work To program you can use the following options: Command line Write a script file Write a function function [output_args] = function_name(input_args) statements % Assign values to output_args end

Related


More Related Content