Short Introduction into MATLAB

Similar documents
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

Introduction to Matlab

Desktop Command window

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

Laboratory 1 Octave Tutorial

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.

Introduction to MATLAB LAB 1

Stokes Modelling Workshop

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

MATLAB/Octave Tutorial

The Mathematics of Big Data

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

Introduction to MATLAB

Lecturer: Keyvan Dehmamy

Introduction to MATLAB

A Guide to Using Some Basic MATLAB Functions

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

Programming in Mathematics. Mili I. Shah

Programming 1. Script files. help cd Example:

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

MATLAB TUTORIAL WORKSHEET

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

Getting To Know Matlab

A Brief Introduction to MATLAB

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

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

An Introduction to MATLAB

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

Matlab Tutorial: Basics

Introduction to MatLab. Introduction to MatLab K. Craig 1

Introduction to MATLAB programming: Fundamentals

Getting Started with MATLAB

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

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

Introduction to Matlab. By: Hossein Hamooni Fall 2014

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

Some elements for Matlab programming

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

Introduction to MATLAB

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

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

INTRODUCTION TO NUMERICAL ANALYSIS

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

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

Matlab and Octave: Quick Introduction and Examples 1 Basics

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Overview. Linear Algebra Notation. MATLAB Data Types Data Visualization. Probability Review Exercises. Asymptotics (Big-O) Review

Matlab Programming Arrays and Scripts 1 2

MATLAB SUMMARY FOR MATH2070/2970

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Using the fprintf command to save output to a file.

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

MATLAB Operators, control flow and scripting. Edited by Péter Vass

Working with Excel Files. a = round(rand(5,3)*100) xlswrite('rand.xlsx',a) b = xlsread('rand.xlsx')

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

AN INTRODUCTION TO MATLAB

Chapter 1 Introduction to MATLAB

Short Version of Matlab Manual

What is MATLAB and howtostart it up?

Matlab Tutorial, CDS

Matlab Tutorial. CS Scientific Computation. Fall /51

CS335. Matlab Tutorial. CS335 - Computational Methods in Business and Finance. Fall 2016

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

A quick Matlab tutorial

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

Eng Marine Production Management. Introduction to Matlab

Introduction in MATLAB (TSRT04)

Introduction to MATLAB Practical 1

MATLAB Premier. Asst. Prof. Dr. Melik DÖLEN. Middle East Technical University Department of Mechanical Engineering 10/30/04 ME 304 1

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

Introduction to MATLAB

MatLab Just a beginning

Matlab is a tool to make our life easier. Keep that in mind. The best way to learn Matlab is through examples, at the computer.

Mathworks (company that releases Matlab ) documentation website is:

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).

A very brief Matlab introduction

Getting started with MATLAB

Math 375 Natalia Vladimirova (many ideas, examples, and excersises are borrowed from Profs. Monika Nitsche, Richard Allen, and Stephen Lau)

Introduction to MATLAB

CM0340 Tutorial 2: More MATLAB

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Introduction to MATLAB

MATLAB Introductory Course Computer Exercise Session

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

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

How to learn MATLAB? Some predefined variables

Introduction and MATLAB Basics

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

MATLAB Project: Getting Started with MATLAB

Matlab Tutorial and Exercises for COMP61021

Introduction to MATLAB

MATLAB Project: Getting Started with MATLAB

Nuts & Bolts guide to MATLAB

A Very Brief Introduction to Matlab

ARRAY VARIABLES (ROW VECTORS)

1 Introduction to Matlab

Transcription:

Short Introduction into MATLAB Christian Schulz christian.schulz@cma.uio.no CMA/IFI - UiO

1. Basics Startup Variables Standard Operations 2. Next Steps Plots Built in Functions Write/Plot to File 3. Programming Scripts and Functions Flow Control Built in Functionality vs. Programming 4. Now What? 5. Comments/Conventions Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 2/23

Starting MATLAB MATLAB is installed at computers of Dep. of Informatics as well as Dep. of Mathematics Just type in > matlab & Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 3/23

Definition of Variables Numbers: >> alpha = 5 alpha = 5 Row-Vectors/Arrays: >> b = [1 2 3] % or b = [1,2,3] b = 1 2 3 Matrices: >> A = [1 1 1; 2 2.5 3; 4 5 6] A = 1.0000 1.0000 1.0000 2.0000 2.5000 3.0000 4.0000 5.0000 6.0000 Matrices are basic data elements in MATLAB (MATrix LABoratory) Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 4/23

Variables cont. Specifying variable with ; at end omits output: >> C = [1 3 5; 2 4 6; -1-2 -3]; >> Command whos prints information about all defined variables: >> whos Name Size Bytes Class A 3x3 72 double array C 3x3 72 double array alpha 1x1 8 double array b 1x3 24 double array Grand total is 22 elements using 176 bytes Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 5/23

Variables in Workspace Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 6/23

Arithmetic Operations Standard Matrix Operations are done by just typing them in: >> ApC = A+C; AtC = A*C; AexpAlpha = A^alpha; >> d = A*(b ); Conjugate complex transpose is given by A If you need just transpose, use A. Element wise operations are denoted by an additional. : >> At elemc = A.*C; Aelem expalpha = A.^alpha; Solving linear systems in form of A*x = d can be solved using left division: >> x = A\d; Also possible to solve y*a = b : >> y = b/a; Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 7/23

Accessing Elements/Submatrices Indicies of matrices/vectors/arrays start with 1 Element of matrix: >> A(1,2); Create a sequence: >> 2:.5:4 ans = 2.0000 2.5000 3.0000 3.5000 4.0000 Variable ans contains value of last operation with unstored result Column i in matrix: First two rows: >> A(:,i); >> A(1:2,:); Can also use array with indicies: >> A([1 3 2],:); Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 8/23

Plots Command plot(x,y) plots vector y against x title( plot-title ); sets title Can label axis with xlabel( x-axis-label ); Plot several things into one figure: hold on; Delete old plot when plotting new: hold off; plottools on; gives you a editing tool for a figure More plot commands (e.g. surfl or polar) >> surfl(a); >> title( A ); >> xlabel( column-index ); >> ylabel( row-index ); >> zlabel( value ); >> plottools on; Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 9/23

Built in Functions MATLAB has a lot of built in functions A function can return more than one value: >> [m,n] = size(a); Many functions normally applied to scalar values (e.g. sin, cos) can also be applied to matrices (are applied element wise) To get more information about what functions are built in and syntax/usage of specific functions use MATLAB built in help or/and documentation Help: >> help command Start Documentation: >> doc Documentation for a command: >> doc command >> lookfor text searches text in (first line of) help of functions Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 10/23

Store/Load Data File Input/Output save filename; stores all variables in workspace in binary file filename.mat save filename x y z; saves x y z into filename.mat load filename; correspondingly loads variables stored in filename.mat into workspace clear A; removes variable A from workspace clear; removes all variables Plots Use print to plot into a file Create figure and store returned handle: >> figid = figure ( figure(figid2); makes figid2 current figure ) Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 11/23

File Input/Output cont. Plots cont. Do Plots (will be done in current figure) Print: >> print (figid, -dps, figure.ps ) Formated Output fid = fopen(filename, permissions); opens file filename with corresponding permissions fprintf(fid, format, a,b,..); writes formated output into file associated with fid (behaves as fprintf in C) fclose(fid); closes corresponding file Be aware how matrices/vectors/arrays and fprintf interact when using it (see >> help fprintf )! Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 12/23

General Scripts and Functions For bigger tasks one wants to reuse code MATLAB program-files have extension.m Two possibilities: script-files and function-files Functions/scripts are called/executed by typing in filename (without.m ) Script-Files Contain a series of MATLAB commands Commands are executed as if they were typed in All variables are global interact with workspace Problem: must have matching variable-names / name overlapping Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 13/23

Structure General Function-Files Begin with declaration of function function [out1, out2,...] = funname(in1, in2,...) in1, in2,...: input parameter out1, out2,...: output parameter Followed by some rows of comments (comment starts after % ) which are displayed when entering >> help funname Body of function follows After Body subfunctions may follow Used variables are local Subfunctions are not visible outside can structure code Name file as funname.m Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 14/23

Flow Control Like programming languages MATLAB has branching and loops Branching Mostly branching is done using if: if condition1 program1 elseif condition2 program2 else program3 end A condition is as normal something like (count < limit) & ((height - offset) >= 0) Check operators for possible conditions/concatenations Be careful when using matrices in conditions (A ~= B) MATLAB also supports switch Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 15/23

Loops Flow Control cont. For-loop: Executes block of code specified number of times for variable = expression statements end While-loop: Repeatedly executes statements while condition is true while condition statements end Example: for i = [1,2,4,5]; A(i) = i; end More Flow Control for loops like break, continue, etc Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 16/23

Built in Functionality vs. Programming Built in functions are optimized Use built in functions / matrix operations whenever possible (instead of loops,...) Example function [R,success] = maketriangular(a) % uses row operations to make A upper triangular % use: maketriangular(a) % in: A n x n Matrix % out: R n x n upper triangular Matrix (if successful) % success 0 (false) if failed, 1 (true) if successful success = 0; [m, n] = size(a); if (m ~= n) return; % returns from function end Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 17/23

Built in Functionality vs. Programming Example cont. - Loops for k = 1:n-1 % row operations for ii row = k+1:n factor = A(ii row,k) / A(k,k); A(ii row,k) = 0; for ii column = k+1:n A(ii row,ii column) = A(ii row,ii column) -... factor * A(k,ii column); end end end success = 1; R = A; Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 18/23

Built in Functionality vs. Programming Example - Matrix Operations Replace for-loops by matrix operations for k = 1:n-1 % row operations factor(k+1:n,k) = A(k+1:n,k)/A(k,k); A(k+1:n,k) = 0; A(k+1:n,k+1:n) = A(k+1:n,k+1:n) -... factor(k+1:n,k)*a(k,k+1:n); end But (Of Course) If an exercise says you should implement an algorithm doing xyz, you should not just call the corresponding MATLAB function xyz Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 19/23

How To Continue Make intense use of MATLAB help and doc commands You can use the MATLAB editor for programming, just type >> edit For further information about MATLAB google for it Some tutorials I have found: http://www.cyclismo.org/tutorial/matlab/ http://web.ew.usna.edu/ mecheng/ DESIGN/CAD/MATLAB/usna.html http://www.math.ufl.edu/help/matlab-tutorial/ http://www.nd.edu/ hlin1/ee550/page4.html Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 20/23

Comments, Conventions Structure your code, reuse code Comment your code! Describe what, how, why; do not repeat code When defining a function, write the comments for that function, including description of the input, output parameter and what the function does Use indentation (MATLAB editor does this) Use appropriate naming, e.g relativesize or relsize and abssize instead of size1, size2 or even s1, s2 Use names starting with capital letter for matrices, with small letter for vectors, scalars Do not redefine MATLAB commands or predefined variables Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 21/23

Compulsory Exercises One report per student Report written in English Write in TeX or LaTeX, not Word All source files have to be handed in Use names given in assignment for functions, variables, files. Do not change upper/lower case. (Scripts are used to test your code) Function names and corresponding.m filenames must be identical Send packed solutions to me Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 22/23

Thank you for your attention. Have fun with the exercises. Christian Schulz (CMA/IFI - UiO) Short Introduction into MATLAB 23/23