Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following.

Similar documents
MATLAB TUTORIAL WORKSHEET

Dr Richard Greenaway

EE 301 Signals & Systems I MATLAB Tutorial with Questions

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

Introduction to MATLAB

The Mathematics of Big Data

A quick Matlab tutorial

Introduction to MATLAB

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

ECE 3793 Matlab Project 1

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

Finding MATLAB on CAEDM Computers

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

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

Chapter 1 Introduction to MATLAB

Desktop Command window

An Introduction to MATLAB

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

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

HW 4 HINTS. 1. Chapter 6

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

EE168 Handout #6 Winter Useful MATLAB Tips

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

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

Lecturer: Keyvan Dehmamy

Unix Computer To open MATLAB on a Unix computer, click on K-Menu >> Caedm Local Apps >> MATLAB.

MATLAB Project: Getting Started with MATLAB

MATLAB Tutorial III Variables, Files, Advanced Plotting

MATLAB GUIDE UMD PHYS401 SPRING 2012

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:

Introduction to Matlab. Matlab variable types. Matlab variable types. Matlab variable types. Notes. Eugeniy E. Mikhailov. Lecture 02. Notes.

ENGR 1181 MATLAB 05: Input and Output

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

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Math 144 Activity #4 Connecting the unit circle to the graphs of the trig functions

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.

Physics 326G Winter Class 6

MAT 343 Laboratory 4 Plotting and computer animation in MATLAB

Introduction to Matlab

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Programming 1. Script files. help cd Example:

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

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

Practice Reading for Loops

LECTURE 1. What Is Matlab? Matlab Windows. Help

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

A Guide to Using Some Basic MATLAB Functions

Introduction to MATLAB LAB 1

Introduction to MATLAB

EGR 111 Functions and Relational Operators

Grace days can not be used for this assignment

APPM 2460 Matlab Basics

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

1 Introduction to Matlab

A very brief Matlab introduction

MATLAB BASICS. M Files. Objectives

MATLAB GUIDE UMD PHYS401 SPRING 2011

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

Some elements for Matlab programming

ARRAY VARIABLES (ROW VECTORS)

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

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command...

Matlab Introduction. Scalar Variables and Arithmetic Operators

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

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

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

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:

How to learn MATLAB? Some predefined variables

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

EP375 Computational Physics

Class #15: Experiment Introduction to Matlab

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

Introduction to Matlab. By: Hossein Hamooni Fall 2014

AMS 27L LAB #2 Winter 2009

To see what directory your work is stored in, and the directory in which Matlab will look for files, type

AN INTRODUCTION TO MATLAB

Introduction to MATLAB

APPM 2460 PLOTTING IN MATLAB

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

EGR 111 Functions and Relational Operators

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

Introduction to Matlab

McTutorial: A MATLAB Tutorial

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

Introduction to MATLAB

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Getting Started with MATLAB

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

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

MATLAB GUIDE UMD PHYS375 FALL 2010

Mathworks (company that releases Matlab ) documentation website is:

Homework 1 Description CmpE 362 Spring Instructor : Fatih Alagoz Teaching Assistant : Yekta Said Can Due: 3 March, 23:59, sharp

MATLAB Project: Getting Started with MATLAB

EGR 111 Introduction to MATLAB

EOSC 352 MATLAB Review

Introduction to Matlab

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

Transcription:

A Matlab Primer Here is how the Matlab workspace looks on my laptop, which is running Windows Vista. Note the presence of the Command Window in the center of the display. You ll want to create a folder in which you ll place files you create; mine is C:\physics_326_matlab. Notice the presence of a couple of script files in the current folder pane to the left: basics.m and MakingAScriptFile.m. Keep in mind that Matlab is case sensitive: pi is not the same thing as Pi. See the file basics.m, which contains much of the following. Matlab processes scripts (as well as things you say directly to Matlab by typing something at the ">>" prompt) by reading what it is told, then deciding what answer to return. It is not a compiled language: the software does not generate an executable program which can be run outside the Matlab environment. For example, try typing this to see what happens (don't type the >> prompt, of course): >> a = 5 If you had put a semicolon after the 5, Matlab wouldn't have reported an answer after evaluating the line, e.g. >> a = 5; Matlab remembers the value of a: try typing >> a copyright 2013 George Gollin 1

and it'll tell you that a is 5. You can continue a line onto the next by using an ellipsis (three successive periods). For example, to define a 3 x 6 matrix you could tell Matlab this: >> a = [10 20 30 50 60; 11 21 31 51 61; 12 22 32 52 62]; though your code would be easier to read when written like this: >> a = [10 20 30 50 60; 11 21 31 51 61; 12 22 32 52 62]; The above matrix is displayed by Matlab this way: >> a a = 10 20 30 50 60 11 21 31 51 61 12 22 32 52 62 To access the 3rd row, 2nd column of this matrix, refer to it as, for example, a(3,2): >> a(3,2) 22 Here are some basic operations you'll use in writing matlab code. 1. incrementing the value a variable: >> k = 5; >> k = k + 1 k = 6 2. creating an equally-spaced sequence of numbers from 1 to 4, with each being 0.7 larger than the preceding: >> 1 : 0.7 : 4 1.0000 1.7000 2.4000 3.1000 3.8000 You can create a vector that is 1 row by 5 columns this way: >> b = 1 : 0.7 : 4 b = 1.0000 1.7000 2.4000 3.1000 3.8000 3. Asking for the dimensions of an object: >> size(b) 1 5 4. Taking the transpose of a matrix: use an apostrophe (a single quote) to take the transpose. copyright 2013 George Gollin 2

>> c = b' c = 1.0000 1.7000 2.4000 3.1000 3.8000 5. Multiplying matrices: >> b*c 33.7000 >> c*b 1.0000 1.7000 2.4000 3.1000 3.8000 1.7000 2.8900 4.0800 5.2700 6.4600 2.4000 4.0800 5.7600 7.4400 9.1200 3.1000 5.2700 7.4400 9.6100 11.7800 3.8000 6.4600 9.1200 11.7800 14.4400 6. Displaying a text string in the command window: note the use of single quotes to delimit a text string. >> disp('and that is that!') Use two successive apostrophes if you'd like to insert an apostrophe into a text string: >> disp('and that''s that!') No semicolon is required after the disp() command since it doesn t return a value. See the file MakingAScriptFile.m, which contains much of the following. To create a new script ("m") file, go to the File New Script menu. An untitled script file will open in the editor. Here are some of the things you might want to do in a script file. copyright 2013 George Gollin 3

1. Conditional statements. The format in the following should be clear to you, I think. a = 5; b = 6; if a > b disp('a is greater than b') if a >= b disp('a is greater than or equal to b') if a < b disp('a is less than b') if a <= b disp('a is less than or equal to b') % note the use of a double equal sign here: if a == b disp('a equals b') We can construct more complicated conditions using the logical operators && ("and"), ("or"), and ~ ("not"). Note that "true" is 1 and "false" is 0. For example, if (a < b) (a > b) disp('a doesn''t equal b') if ~(a == b) disp('a doesn''t equal b') if a == 1 disp('a is 1') else disp('a is not 1') if a == 1 disp('a is 1') elseif a == 2 disp('a is 2') elseif a == 3 disp('a is 3') else disp('i give up.') copyright 2013 George Gollin 4

2. Loops. There are lots of ways to execute loops. Here s an example. I m going to do something clever inside the loop to encode three numerical values into a string which I ll explain shortly. % I will step in angle by 0.5 radians. Note that Matlab knows about pi. dtheta = 0.5; for theta = 0 : dtheta : 2*pi SinTheta = sin(theta); CosTheta = cos(theta); message = sprintf('th = %f, sin(th) = %f, and cos(th) = %f',... theta, SinTheta, CosTheta); disp(message) % I would have used sind(theta) and cosd(theta) if theta had % been in degrees instead of radians. The sprintf function creates a text string, inserting formatted representations of quantities as desired. The three %f that appear in the string are format specifiers representing a floating point number such as 3.14159. (You would use a %d for an integer when you don t want there to be a decimal point in the text representation of the number.) Use the functions sind and cosd when the argument is in degrees. You can use break to jump out of a loop early: for j = 1:20 message = sprintf('j is %d', j); disp(message) if j > 5 disp('time to leave this loop') break 3. Drawing graphs. One of many ways to plot a curve is to give Matlab a list of x and y values. Here s an example using a loop. % create a vector of angles, and reserve space for a y array. theta = 0 : 0.01 : 2*pi; y1 = zeros(1,length(theta)); % fill the vector holding the sines of the angles using a loop for index = 1 : length(theta) y1(index) = sin(theta(index)); copyright 2013 George Gollin 5

% draw the graph plot(theta, y1); I could have done this more compactly, without a loop, by taking advantage of Matlab s all-at-once matrix manipulation features: or even just theta = 0 : 0.01 : 2*pi; y2 = sin(theta); plot(theta, y2); plot(0 : 0.01 : 2*pi, sin(0 : 0.01 : 2*pi)); Here is a loop which plots a Limaçon of Pascal, r( θ) = b+ acos ( θ), for a = 2 and b = 1. % set the values of a and b: a = 2; b = 1; % generate a list of angles theta: theta = 0 : 0.01 : 2*pi; % ask how many elements are in theta: thetalength = length(theta); % create arrays of x and y points, initialized to zero. The function % zeros(n,m) creates an n row by m column object. We want 1 row, % lots of columns. x = zeros(1, thetalength); y = zeros(1, thetalength); % Now loop over angles to generate r, then x and y. for index = 1 : thetalength r = b + a * cos(theta(index)); x(index) = r * cos(theta(index)); y(index) = r * sin(theta(index)); % Now open a new figure window, plot the figure, then add a title. figure; plot(x,y); title ('Limaçon of Pascal'); % now set the axes to be drawn with equal lengths per unit of distance. axis equal I could have done this much more compactly. The tricky thing in the following is the use of the.* operator to take a dot product (a term-by-term multiplication) of two vectors. Here it is: copyright 2013 George Gollin 6

plot( (b + a*cos(0 : 0.01 : 2*pi)).* cos(0 : 0.01 : 2*pi),... (b + a*cos(0 : 0.01 : 2*pi)).* sin(0 : 0.01 : 2*pi) ); axis equal title ('Limaçon of Pascal'); See the file DebuggingCode.m, which is listed below. copyright 2013 George Gollin 7