MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Similar documents
Introduction to MATLAB

Introduction to Engineering gii

Introduction to Matlab

Lab 1 - Worksheet Spring 2013

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Introduction to MATLAB Programming

What is Matlab? The command line Variables Operators Functions

Introduction to MATLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Programming in MATLAB

Chapter 2. MATLAB Basis

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

Introduction to MatLab. Introduction to MatLab K. Craig 1

Basic MATLAB Tutorial

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

Chapter 1 Introduction to MATLAB

Digital Image Analysis and Processing CPE

Introduction to Matlab. High-Level Computer Vision Summer Semester 2015

Chapter 3. built in functions help feature elementary math functions data analysis functions random number functions computational limits

Finding, Starting and Using Matlab

To start using Matlab, you only need be concerned with the command window for now.

Programming in Mathematics. Mili I. Shah

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Laboratory 1 Octave Tutorial

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Matlab Programming Introduction 1 2

Introduction to Matlab

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to Matlab

INTRODUCTION TO MATLAB

A Quick Tutorial on MATLAB. Zeeshan Ali

Introduction to PartSim and Matlab

MatLab Just a beginning

MATLAB QUICK START TUTORIAL

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

AMS 27L LAB #1 Winter 2009

PART 1 PROGRAMMING WITH MATHLAB

EP375 Computational Physics

A General Introduction to Matlab

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB. A Tutorial By. Masood Ejaz

MATLAB INTRODUCTION. Risk analysis lab Ceffer Attila. PhD student BUTE Department Of Networked Systems and Services

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

CS129: Introduction to Matlab (Code)

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Stokes Modelling Workshop

Introduction to MATLAB

MATLAB Tutorial The Basics

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Lecturer: Keyvan Dehmamy

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

Matlab Tutorial, CDS

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Mathematical Operations with Arrays and Matrices

Lecture 1: What is MATLAB?

Some elements for Matlab programming

CDA5530: Performance Models of Computers and Networks. Chapter 8: Using Matlab for Performance Analysis and Simulation

Consider this m file that creates a file that you can load data into called rain.txt

Chapter 2. MATLAB Fundamentals

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Welcome to EGR 106 Foundations of Engineering II

What is a Function? EF102 - Spring, A&S Lecture 4 Matlab Functions

Introduction to Matlab

Dr Richard Greenaway

AN INTRODUCTION TO MATLAB

1 Week 1: Basics of scientific programming I

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

What is Matlab? A software environment for interactive numerical computations

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

6.094 Introduction to MATLAB January (IAP) 2009

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

A very brief Matlab introduction

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Introduction to MATLAB

Getting started with MATLAB

LabVIEW MathScript Quick Reference

Introduction to MATLAB

MATLAB The first steps. Edited by Péter Vass

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

LAB 1 General MATLAB Information 1

1. Register an account on: using your Oxford address

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Matlab as a calculator

Introduction to MATLAB 7 for Engineers

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Getting started with MATLAB

Transcription:

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

What is MATLAB? MATLAB (MATrix LABoratory) developed by The Mathworks, Inc. (http://www.mathworks.com) Key Features: High-level language for numerical computation, visualization, and application development Interactive environment for iterative exploration, design, and problem solving Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, numerical integration, and solving ordinary differential equations Built-in graphics for visualizing data and tools for creating custom plots 2

MATLAB s Workspace No need to pre-allocate or declare variables o but sometimes it will speed up the code variables will be automatically stored in the workspace o who : to list all the variables existing in your workspace o whos : similar to who, but with all details such as size and class o clear all : to clear all the variables in the workspace o clear name-of-variable : to clear a specific variable o save FileName : save workspace variables to FileName.mat file o load FileName : load variables from FileName.mat file 3

Rules on Variables and File Names Case sensitive, Var and var are 2 distinct variables. Variable begins with a letter, i.e., A2z or a2z Can be a mix of letters, digits, and underscores ( vector_a) Reserved characters: % = + ~ ; :! [ ] ( ), @ # $ & ^ Script Files: Ordinary ASCII (text) files that contain Matlab commands. Have an extension.m (e.g., myfile.m) and, for this reason, they are known as m-files. The commands in this file may then be executed using >> myfile 4

MATLAB s Syntax % is used for comments >> expression o >> a=2+3 >> function(par1, par2) o >>sum(b,1) % sums the elements of B along the first dimension. >> [out1,out2]=function(par1, par2); o [B, Z]=sort(A, 1); % sorts the elements of A in ascending order along the first dimension. o % Z is the same size as A and describes the arrangement of the elements % of A into B along the sorted dimension. We can add ; at the end of a syntax to suppress (hide) printing the results. 5

MATLAB s Syntax We can add ; to enable multiple commands on the same line. o >> a=6; b=7; We can also use comma, to enable multiple commands on the same line without suppressing printing. o >> a=6, b=7 a = b = 6 7 6

Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = >> a(2,1) ans = 4 1 2 3 4 5 6 7 8 9 >> v1=a(1,:) v1 = 1 2 3 % use space or comma to separate elements on the same % row, semicolon to next row % Matrix indexing (row index, column index) % calls the first row vector 7

Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = >> v2=a(:,2) v2 = 2 5 8 1 2 3 4 5 6 7 8 9 % use space or comma to separate elements on the same % row, semicolon to next row % calls the second column vector 8

Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9 >> a(:,1)=v2 a = 2 2 3 5 5 6 8 8 9 % use space or comma to separate elements on the same % row, semicolon to next row % replaces the first column by the second column vector 9

Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9 >> a(:,2)=[] a = 2 3 5 6 8 9 % use space or comma to separate elements on the same % row, semicolon to next row % removes the second column vector 10

Vectors and Matrices >> a = [1 2 3;... 4 5 6;... 7 8 9] a = 1 2 3 4 5 6 7 8 9 % use "..." to continue on the next line. >> v = 0:2:10 % equidistant elements (start_value : interval : end_value) v = 0 2 4 6 8 10 >> v = 2:8 % default interval is 1 v = 2 3 4 5 6 7 8 11

Special Matrices eye(3) ans = 1 0 0 0 1 0 0 0 1 zeros(3, 3) ans = 0 0 0 0 0 0 0 0 0 % to create a 3 x 3 identity matrix % to create a 3 x 3 zero matrix 12

Special Matrices ones(2, 3) % create a 2 x 3 matrix with all elements = 1 ans = 1 1 1 1 1 1 rand(3, 2) % create a 3 x 2 matrix with all elements randomized between 0 to 1 ans = 0.1 0.9 0.2 0.31 0.15 0.4 13

Operators Basic Operators o + Array/Matrix addition o - Array/Matrix subtraction o * Array/Matrix product o / Array/Matrix division o ^ Matrix power o Array/Matrix conjugate For elementwise operation, place a dot before the operator o.*./.^ Relational Operators: <, <=, >, >=, ==, ~= Logical Operators: &,, ~, xor 14

Operators Example ---- find( conditional statement ) o A=[2 8 7; 1 3 2], I=find(A>=2 & A<=3) A = 2 8 7 1 3 2 I = 1 4 6 15

Some Pre-defined Constant Values >> a = pi a = 3.14159 % the π number >> b= 2+3i % i, j are the imaginary unit i = 1 b = 2+3i >> c = Inf; % The infinity >> d = NaN; % Not a Number, i.e., 0 0 or 16

Some Built-in Functions Trigonometric functions: o sin(x), cos(x), tan(x), cot(x), asin(x), acos(x), atan(x), Exponentials and Logarithms: o exp(x), log(x), log10(x) Other elementary functions: o ceil (x), floor (x), round (x), sign (x) o size(x) % gets the size of the matrix X o abs(x) % gets the absolute value of all the elements in matrix X o max(x) or min(x) % gets the max/min values of the matrix X along the first dimension o mean(x) % gets the mean of the elements of the matrix X along the first dimension o sum(x) % gets the sum of the elements of the matrix X along the first dimension 17

How to Build a Function? Functions are Script Files (m-files) with the first line in the following form function [out1, out2]=funname(par1, par2) The function name should be the same as that of the file, o i.e., the m-file should be save as funname.m Functions are executed using local workspaces o There is no risk of conflicts with the variables in the main workspace. At the end of a function execution only the output arguments will be visible in the main workspace. 18

How to Build a Function? 19

Flow Control Statements If Condition: 20

Flow Control Statements switch Condition: Example 21

Flow Control Statements for Loop: Examples while Loop: 22

Plot Function Example 1 >> x=[0 1 2 3 4]; >> plot(x) % plots x versus its index values >> plot(x, x.^2) % plots x 2 versus x Example 2 >> figure % open new figure >> x=pi*[-1:.05:1]; % π x π >> plot(x, sin(x)); % plots sin(x) versus x >> xlabel('radians'); % Assign label for x-axis >> ylabel('sin value'); % Assign label for y-axis >> title('dummy'); % Assign plot title 23

Plot Function Example 2: with multiple plots on the same figure >> figure % open new figure >> subplot(1, 2, 1); % breaks the figure window into 1-by-2 matrix of small axes % and prints on the first column >> plot(x, sin(x)); % plots sin(x) versus x >> legend('sin(x)') % adds the legend sin x to the plot >> subplot(1, 2, 2); % prints on the second column >> plot(x, cos(x)); % plots cos(x) versus x >> legend( cos(x)') % adds the legend cos x to the plot 24

Thank you 25