Introduction to Matlab

Slides:



Advertisements
Similar presentations
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Advertisements

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Introduction to MATLAB The language of Technical Computing.
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
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.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
MATLAB - Basics Centro de Informática Universidade Federal de Pernambuco Aprendizagem de Máquina – IN1102 Arley Ristar –
Introduction to Matlab EE 2303 Lab. MATLAB stands for “Matrix Laboratory” APPLICATIONS OF MATLAB:  Mathematical Calculations  Data Analysis & Visualization.
MATLAB for Image Processing April 10 th, Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Introduction to Matlab Οικονομίδης Δημήτρης
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
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
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Yasser F. O. Mohammad Approximations and Round-off Errors.
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Matlab The language of Technical computing Mr. D. Suresh Assistant Professor, Dept. of CSE, PSNA CET, Dindigul.
Introduction to Matlab
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.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
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  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to Matlab
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn 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
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
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.
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.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Introduction to Matlab
Computer Application in Engineering Design
Lecture: MATLAB Chapter 1 Introduction
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
Introduction To MATLAB
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
CS 175 Project in AI Discussion -- matlab
INTRODUCTION TO MATLAB
How to Use MATLAB A Brief Introduction.
Presentation transcript:

Introduction to Matlab Kocaeli University Digital Signal Processing Applications

What is Matlab? Matlab = Matrix Laboratory Easy to learn Robust Powerful functions and toolboxes (fft, plot, wavread, lpc, hist and many others)

What is Matlab? General Picture Matlab Command Line m-files functions mat-files Command execution like DOS command window Series of Matlab commands Input Output capability Data storage/ loading

Features Command Window type commands Current Directory View folders and m-files Workspace View program variables Double click on a variable to see it in the Array Editor Command History view past commands save a whole session using diary

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;

Array, Matrix A vector x = [1 2 5 1] Transpose y = x’ y = 1 2 5 1 Transpose y = x’ y = 1 2 5 A matrix : Rows are seperated by semicolumn x = [1 2 3; 5 1 4; 3 2 -1] 1 2 3 5 1 4 3 2 -1

Long Array, Matrix t =1:10 k =2:-0.5:-1 B = [1:4; 5:8] 1 2 3 4 5 6 7 8 9 10 k =2:-0.5:-1 k = 2 1.5 1 0.5 0 -0.5 -1 B = [1:4; 5:8] x = 1 2 3 4 5 6 7 8

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 = 0 0 0 x = ones(1,3) 1 1 1 x = rand(1,3) 0.9501 0.2311 0.6068

Matrix Index Given: A(-2), A(0) 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.

Exercise Print the element in row 3 and column 2 Print all elements in row 2 Print first and second elements in column 2

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

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

Matrices Operations Given A and B: Addition Subtraction Product Transpose

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

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

Exercise x=[1 2;3 4]; y=[5 6;7 8]; z=[1 2 3]; t=[4 5 6]; Sum of x and y Product of x and y Element by element product of x and y Element by element division of z over t Element by element division of z over t, but for the first two elements only

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

Plot the function e-x/3sin(x) between 0≤x≤4π Create an x-array of 100 samples between 0 and 4π. Calculate sin(.) of the x-array Calculate e-x/3 of the x-array Multiply the arrays y and y1 >>x=linspace(0,4*pi,100); >>y=sin(x); >>y1=exp(-x/3); >>y2=y*y1;

Plot the function e-x/3sin(x) between 0≤x≤4π Multiply the arrays y and y1 correctly Plot the y2-array >>y2=y.*y1; >>plot(y2)

Display Facilities plot(.) stem(.) subplot(.) x=linspace(0,4*pi,100); y=sin(x); y1=exp(-x/3); y2=y.*y1; plot(x,y2) stem(y2) stem(x,y2) subplot(3,1,1) plot(x,y); subplot(3,1,2) plot(x,y1) subplot(3,1,3) plot(x,y2)

Display Facilities title(.) xlabel(.) ylabel(.) legend(.) >>title(‘This is the sinus function’) >>xlabel(‘x (secs)’) >>ylabel(‘sin(x)’)

Exercise Write a program which plots 2 waves in the same graph. Two cycles of sin wave Two cycles of cos wave. t=0:0.1:4*pi; x=sin(t); y=cos(t); plot(t,x,'r+-',t,y,'bo-.'); figure; plot(t,x,'r+-'); hold on; plot(t,y,'bo-.‘) legend('sin','cos'); hold off;

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

Flow Control if for while break continue return ….

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

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

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

Exercise Write a for program to compute 0.5+1+1.5+2+…+50 With a for loop With a while loop Without loop : Might use sum(.). If possible avoid loops in Matlab. Without loop, without sum(.) a=[0.5:0.5:50]; x=sum(a) b=ones(length(a),1); x=a*b

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

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

Writing User Defined Functions Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function is You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2)

Writing User Defined Functions Examples Write a function : out=squarer (A, ind) Which takes the square of the input matrix if the input indicator is equal to 1 And takes the element by element square of the input matrix if the input indicator is equal to 2 Same Name

Notes: “%” is the neglect sign for Matlab (equivalent 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

Useful Commands The two commands used most by Matlab users are >>help functionname >>lookfor keyword

Exercise Implement fft(.) in Matlab With loops Without loop

Questions? Thank you!