Introduction to MATLAB programming: Fundamentals

Similar documents
UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

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

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Desktop Command window

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

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Introduction to Matlab

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Introduction to MATLAB Practical 1

Matlab Tutorial: Basics

MATLAB TUTORIAL WORKSHEET

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

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

A Guide to Using Some Basic MATLAB Functions

Computational Photonics, Seminar 01 on Introduction into MATLAB, Page 1

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

Introduction to MATLAB

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

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

Lecturer: Keyvan Dehmamy

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

Grace days can not be used for this assignment

Getting started with MATLAB

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Chapter 1 Introduction to MATLAB

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

How to learn MATLAB? Some predefined variables

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

The Mathematics of Big Data

Introduction to MatLab. Introduction to MatLab K. Craig 1

Getting To Know Matlab

Introduction to MATLAB

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

EE 301 Signals & Systems I MATLAB Tutorial with Questions

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

An Introduction to MATLAB

2.0 MATLAB Fundamentals

Computational Mathematics

Introduction to MATLAB LAB 1

A very brief Matlab introduction

Laboratory 1 Octave Tutorial

Stokes Modelling Workshop

Introduction to Matlab

CSCI 6906: Fundamentals of Computational Neuroimaging. Thomas P. Trappenberg Dalhousie University

1 Overview of the standard Matlab syntax

Introduction to MATLAB

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

MatLab Just a beginning

Introduction to MATLAB

What is MATLAB and howtostart it up?

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

MATLAB BASICS. Macro II. February 25, T.A.: Lucila Berniell. Macro II (PhD - uc3m) MatLab Basics 02/25/ / 15

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Matlab Introduction. Scalar Variables and Arithmetic Operators

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

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

Summer 2009 REU: Introduction to Matlab

CME 192: Introduction to Matlab

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

CITS2401 Computer Analysis & Visualisation

A Very Brief Introduction to Matlab

Introduction to Matlab

MATLAB GUIDE UMD PHYS401 SPRING 2012

Eng Marine Production Management. Introduction to Matlab

MATLAB Introductory Course Computer Exercise Session

1 Introduction to Matlab

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

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB

Introduction to Matlab

What is Matlab? A software environment for interactive numerical computations

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Introduction to Matlab

MATH 3511 Basics of MATLAB

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

PART 1 PROGRAMMING WITH MATHLAB

Introduction to Matlab

LAB 1: Introduction to MATLAB Summer 2011

Introduction to MATLAB

TUTORIAL MATLAB OPTIMIZATION TOOLBOX

W1005 Intro to CS and Programming in MATLAB. Plo9ng & Visualiza?on. Fall 2014 Instructor: Ilia Vovsha. hgp://

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction to Matlab for Econ 511b

Getting started with MATLAB

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

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

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Math Sciences Computing Center. University ofwashington. September, Fundamentals Making Plots Printing and Saving Graphs...

MATLAB SUMMARY FOR MATH2070/2970

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

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

Introduction to Matlab

GRAPHICS AND VISUALISATION WITH MATLAB

Transcription:

Introduction to MATLAB programming: Fundamentals Shan He School for Computational Science University of Birmingham Module 06-23836: Computational Modelling with MATLAB

Outline Outline of Topics Why MATLAB? Matrix operations in MATLAB Programming in MATLAB Plotting in MATLAB

Why MATLAB? What is it? A humble origin: an interactive matrix calculator for students. Now more than 1 million users Used in engineering, science, and economics, etc. A high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numerical computation. Matworks

Why MATLAB? Key features High-level language for technical computing. Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration. 2-D and 3-D graphics functions for visualizing data. Interactive tools for iterative exploration, design, and problem solving.

Why MATLAB? Why MATLAB? Concise matrix notation and great matrix manipulation. Easy visualisation. Many useful toolboxes and great community support. Great tool for computational modelling and data analysis.

Why MATLAB? A tour to MATLAB

Matrix operations in MATLAB The most useful command in MATLAB >> help

Matrix operations in MATLAB The heart of MATLAB: matrices MATLAB stands for matrix laboratory. The basic data type is matrix (including vectors). Matrix operations: create, access, modify and manipulate matrices Matrix operation is very fast in MATLAB try to avoid for-loops

Matrix operations in MATLAB Creating Matrices Create an empty matrix: >> A = [] Enter data directly: >> A = [1 1; 2 3; 4 5] If you know the pattern of your matrix: >> A =[1:2:100] A lot of functions to create specific matrices: zeros(), ones(), rand(), eye() Click here to download an example.

Matrix operations in MATLAB Transposing and concatenating matrices To transpose matrix A, use the transpose operator >> B = A To concatenate matrix, enclose them inside of square brackets. >> C = [A; 6 7]

Matrix operations in MATLAB Indexing To extract individual entries from a matrix, use indices inside round brackets: >> A(1,2) Use the : operator to extract all entries along a certain dimension: >> A(1,:) Use end statement to get the last index of a dimension: >> A(end,1) Use logical indexing to find specific entries: >> A(A>2) We can also use find() function to find specific entries: >> A(find(A>2))

Matrix operations in MATLAB Assignment and deletion To change entries in the matrix, using indexing to specify the entries and assign new values: >> A(1,2) = 100 >> C(:, 1:2:end) = 500 To delete entries, assign [] : >> A(1,:) = [] For a matrix, you can only delete column(s) or row(s) For array (1D matrix), you can delete any entries. >> B(3) = []

Matrix operations in MATLAB Reshaping and Replication We can reshape an array of size m-by-n to size p-by-q by reshape() >> A = rand(2,3) >> reshape(a, 1, 6) We can also tile an array m-by-n times using repmat() function: >> A = rand(2,3) >> repmat(a, 1, 6)

Matrix operations in MATLAB Matrix manipulations We can perform matrix addition, subtraction, multiplication, exponentiation, etc. For example, matrix multiplication of an m-by-n matrix and an n-by-p matrix yielding an m-by-p matrix: >> A = rand(3,3) >> B = rand(3,3) >> C = A*B We can do element-wise matrix arithmetic by using. precede the arithmetic operator >> D = A.*B For element-wise arithmetic operation, both matrices must be the same size.

Matrix operations in MATLAB Matrix manipulations We can also do matrix division, but be aware of Matrix Right Divide / and Matrix Left Divide \ >> C = A*B >> Alsq = C/B >> Blsq = A \ C Note that C/B is equivalent to (B \ C ) and A\ C is equivalent to inv(a)*c

Matrix operations in MATLAB Sparse Matrices If we have a large matrix but containing many zeros, it is better to convert it to a sparse matrix: >> A = zeros(1000,1000); >> A = sparse(a); We can use sparse matrices just like ordinary matrices but slower: >> A(10,10)=1; >> B = rand(1000,1000); >> C=A*B; We can always convert sparse matrices to full matrices. >> A = full(a);

Programming in MATLAB Flow of Control MATLAB only has the following statements: if, else, elseif switch statements for loops while loops try/catch statements

Programming in MATLAB Scripts and functions We can use MATLAB editor to edit/save/load/exceute your programs. Two types of MATLAB programs: scripts and functions. A script is a collection of Matlab commands. The commands in the script are executed exactly as at the command prompt. However, scripts: No lexical scoping, that is, the variables in scripts are global. We cannot reuse the same variable name multiple times. Cannot be parameterize to be called multiple times with different inputs. Difficult to read and understand. Slow.

Programming in MATLAB Creating functions Open a new file in MATLAB editor, or type: edit filename.m at the command prompt. In you m file, begin by creating the function header: function [output1,output2, output3...] = myfunction(input1, input2...) We can use the inputs as local variables. All variables are local in a function. We must assign values to each of the outputs before the function terminates. Although optional, it is better to end the function with the end keyword Let s see an example (download from here).

Programming in MATLAB Functions: other issues The functions must located in the directories of the command path, or under the current working directory. Use % for comments We can have multiple functions in a.m file We can pass functions as inputs to other functions by creating handle to the function and then pass the handle as a variable. >> x = fminbnd(@humps, 0.3, 1) We can create anonymous functions without having to store your function to a file each time: fhandle = @(arglist) expr. For example: >> sqr = @(x) x.*x;

Plotting in MATLAB 1D/2D plots To plot 1D and 2D data, we use plot(y). If y is a vector: a piecewise linear graph of the elements of y versus the index of the elements of y If y is a matrix: plot(y) will automatically cycle through a predefined (but customizable) list of colors to allow discrimination among sets of data If we specify two vectors as arguments, plot(x,y) produces a graph of y versus x. Use hold on command to superimpose all the plots onto the same figure. Use hold off to disable.

Plotting in MATLAB 3D plots To plot 3D lines: plot3(x1,y1,z1) To plot 3D shaded surface: surf(x,y,z) You can also specified shading property: shading flat/faceted/interp To plot 3D mesh: mesh(x,y,z) To plot contour lines: contourf(x,y,z) Download my example at here

Plotting in MATLAB Customise plots Multiple subfigures: subplot(nr,nc,i) Title: title( you title ) Axis labels: xlabel( x ); ylabel( y ); zlabel( z ) We need to get handle of a figure Handle of the current figure: gcf() Handle of the current set of axis: gca() To access specific properties: get(handle, property ) To change specific properties: set(handle, property1, value1, property2, value2,...)