Fall 2015 Math 337. Basic MatLab

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

Introduction to MATLAB

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

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

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

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

Lab 1 Intro to MATLAB and FreeMat

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

Introduction to MATLAB

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

Computer Packet 1 Row Operations + Freemat

MATLAB Lecture 1. Introduction to MATLAB

A Brief Introduction to MATLAB

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

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.

Getting Started in Matlab

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

MAT 275 Laboratory 1 Introduction to MATLAB

A General Introduction to Matlab

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:

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

2.0 MATLAB Fundamentals

Introduction to MATLAB

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

MATLAB primer for CHE 225

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

Lecture 5 of AMS 200 LaTeX Typesetting, Writing and

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

Introduction to Unix and Matlab

What is Matlab? A software environment for interactive numerical computations

AMS 27L LAB #2 Winter 2009

MatLab Just a beginning

Matlab Tutorial, CDS

University of Alberta

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

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

MATH 2221A Mathematics Laboratory II

Introduction to Matlab. By: Hossein Hamooni Fall 2014

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

Introduction to MATLAB

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

A Guide to Using Some Basic MATLAB Functions

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

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

Introduction to MATLAB

Dr Richard Greenaway

Introduction to MATLAB

Introduction to MatLab. Introduction to MatLab K. Craig 1

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

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

Workpackage 5 - Ordinary Differential Equations

INTRODUCTION TO MATLAB

Introduction to Engineering gii

Introduction to MATLAB

Introduction to MATLAB

Introduction to Matlab

selected topic in transportation 2552/2. 1 Prepared by Lect.Weerakaset Suanpaga

Math F302: Octave Miscellany September 28, e 1 x3 dx. Here s how to find a numerical approximation with Octave

Computer Laboratory Manual: Calculus for the Life Sciences

Lecture 2: Variables, Vectors and Matrices in MATLAB

Stokes Modelling Workshop

MATLAB BEGINNER S GUIDE

Can be put into the matrix form of Ax=b in this way:

A Tour of Matlab for Math 496, Section 6

Chill Out: How Hot Objects Cool

MATH 2221A Mathematics Laboratory II

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

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

PART 1 PROGRAMMING WITH MATHLAB

MATLAB An introductory tutorial

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Lab 1 Introduction to MATLAB and Scripts

Teaching Manual Math 2131

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

EOSC 352 MATLAB Review

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

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

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

The MathWorks - MATLAB Digest June Exporting Figures for Publication

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

Computational Photonics, Seminar 01 on Introduction into MATLAB, Page 1

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Solving Systems of Equations Using Matrices With the TI-83 or TI-84

AMS 27L LAB #1 Winter 2009

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

Grace days can not be used for this assignment

Basic MATLAB. Getting MATLAB (for MIT students) Creating matrices and vectors. Some operation of matrices/vectors.

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

ECE 3793 Matlab Project 1

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

An Introduction to Numerical Methods

MATH36032 Problem Solving by Computer. Publishable Quality Graphics

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

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

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

Introduction to Simulink

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

MATLAB/Octave Tutorial

Transcription:

Fall 215 Math 337 Basic MatLab MatLab is a powerful software created by MathWorks, which is used extensively in mathematics, engineering, and the sciences. It has powerful numerical and graphic capabilities, so is an invaluable tool for students to learn. This sheet is an introduction to the software for Math 337. Basic commands are introduced, and several examples related to class are shown, including a couple of functions and graphics commands. MatLab stands for Matrix Laboratory, so we begin with some basics applied to matrices. The matrices, A and B, are entered into MatLab in the following manner: A = [1-2 3-1 2 5 6 1 4 3-1 -3 ]; B = [2 1 4;-1-3 5 1;6-2;1-2 -1 5]; The matrices in MatLab are stored in the variables A and B. One enters the rows starting with a [, then numbers are entered either separated by a space or comma. New rows to the matrix are produced either with with a ; separating the rows or using enter to move to the next line. The matrix ends when ] is entered. If this is followed by a ;, then MatLab does not output the matrix. However, if enter is used, MatLab displays the matrix. Normal arithmetic operations are easily performed, such as A + B, A B, or raising a power A 3, in the last case typing A^3. Below shows the matrix multiplication, AB, and the inverse, A 1. Note that if no variable is specified, then MatLab automatically stores the answer in the temporary variable ans. A*B -7-5 -3 23 1-17 5 23 22-2 -4 42-11 6-5 17 inv(a) -.3919.1486.181.2973-1.5811 1.135 -.811 1.27.1351 -.1892.1351 -.3784.5541 -.1757.541 -.3514 MatLab can easily solve the linear equation Ax = b. We note that the transpose of a matrix is given by b in MatLab. Below we show two ways to solve this equation. b = [2 1 5]; x = linsolve(a,b ) x =

.818 1.8919-1.4865 -.5946 inv(a)*b.818 1.8919-1.4865 -.5946 Similarly, MatLab can readily find eigenvalues and eigenvectors. eig(a) 2.39 + 4.9965i 2.39-4.9965i -1.921 1.2842 [v,d]=eig(a) v = Columns 1 through 3 -.1627 -.3412i -.1627 +.3412i.495 -.3486 -.541i -.3486 +.541i.7464 -.7459 -.7459 -.1567. -.4199i. +.4199i -.56 Column 4.735.9698 -.1967 -.1242 d = Columns 1 through 3 2.39 + 4.9965i 2.39-4.9965i -1.921 Column 4 1.2842 We note that all of the previous scripts were produced in MatLab and stored in a text file (test.txt). This is readily done by the MatLab commands: diary( test.txt ) diary on MatLab entries here

diary off The file test.txt will contain all the commands entered and the MatLab output in a text file, which can be easily read in any text editor. At this time we turn specifically to our murder problem. From the main lectures we saw that we needed to find the values for the heat coefficient, k, and the time of death, t d, which solved the equations 22 + 8e k = 28 and 22 + 8e kt d = 37. The first equation is equivalent to finding the zero of f(k) = 22 + 8e k 28. MatLab has an easy way to enter an inline function, and the software has the special function fzero, which can be used to numerically find zeroes of a function. This special function fzero has the form fzero(f, x), where f is the function of some variable, and x is an initial guess where the zero might be. The sequence of commands below begins with obtaining more digits, the it creates the necessary functions and solves them. format long f = @(k) 22+8*exp(-k)-28; k = fzero(f,.3) k =.28768272451781 ft = @(t) 22+8*exp(-k*t)-37; td = fzero(ft,-5) td = -2.185811344673 These values agree well with the ones found in the lecture notes. As pointed out in lecture, many differential equations cannot be solved exactly. MatLab has powerful routines for numerically solving a differential equation. Here we use the example from our murder problem: dt (t) = k(t (t) T e ), T () = 3, (1) dt where t = corresponds to 8:3 AM, T e = 22 C is the room temperature, and k is the heat coefficient found above using the body temperature at t = 1. Rather than working with line commands in the Command Window of MatLab, it is valuable to learn to create M-files for both functions and scripts. Before creating a new function or script file, it is a very good idea to point the Command Window to the directory where you are going to store your MatLab files. This is done in the window below greyed area for operations. We begin by creating a function, which is done by clicking on the New tab and selecting Function. In this case, we want to enter the equation for the right hand side of the differential equation (1) and below we see the M-file (stored murder.m). function yp = murder(t,y) % Model for Newton s Law of Cooling % Parameters for the model

k = log(4/3); Te = 22; yp = -k*(y - Te); % This is the RHS of the DE end Note that MatLab uses % beginning any comments that are not executed. The next step is to use MatLab s numerical ODE solver to create the solution of the differential equation from the time of death, t d, found earlier for about 12 hours after the murder. Below we show the solving of the model and plotting of the graph. [t1,y1] = ode23(@murder,[,td],3); [t2,y2] = ode23(@murder,[,12],3); plot(t1,y1, b-,t2,y2, b- );grid; This produces a blue solution from the time of death until 12 hours after t =. This is quick way to produce a graph, but we want to illustrate the power of MatLab to produce a very good looking, professional graph. Below we present an M-file, Script, which executes a series of commands to produce a good graph. There are comments interior to the program to help understand what is happening, and clearly one could explore other options to improve the graph. clear figure(1) clf hold off % Clear previous definitions % Assign figure number % Clear previous figures % Start with fresh graph mytitle = Body Temperature ; xlab = $t$ ; % X-label ylab = $T(t)$ ; % Y-label % Title f = @(k) 22+8*exp(-k)-28; % Function for finding heat coefficient k = fzero(f,.3); % Find the heat coefficient ft = @(t) 22+8*exp(-k*t)-37; % Function for finding the time of death td = fzero(ft,-5); % Find the time of death [t1,y1] = ode23(@murder,[,td],3); % Simulate the heat equation [t2,y2] = ode23(@murder,[,12],3); % Simulate the heat equation plot(t1,y1, k-, LineWidth,1.5); % Plot from death to t = hold on % Plots Multiple graphs plot(t2,y2, k-, LineWidth,1.5); % Plot from t = to 12 plot([-9,td],[37,37], r-, LineWidth,1.5); % Plot from t = -9 to td plot([-9,12],[22,22], b:, LineWidth,1.5); % Plot room temperature plot([td,td],[2,4], k:, LineWidth,1.5); % Plot time of murder grid % Adds Gridlines text(-2.7,28, Time of death, $t_d$, rot,9, FontSize,14,... FontName, Times New Roman, interpreter, latex ); text(-4.8,22.6, Room Temperature, color, blue, FontSize,14,... FontName, Times New Roman ); axis([-9 12 2 4]); % Defines limits of graph

fontlabs = Times New Roman ; % Font type used in labels xlabel(xlab, FontSize,14, FontName,fontlabs, interpreter, latex ); % x-label size and font ylabel(ylab, FontSize,14, FontName,fontlabs, interpreter, latex ); % y-label size and font title(mytitle, FontSize,16, FontName, Times New Roman, interpreter, latex ); % Title size/font set(gca, FontSize,12); % Axis tick font size print -depsc body_temp.eps % Create figure as EPS file print -djpeg body_temp.jpg % Create figure as JPEG file Note: All graphing scripts should have the first 4 commands to start with a clean Workspace, clear old and label a new figure, and refresh the figure window. The last lines starting with fontlabs give the user a lot of control on the appearance of writing on the graphs, so are very useful to have readily available for any graph. Finally, the last two lines are necessary to have the graph readily available for use in a document (EPS for L A TEXand JPG for Word). In subsequent graphing script these heading and trailing lines of code will not appear in these write-ups to avoid repetition. The product of MatLab script entitled murder plot, which is available from my webpage, is 4 Body Temperature 38 36 T(t) 34 32 3 28 Time of death, td 26 24 22 Room Temperature 2 8 6 4 2 2 4 6 8 1 12 t