1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Introduction to Matlab Οικονομίδης Δημήτρης
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Mikael Johansson and Frank Lingelbach Department of Signals, Sensors, and Systems Introduction to Matlab 2E1215, Lecture 2 – Matlab Programming
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Matlab The language of Technical computing Mr. D. Suresh Assistant Professor, Dept. of CSE, PSNA CET, Dindigul.
Introduction to Matlab
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
1 CS1371 Introduction to Computing for Engineers Introduction to Matlab.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
MATLAB Programming COMM2M Harry R. Erwin, PhD University of Sunderland.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Matlab for Engineers Matlab Environment Chapter 2.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
INTRODUCTION TO MATLAB 1. OUTLINE:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Introduction to Matlab
Lecture: MATLAB Chapter 1 Introduction
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
محاسبات عددی در مهندسی پزشکی جلسه اول و دوم مقدمه ای بر نرم افزار MATLAB گلناز بغدادی 1391.
Introduction to MATLAB [Vectors and Matrices] Lab 2
INTRODUCTION TO MATLAB
Presentation transcript:

1 Introduction to Matlab

2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us MATLAB is a high-level language and interactive environment that enables us to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran. How high? Assembly High Level Languages such as C, Pascal etc. Matlab

3 Matlab Desktop Command Window Launch Pad History

4 Matlab Desktop – cont’d Command Window Workspace Current DIrectory

5 Matlab Help

6 Matlab Programs Matlab is an extravagant calculator if all we can do is execute commands typed into the Command Window… So how can we execute a “program?” Programs in Matlab are: –Scripts, or –Functions Scripts: Matlab statements that are fed from a file into the Command Window and executed immediately Functions: Program modules that are passed data (arguments) and return a result (i.e., sin(x)) These can be created in any text editor (but Matlab supplies a nice built-in editor)

7 Matlab Editor Color keyed text with auto indents tabbed sheets for other files being edited Access to commands

8 The Matlab Environment Matlab is an interpreted language –Commands are typed into the COMMAND Window and executed immediately –Variables are allocated in memory as soon as they are first used in an expression –Commands must be re-entered to be re-executed All variables created in the Command Window are in what is called the Base Workspace –Variables can be reassigned new values as needed –Variables can be selectively cleared from the workspace The Workspace can be saved to a data file –File extension is.mat (ex: mydata.mat) –File is in binary and essentially unreadable by humans –.mat files can be reloaded back into the Matlab Workspace

9 Reserved Words… Matlab has some special (reserved) words that you may not use… for end if while function return elseif case otherwise switch continue else try catch global persistent break

10 Variables No need for types. i.e., All variables are created with double precision unless specified and they are matrices. After these statements, the variables are 1x1 matrices with double precision int a; double b; float c; Example: >>x=5; >>x1=2;

11 Array, Matrix a vector x = [ ] x = a matrix x = [1 2 3; 5 1 4; ] x = transpose y = x’ y =

12 Long Array, Matrix t =1:10 t = k =2:-0.5:-1 k = B = [1:4; 5:8] x =

13 Generating Vectors from functions zeros(M,N)MxN matrix of zeros ones(M,N)MxN matrix of ones rand(M,N)MxN matrix of uniformly distributed random numbers on (0,1) x = zeros(1,3) x = x = ones(1,3) x = x = rand(1,3) x =

14 Matrix Index The matrix indices begin from 1 (not 0 (as in C)) The matrix indices must be positive integer Given: A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals. A(4,2) Error: ??? Index exceeds matrix dimensions.

15 Concatenation of Matrices x = [1 2], y = [4 5], z=[ 0 0] A = [ x y] B = [x ; y] C = [x y ;z] Error: ??? Error using ==> vertcat CAT arguments dimensions are not consistent.

16 Operators (arithmetic) +addition -subtraction *multiplication /division ^power ‘complex conjugate transpose

17 Matrices Operations Given A and B: AdditionSubtractionProductTranspose

18 Operators (Element by Element).*element-by-element multiplication./element-by-element division.^element-by-element power

19 The use of “.” – “Element” Operation K= x^2 Erorr: ??? Error using ==> mpower Matrix must be square. B=x*y Erorr: ??? Error using ==> mtimes Inner matrix dimensions must agree. A = [1 2 3; 5 1 4; 3 2 1] A = y = A(3,:) y= b = x.* y b= c = x. / y c= d = x.^2 d= x = A(1,:) x= 1 2 3

20 Basic Task: Plot the function sin(x) between 0 ≤ x ≤4π Create an x-array of 100 samples between 0 and 4 π. Calculate sin(.) of the x-array Plot the y-array >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y)

21 Display Facilities plot(.) stem(.) Example: >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y) >>plot(x,y) Example: >>stem(y) >>stem(x,y)

22 Operators (relational, logical) == Equal to ~= Not equal to < Strictly smaller > Strictly greater <= Smaller than or equal to >= Greater than equal to & And operator | Or operator

23 Flow Control if for while break ….

24 Control Structures If Statement Syntax if (Condition_1) Matlab Commands elseif (Condition_2) Matlab Commands elseif (Condition_3) Matlab Commands else Matlab Commands end Some Dummy Examples if ((a>3) & (b==5)) Some Matlab Commands; end if (a<3) Some Matlab Commands; elseif (b~=5) Some Matlab Commands; end if (a<3) Some Matlab Commands; else Some Matlab Commands; end

25 Control Structures For loop syntax for i=Index_Array Matlab Commands end Some Dummy Examples for i=1:100 Some Matlab Commands; end for j=1:3:200 Some Matlab Commands; end for m=13:-0.2:-21 Some Matlab Commands; end for k=[ ] Some Matlab Commands; end

26 Control Structures While Loop Syntax while (condition) Matlab Commands end Dummy Example while ((a>3) & (b==5)) Some Matlab Commands; end

27 Use of M-File Click to create a new M-File Extension “.m” A text file containing script or function or program to run

28 Use of M-File If you include “;” at the end of each statement, result will not be shown immediately Save file as Denem430.m

29 Notes: “%” is the neglect sign for Matlab (equaivalent of “//” in C). Anything after it on the same line is neglected by Matlab compiler. Sometimes slowing down the execution is done deliberately for observation purposes. You can use the command “pause” for this purpose pause %wait until any key pause(3) %wait 3 seconds

30 Programming tips and tricks Programming style has huge influence on program speed! tic; X=-250:0.1:250; for ii=1:length(x) if x(ii)>=0, s(ii)=sqrt(x(ii)); else s(ii)=0; end; toc tic; X=-250:0.1:250; for ii=1:length(x) if x(ii)>=0, s(ii)=sqrt(x(ii)); else s(ii)=0; end; toc tic x=-250:0.1:250; s=sqrt(x); s(x<0)=0; toc; tic x=-250:0.1:250; s=sqrt(x); s(x<0)=0; toc; slow.m fast.m Loops are slow: Replace loops by vector operations! Memory allocation takes a lot of time: Pre-allocate memory! Use profile to find code bottlenecks!

31.m files.m files are just text files Do work in.m file Paste work into Matlab prompt Use ; to end lines Save.m files Use % to add comments

32 Basic Operations Always use brackets [ ] to define matrices Always use prentices ( ) to call values of matrices The row is always first, the column is always second, i.e. M1(3,2) is not the same as M1(2,3) To see which variables exist, use >>whos To delete variables, use >>clear x y To find out size use >>size(M1)

33 Strengths of MATLAB MATLAB is relatively easy to learn MATLAB code is optimized to be relatively quick when performing matrix operations MATLAB may behave like a calculator or as a programming language MATLAB is interpreted, errors are easier to fix Although primarily procedural, MATLAB does have some object-oriented elements

34 Weaknesses of MATLAB MATLAB is NOT a general purpose programming language MATLAB is an interpreted language (making it for the most part slower than a compiled language such as C++) MATLAB is designed for scientific computation and is not suitable for some things (such as parsing text)

35 References Mastering MATLAB 7. Duane C. Hanselman, Bruce L. Littlefield. Prentice Hall, 2004.Duane C. HanselmanBruce L. Littlefield MATLAB: An Introduction with Applications. Amos Gilat. Wiley, 2003.Amos Gilat