A very brief Matlab introduction

Similar documents
A Guide to Using Some Basic MATLAB Functions

1 Introduction to Matlab

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Chapter 1 Introduction to MATLAB

A General Introduction to Matlab

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

Stokes Modelling Workshop

MATLAB Functions and Graphics

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

Introduction to MATLAB

Math 2250 MATLAB TUTORIAL Fall 2005

Lecturer: Keyvan Dehmamy

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

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

Introduction to GNU-Octave

Introduction to Engineering gii

LAB 1 General MATLAB Information 1

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

A QUICK INTRODUCTION TO MATLAB

Introduction to Matlab

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

MATLAB Workshop Dr. M. T. Mustafa Department of Mathematical Sciences. Introductory remarks

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

Dr Richard Greenaway

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

LAB 1: Introduction to MATLAB Summer 2011

Matlab Introduction. Scalar Variables and Arithmetic Operators

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

General MATLAB Information 1

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

Laboratory 1 Octave Tutorial

Chapter 1 MATLAB Preliminaries

How to learn MATLAB? Some predefined variables

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

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

Programming in Mathematics. Mili I. Shah

Some elements for Matlab programming

Finding, Starting and Using Matlab

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Introduction to MATLAB

Introduction to MATLAB Practical 1

Quick MATLAB Syntax Guide

Introduction to Matlab

Lab 1 Intro to MATLAB and FreeMat

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

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

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

Chapter 2. MATLAB Fundamentals

Ebooks Chemical Engineering

Introduction to Matlab

AN INTRODUCTION TO MATLAB

A. Matrix-wise and element-wise operations

Introduction to MATLAB

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

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

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

An Introduction to MATLAB II

Introduction to Matlab

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.

MATLAB QUICK START TUTORIAL

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

Matlab Tutorial and Exercises for COMP61021

Introduction to Matlab

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

CSE/Math 485 Matlab Tutorial and Demo

Introduction to MATLAB

ELEMENTARY MATLAB PROGRAMMING

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

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

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

EGR 111 Introduction to MATLAB

University of Alberta

Arithmetic and Logic Blocks

ARRAY VARIABLES (ROW VECTORS)

Entering the numbers in these two ways creates the same matrix A.

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Matlab Workshop I. Niloufer Mackey and Lixin Shen

Mechanical Engineering Department Second Year (2015)

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

PART 1 PROGRAMMING WITH MATHLAB

Matlab Tutorial 1: Working with variables, arrays, and plotting

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

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

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

QUICK INTRODUCTION TO MATLAB PART I

Dr Richard Greenaway

Basic MATLAB Intro III

Script started on Thu 25 Aug :00:40 PM CDT

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

Numerical Methods Lecture 1

EE 301 Signals & Systems I MATLAB Tutorial with Questions

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to Matlab

Transcription:

A very brief Matlab introduction Siniša Krajnović January 24, 2006 This is a very brief introduction to Matlab and its purpose is only to introduce students of the CFD course into Matlab. After reading this short note and truing the presented examples one should be able to write short programs that are required in the CFD course. Matlab itself provides a comprehensive help that can be reached by typing help in the command window or by using the Matlab Help window. Vectors and Matrices Every variable in Matlab is a matrix. Here we show how to assign numbers, vectors and matrices to variables. The Matlab prompt is >> and the commands below are typed in after the prompt and concluded with a carriage return. The response of Matlab appears on the next line. >> a=10 a = 10 >> v=[1;7;9] v = 1 7 9 >> B=[1,2,3;4,5,6;7,8,9] B = 1 2 3 4 5 6 7 8 9 As we can see from these examples, the rows of a matrix are separated by semicolons, while the entries on any given row are separated by commas (Note that spaces can be used instead of commas). Each of these three variables a, v and B is regarded as a matrix. The scalar a is a 1 x 1 matrix, the vector v is a 3 x 1 matrix and B is a 3 x 3 matrix.

Indices of variables must be positive integers. Thus, x(0), x(-3), x(1.2) or B(0,1) are not allowed. The size of a matrix can be found using the function size(b), which returns the pair of numbers m, n when B is an m x n matrix. >> size(b) 3 3 >> size(v) 3 1 The length of the vector v is found by using the function length(v). >> length(v) 3 If we want to prevent Matlab from displaying the entered matrix immediately afterward, we need to end the line with a semicolon >> v=[1;7;9]; >> Although Matlab says nothing when you ended the line with a semicolon, it has remembered your definition of v. The square root function sqrt is a built-in feature >> sqrt(27)

5.1962 The variable ans contains the result of the most recent computation which can then be used as an ordinary variable in subsequent computations: >> ans*10 51.9615 In this example the result of sqrt(27) from previous computation that was stored in ans was multiplied with 10 and the result of this multiplication is stored in ans overwriting the old value of ans. Another built-in variable is pi: >> pi 3.1416 The predefined functions in Matlab (besides the square root) include: abs angle real,imag conj exp round fix sign rem sin, cos, tan asin, acos, atan absolute value phase angle of a complex number real part, imaginary part of complex number complex conjugation exponential function round to the nearest integer round towards zero signum function remainder trigonometric functions inverse trigonometric functions >> sin(1)^3*tan(1) 0.9279 >> exp(1)

2.7183 >> log(ans) 1 Matlabs online help includes a list of built-in special functions and routines, as well as a list of other commands on which help is available. To obtain the list type help. For help on a particular command, type help followed by the command. You can also get help by using the Matlab Help window, which is accessed by going to the Help menu at the top of the screen. >> help sin SIN Sine of argument in radians. SIN(X) is the sine of the elements of X. See also ASIN, SIND. You can also type help help to find out how to find help. For demonstrations of Matlab features type demo in the command window. Operations on matrices For matrices A and B, Matlab can compute the sum, difference and the product of these two matrices. To do this, type A+B, A-B, and A*B, respectively. Note! The order is important in matrix multiplication: >> A=[1,2;3,4] A = 1 2 3 4 >> B=[1,0;1,0] B =

1 0 1 0 >> A*B 3 0 7 0 >> B*A 1 2 1 2 Typing A^2, for a square (n x n) matrix A, yields the matrix product A*A. Applying to a matrix A any of the built-in functions returns a matrix of the same dimensions containing the values of the functions as if it had been applied componentwise: >> A=[pi,0;pi/2,3*pi/2] A = 3.1416 0 1.5708 4.7124 >> cos(a) -1.0000 1.0000 0.0000-0.0000 When we want Matlab to multiply two matrices A and B that have the same dimension in a comprehensive fashion (rather than the matrix multiplication), we use the operator.* rather than *. Similarly, componentwise division of two matrices of the same dimension can be accomplished by writing A./B which creates a matrix whose entries are A(i,j)/B(i,j).

>> A=[1,2;3,4] A = 1 2 3 4 >> B=[2,4;6,8] B = 2 4 6 8 >> A*B 14 20 30 44 >> A.*B 2 8 18 32 There are several commands in Matlab which helps you to easy input certain standard types of matrices. The built-in function zeros(m,n) returns an m x n matrix of zeros. ones (m,n) returns an m x n matrix of ones. When the matrix contains only one row, we have a row vector instead of a matrix, and a Matlab provides a special ways to input certain types of row vectors. In Matlab, the notation start:step:stop denotes a row vector whose first entry is start and whose consecutive entries differ by step, and whose last entry does not excide stop. >> a=2:2:8 a = 2 4 6 8 The parameters start, step and stop do not have to be integers:

>> v=-5.1:0.2:-4 v = -5.1000-4.9000-4.7000-4.5000-4.3000-4.1000 If the parameter step is omitted, Matlab assumes it equal to one >> x=3:6 x = 3 4 5 6 Graphics The ability to do plotting in Matlab is very useful. Here we shall describe how to plot 2D graphs, 3D surfaces, 2D contours of 3D surfaces and vector arrows. To plot a number of ordered pairs (x,y) connected by straight lines, we built row vectors x and y containing the x and y values and ask Matlab to make a plot. >> x=1:5; >> y=0:0.2:.8; >> plot(x,y) Vectors x and y contain two different coordinates of the same set of points in the plane and thus must have the same length. Matlab functions are vectorized and constructing the needed vectors to graph a build in function is done by first constructing a vector of the desired x values, and than the corresponding y values come from applying the buil-in function to the x vector: >> x=0:0.1:pi; >> y=sin(x); >> plot(x,y) Some useful commands in making plots are

xlabel( x axis label ), ylabel( y axis label ), title( title) labels the horizontal and the vertical axes and the title, respectively, in the current plot. axis([a b c d]) grid hold on hold off subplot legend changes the viewing window of he current graph adds a rectangular grid to a current plot freezes the current plot so that the subsequent plots will be displayed on the same axes releases the current plot puts multiple plots in one graphics window creates a small box inside the current plotting window that identifies multiple plots in the same window Three-dimensional plots Making a 3D plot in Matlab is often done in two steps. First we define a mesh in an area where we want to make a 3D plot. This area is defined by two vectors x and y, of the length n and m, which represent x- and y-values in the mesh. Now we make a mesh with a command [X,Y]=meshgrid(x,y). The matrix X contains the vector x copied in m rows, and the matrix Y contains the vector y copied in n columns. The second step is to compute the value of a function using X and Y matrices. To plot a surfaces of a function in Matlab we can use following commands mesh(z), surf(x,y,z), surfc(x,y,z) >> [X, Y]=meshgrid(x,y); >> R=sqrt(X.^2+Y.^2)+eps;Z=sin(R)./R; >> surfc(x,y,z)

If we want to plot only contours of function Z above we can use command contour(x,y,z,n) where n is the number of contour lines-2 we want to display. Vector arrows can be plotted with a command quiver(x,y,dzdx,dzdy,n) where n controls the size of the arrows. [dzdx,dzdy]=gradient(z); quiver(x,y,dzdx,dzdy,3) Assigning the values in the matrices If we want to put the value of the third column in a matrix A equal to 5 and we know that matrix A has size n x m, than we can write A(:,3)=5*ones(n,1) If we want to make a matrix B from several existing vectors a, x and d where all three vectors are of the same length we can write B=[a x d]. A simple FOR loop

for i=2:n_i-1 for j=2:n_j-1 end Expression such as k_e(i,j)= end A simple WHILE loop while (res>max_e) end Printing out the figure in some JPG file For printing out figures we can use command print djpg filename Measuring the time of the calculation If we want to measure the time a calculation requires we can use commands tic and toc If we want to find out the time that our while loop requires we can write tic while (res>max_e) end toc Plotting several plots If we want to plot several plots in separate windows we can use command figure(n) where n is the number of the figure. figure(1) Cleaning the memory of the Matlab If we start writing a new program and want to be sure that our variables don t have some old values we can write command clear all at the beginning of the program.