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

Similar documents
MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

EGR 111 Introduction to MATLAB

ARRAY VARIABLES (ROW VECTORS)

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

Grace days can not be used for this assignment

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

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

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

University of Alberta

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

An Introduction to Numerical Methods

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Dr Richard Greenaway

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

MATLAB QUICK START TUTORIAL

MATLAB TUTORIAL WORKSHEET

1 Introduction to Matlab

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

How to learn MATLAB? Some predefined variables

Class #15: Experiment Introduction to Matlab

A very brief Matlab introduction

MATLAB SUMMARY FOR MATH2070/2970

A Guide to Using Some Basic MATLAB Functions

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Stokes Modelling Workshop

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

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

Introduction to MATLAB Practical 1

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016

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

Introduction to MATLAB

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

Introduction to Matlab

MATLAB Project: Getting Started with MATLAB

PART 1 PROGRAMMING WITH MATHLAB

Introduction to MATLAB

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

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:

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

MATLAB Tutorial III Variables, Files, Advanced Plotting

Introduction to Matlab

Mechanical Engineering Department Second Year (2015)

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

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

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Laboratory 1 Octave Tutorial

Numerical Methods Lecture 1

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

2.0 MATLAB Fundamentals

Lab 1 Intro to MATLAB and FreeMat

Math 2250 MATLAB TUTORIAL Fall 2005

Here is a quick introduction to Matlab and a couple of its symbolic and control functions.

Introduction to Matlab

ENGR 1181 MATLAB 02: Array Creation

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

Computational Modelling 102 (Scientific Programming) Tutorials

AMS 27L LAB #2 Winter 2009

An Introduction to MATLAB II

MATLAB Project: Getting Started with MATLAB

MATLAB Guide to Fibonacci Numbers

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

Getting Started with MATLAB

Matlab 1: Get Started

Introduction to MATLAB

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

Lab #1 Revision to MATLAB

Mathworks (company that releases Matlab ) documentation website is:

Matlab Introduction. Scalar Variables and Arithmetic Operators

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

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

AN INTRODUCTION TO MATLAB

QUICK INTRODUCTION TO MATLAB PART I

Computer Project: Getting Started with 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.

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

MATLAB Lecture 1. Introduction to MATLAB

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

Introduction to Matlab

A quick Matlab tutorial

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

Finding, Starting and Using Matlab

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Lab of COMP 406 Introduction of Matlab (II) Graphics and Visualization

Programming in Mathematics. Mili I. Shah

A QUICK INTRODUCTION TO MATLAB

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

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

MATLAB GUIDE UMD PHYS401 SPRING 2012

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

Matlab Tutorial and Exercises for COMP61021

Introduction to MATLAB

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

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

Introduction to MATLAB Programming

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

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

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Lecture 2: Variables, Vectors and Matrices in MATLAB

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

Transcription:

1 Matlab Tutorial 1- What is Matlab? Matlab is a powerful tool for almost any kind of mathematical application. It enables one to develop programs with a high degree of functionality. The user can write programs, scripts or interface with the program directly calling various math functions. Matlab is more than a glorified calculator. It has powerful plotting capabilities that allow one to plot 2D and 3D graphs, contour plots, vector plots, slices etc. 2- Basic Matlab Matlab interfaces to the user through a scripting window. In essence, one types commands into the Command Window. The Matlab engine then interprets these commands and executes the appropriate function(s). For example, entering: >> var1 = 2 var1 = 2 >> var2 = var1 + var1^2 var2 = 6 In this case in the first line the user asks Matlab to create a new variable var1 and assign it a value of 2. Next, another variable is created var2 and an expression involving var1 is evaluated and its value is assigned to the newly created variable. The value assigned to a variable can be checked by simply typing in the variable name: >> var2 var2 = 6 Let s look at something a bit more useful. One of Matlab s strong points is its ability to deal with matrices of any dimension. Almost all fields of science use arrays. They are

2 very convenient and Matlab simplifies the handling of matrices greatly. For example, let s create three simple arrays: >> A = [ 1 5; 3 6] A = 1 5 3 6 >> B = [3 5.5 1.1; 4 1.2 1.33333] B = 3.0000 5.5000 1.1000 4.0000 1.2000 1.3333 >> X = [0 2; 4-1] X = 0 2 4-1 Three variables are created (A, B and X) which are arrays. Arrays can be created by the use of square brackets and semi-colons. The opening square bracket tells Matlab that one is defining an array. A space between numbers means that they are in adjacent columns, while a semi-colon means the end of a row. One can check the size of an array: >> size(b) ans = 2 3 This means that array B is an array with 2 rows and 3 columns. One can calculate the product of two matrices: >> C = A*B C = 23.0000 11.5000 7.7667 33.0000 23.7000 11.3000

3 In this case a variable C was created that was the product of the matrices A and B. Of course, if the inner dimensions of the two matrices don t agree then Matlab complains, for example: >> C = B*A??? Error using ==> * Inner matrix dimensions must agree. What if one wants to multiply each element in an array with the corresponding elements in another array: >> Y = A.*X Y = 0 10 12-6 The. in front of the operator tells Matlab to apply the operator * between corresponding elements. This holds for other operators. The transpose of a matrix is easily calculated: >> T = A' T = 1 3 5 6 As is the inverse: >> P = inv(t) P = -0.6667 0.3333 0.5556-0.1111 To access an element in an array, all one needs to do is designate the column and row index: >> A(1,2) ans = 5 To access several elements one uses the : operator. For example to extract the first row in array X and assign it to a new variable D one does: >> D = X(1,:)

4 D = 0 2 In this case all the columns in array X are called using the : operator. One can also access a set of indices, for example: >> D = C(1,1:2) D = 23.0000 11.5000 In this example only columns 1 and 2 of row 1 are retrieved from array C. The : operator is also useful in defining arrays. For example, to obtain a vector that starts at 0 and increases in 0.5 steps to a value of 5: >> t = [0:0.5:5] t = Columns 1 through 10 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 Column 11 5.0000 The first value specifies the start value, the second is the increment and the third is the maximum value. By not entering an interval, Matlab assumes it to be equal to 1. One can reference the last element in and index by using the reserved word end, for example, to access elements 3 and on in vector t we use: t(3:end) This and many other basic features are well documented. To get started, check out the following website: http://math.ucsd.edu/~driver/21d-s99/matlab-primer.html Also, Matlab itself has a lot of documentation. Help on a specific function can be accessed by typing help and then the name of the function. Also by typing helpwin a dialog window pops up with a list of all the functions available. By typing helpdesk, Matlab will access the Mathworks website via the Netscape browser.

5 2.1 Plotting in Matlab Plotting in Matlab is extremely easy and at the same time offers a great deal of control to the user. One can alter any aspect of the plot by accessing its properties. In this section, the basic Matlab plotting tools are presented. Let s plot a sine wave. First, the values that we wish to plot must be generated. First the x-values must be created. >> x = [0:0.1:2*pi]; Here we are defining a vector with values that go from 0 to 2π (one period) with a step of 0.1. Note that arithmetic operations are allowed in the definition of arrays as long as they result in real scalars. Now let s calculate the value of the sine wave at each value of x: >> y = sin(x); We passed a vector to the sin function. This is possible with most Matlab functions, simplifying our task. Without this capability, one would have to loop through the vector as in regular programming languages. Now let s plot the function. For basic 2D graphs, the function plot is used: >> plot(x,y) The plot functions plots x vs. y. x and y must be vectors of the same length. The result is:

6 Let s label the graph: >> xlabel('x'); >> ylabel('y = sin(x)'); >> title('plot of sine wave'); If we again call the function plot, the old plot is destroyed, along with labels, title etc. and a new plot is created. To create a new figure one uses the command figure. At this point the new figure is now the focus and all plot command are directed towards it. One might want to have several plots on one pair of axes. For example, close all the figures and type: >> v = cos(x); >> plot(x,y); >> hold on; >> plot(x,v,'r'); In this instance, a sine wave is plotted as before and then Matlab is told to hold the figure. Then a cosine wave is plotted in red. This is accomplished by the third argument in the

7 plot command. This third command (if used) must be a string. It specifies the colour and/or the line type and/or the symbols to be used. The result is shown. See the documentation for the different available options. 3- Two Dimensional Data In Computational Thermofluids, post processing data properly is crucial. The first step is of course to set up a two dimensional field on which to process the data. First we need a mesh. We will be using the function meshgrid. >> [X Y] = meshgrid(-5:5:5,-5:5:5) X = -5 0 5-5 0 5-5 0 5 Y = -5-5 -5 0 0 0 5 5 5 Here we have created a simple 3x3 mesh. The array X holds the x-values for each node point while the array Y holds the y-values. We want our domain to stretch from x=-5 to x=5 in steps of 0.1 and from y=-5 to y=5 also in steps of 0.1: [X Y] = meshgrid(-5:0.1:5,-5:0.1:5); This returns two arrays (X and Y). Each one is 101 by 101 and contains the x and y coordinates of each node respectively. We can now use this domain to calcluate temperatures and fluxes etc, as in class and the first assignment.