Grace days can not be used for this assignment

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

How to learn MATLAB? Some predefined variables

EE 301 Signals & Systems I MATLAB Tutorial with Questions

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

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:

Mathworks (company that releases Matlab ) documentation website is:

Introduction to MATLAB

Desktop Command window

What is MATLAB and howtostart it up?

QUICK INTRODUCTION TO MATLAB PART I

Signals and Systems Profs. Byron Yu and Pulkit Grover Fall Homework 1

Getting Started with MATLAB

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

Introduction to MATLAB

MATLAB Project: Getting Started with MATLAB

Introduction to MATLAB

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

AMS 27L LAB #2 Winter 2009

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

EGR 111 Introduction to MATLAB

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

MATLAB Introductory Course Computer Exercise Session

Introduction to MATLAB LAB 1

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

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

The value of f(t) at t = 0 is the first element of the vector and is obtained by

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

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

A QUICK INTRODUCTION TO MATLAB

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.

Introduction to MATLAB

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

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Introduction to MATLAB

Laboratory 1 Octave Tutorial

Introduction to Matlab

MATLAB Project: Getting Started with MATLAB

Computer Project: Getting Started with MATLAB

PART 1 PROGRAMMING WITH MATHLAB

CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

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

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

LAB 1: Introduction to MATLAB Summer 2011

Lab P-1: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

A Guide to Using Some Basic MATLAB Functions

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

2.0 MATLAB Fundamentals

Short Version of Matlab Manual

1 Introduction to Matlab

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

MATLAB GUIDE UMD PHYS401 SPRING 2012

A very brief Matlab introduction

Introduction to MATLAB

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

A Brief Introduction to MATLAB

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

MATLAB Lecture 1. Introduction to MATLAB

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

Introduction to MATLAB

Introduction to Matlab

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

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

1 Introduction to MATLAB

MATLAB TUTORIAL WORKSHEET

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

ECE Lesson Plan - Class 1 Fall, 2001

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

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

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

Matlab Tutorial: Basics

Stokes Modelling Workshop

Introduction to Matlab

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

Matlab Introduction. Scalar Variables and Arithmetic Operators

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

MATLAB/Octave Tutorial

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

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

Intro to Matlab for GEOL 1520: Ocean Circulation and Climate or, Notions for the Motions of the Oceans

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

Lab. Manual. Practical Special Topics (Matlab Programming) (EngE416) Prepared By Dr. Emad Saeid

STAT 391 Handout 1 Making Plots with Matlab Mar 26, 2006

Lecture 2: Variables, Vectors and Matrices in MATLAB

Introduction to MATLAB programming: Fundamentals

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

MATLAB BASICS. M Files. Objectives

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

University of Alberta

ME305: Introduction to System Dynamics

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

A quick Matlab tutorial

Introduction to MATLAB 7 for Engineers

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

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

Lecture 2. Arrays. 1 Introduction

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

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

Transcription:

CS513 Spring 19 Prof. Ron Matlab Assignment #0 Prepared by Narfi Stefansson Due January 30, 2019 Grace days can not be used for this assignment The Matlab assignments are not intended to be complete tutorials, and they will definitely not explain all the Matlab commands you will need in this course. But we will explain a few of the Matlab commands here, and point out many others that may be useful. After this first Matlab assignment, you should be familiar with the following commands: help helpdesk : (colon) linspace size plot print diary zeros ones eye xlabel ylabel title sin exp +,-,* (transpose) \ (backslash) end (as in x(2:end-1)) as well the concepts of: matrices column vectors row vectors scripts submatrices: A(ind1, ind2) =... calling a function creating a function If you are familiar with Matlab as well as with the above Matlab commands, skip the preface and go directly to the last page, where the actual assignment is to be found. Otherwise, keep on reading. Preface To start matlab, simply type Matlab at the Unix prompt. Matlab should then start, and you get the Matlab prompt, >>. The most important commands in Matlab are: help 1

helpwin helpdesk The first command gives you help inside Matlab. The second command gives you a separate window with the help texts, and the third command starts Netscape and gives you access to the extensive HTML help that comes with Matlab. If you type in help helpdesk at the Matlab prompt, you get: HELPDESK Comprehensive hypertext documentation and troubleshooting. HELPDESK loads the main MATLAB Help Desk page into the Web browser. Try the following: a=1 size(a) and compare the output with: a=1; size(a); The commands do the same, but the ; (semicolon) suppresses printing. You just defined a to be a 1-by-1 matrix. To type in larger matrices, type them in row by row, and use spaces or commas between elements, and use ; to separate rows. E.g. A = [1,2,3;4,5,6]; To look at the contents of the variable A, just type it in at the Matlab prompt: A gives: A = 1 2 3 4 5 6 You can access the elements of A through regular indexing: A(2,3) would give you 6 here. You can only use an index that exceeds the dimensions of A if you are assigning a value to A. Matlab will not complain about: A(2, 5) = 1000 But if you try to get the value of an element that doesn t exist: A(100, 100) Matlab will complain. : (the colon operator) The : can be used in two ways: To create sequences and for indexing. 1:10 gives the sequence 1 2 3 4 5 6 7 8 9 10 The other way of using the colon is to gain access to all rows or columns of a matrix. A(1,:) means: the 1st row of A and all columns. Similarly A(:, 2) means: all rows of A, and the 2nd column. 2

end The keyword end is useful for indexing (and other things). Look at the following: x = 1:10; x(3:end) = 7 1 2 7 7 7 7 7 7 7 7 so here 3:end refers to elements 3 to the end of x. You can also use e.g. end - 1 to refer to the second last element of x. diary The diary command saves the text of your Matlab session. diary on causes Matlab to save all input and most of the output to the file named diary until you give the command diary off. diary 000126 will save the input/output in the file 000126. linspace plot The linspace command stands for linearly spaced and is especially useful when generating graphs. Try the following: x = linspace(0,4,50); plot(x, exp(-x)) to see the graph of x e x. In general, if x and y are vectors of the same length, plot(x,y) plots the vector y versus the vector x, and plot(y) plots the vector y against the vector 1:length(y). You can decorate your graphs even further by using: xlabel ylabel title and use single quote characters to enclose the string: xlabel( The x-axis ) ylabel( The y-axis ) title( The title ) It s easy to print to a file in Matlab: print -dps myfig.ps tells Matlab to print the current figure to the file myfig.ps (and overwrite the current contents of the file). If you want to append to the file, use print -append -dps myfig.ps The following commands are indispensable when creating matrices: 3

zeros ones eye zeros(5,3) creates a 5-by-3 matrix of zeros, ones(3,2) creates a 3-by-2 matrix filled with 1 s, and eye(3,2) creates the 3-by-2 upper left corner of an identity matrix. If the matrix is to be square, you can use just one argument; e.g., ones(3) is the same as ones(3,3). Matrices are truly the building blocks of Matlab, so it comes at no surprise that the arithmetic operators: + - * work on matrices. Try this: A = [1, 2; 3, 4]; B = [0, 1; 1, 1]; A + B A - B A * B and notice that A * B gives you the matrix multiplication of A and B. It is very easy to solve linear equations in Matlab. The backslash does the trick. \ (backslash) If A is an n-by-n invertible matrix and b is a row vector of length n, the solution to Ax = b is given by A\b. The following operation in Matlab: (transpose) leads us to the next topic. The most common mistake in Matlab is beyond any doubt that of confusing row and column vectors. We normally think of a vector as just a vector, but Matlab thinks of it as a matrix. So the question is: is the vector an n-by-1 matrix or an 1-by-n matrix? For example, let A = [1, 2; 3, 4]; and b = [1, 0];. Then Matlab will respond to the command A*b by:??? Error using ==> * Inner matrix dimensions must agree. because b is of the size 1-by-2 and not 2-by-1 as it should be. Remember that size(b) gives you the size. You can fix this by using any of the following: 4

Typing b in again, now as a column vector: b = [1; 0]; Transposing b by using the transpose operator: b = b ;. Using the colon: b(:) returns a column vector, so b = b(:); also does the trick. The last topic for this introduction to Matlab is how you can create your own functions and scripts. Suppose you want to create a function called myfirst that accepts two matrices as input arguments and returns their sum. Then you would have to put these commands: function sum = myfirst(v, w) % sum the two inputs sum = v + w; into a file called myfirst.m in your working directory. Once you have saved it, you can invoke the function: myfirst(1, 0) myfirst([1, 2], [3, 4]) If you look back at the function that you saved in myfirst.m, the first line: function sum = myfirst(v, w) tells Matlab that your function should return the variable sum, that its name is myfirst, and that it accepts two input arguments. Finally, Matlab will print out that percented first line any time you say help myfirst. You should also try to put the following into a file called, say, firstscript in your working directory: A = eye(3); b = ones([3, 1]); x = A\b; and then type firstscript at the Matlab prompt. This simply runs the commands in the file. Remember that more information on Matlab can be/will/was obtained from (i) the first class demo, (ii) the Matlab primer (at DoIT), (iii) the extensive online help. It s time to practice the above notions... 5

Assignment 1. Generate a plot of the function sin on the interval [ 1,1] with an appropriate title. Turn in the figure together with the commands you used to generate the figure. 2. Find the solution to the following 20-by-20 system of equations. Turn in your code and the output. 1... 1 0 1 17..... 0 x = 1 0. 0 1... 1 [Note: All entries in the matrix not explicitly mentioned are meant to be zero]. 3. Create a Matlab function called ssolve that: (a) Accepts three parameters, a, b, and n, in that order. (b) Returns the solution to the following n by n system: 1 1 a 1 2... x =. 1 n 2 0 b b... b 1 n as a column vector. [Note: All entries in the matrix not explicitly mentioned are meant to be zero]. 4. Turn in a printout of the function ssolve and plots of the vectors (a) ssolve(10, 0.1, 30) (b) ssolve(2, -0.05, 50) with the appropriate titles. Also turn in the code that generated these figures. 6