Matlab Handout Nancy Chen Math 19 Fall 2004

Similar documents
Lab 1 Introduction to MATLAB and Scripts

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

AMS 27L LAB #2 Winter 2009

ME422 Mechanical Control Systems Matlab/Simulink Hints and Tips

Introduction to Unix and 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:

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

Matlab Introduction. Scalar Variables and Arithmetic Operators

Getting Started with MATLAB

Lab 1 Intro to MATLAB and FreeMat

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

PERI INSTITUTE OF TECHNOLOGY DEPARTMENT OF ECE TWO DAYS NATIONAL LEVEL WORKSHOP ON COMMUNICATIONS & IMAGE PROCESSING "CIPM 2017" Matlab Fun - 2

Polymath 6. Overview

Lab 1 Introduction to R

Introduction to the MATLAB SIMULINK Program

Excel for Gen Chem General Chemistry Laboratory September 15, 2014

EGR 102 Introduction to Engineering Modeling. Lab 05B Plotting

POLYMATH Example for the Numerical Solution of ODEs

Introduction to Matlab

Use the Move tool to drag A around and see how the automatically constructed objects (like G or the perpendicular and parallel lines) are updated.

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

2 Lab 2: LabVIEW and Control System Building Blocks

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

MMAE-540 Adv. Robotics and Mechatronics - Fall 2007 Homework 4

Figure 1: Control & Simulation Loop

Step 2: After opening the file, you should see following window. Select all the code, right click and then copy the code

2. Getting Started When you start GeoGebra, you will see a version of the following window. 1

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Introduction to Simulink

Let s Make a Front Panel using FrontCAD

x 2 + 3, r 4(x) = x2 1

Introduction to MATLAB LAB 1

ECE 202 LAB 3 ADVANCED MATLAB

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.

Laboratory 1 Introduction to MATLAB for Signals and Systems

How to Do Everything We Need to Do on a TI Calculator in Algebra 2 for Now (Unless Davies Forgot Something)

Basic Graphs. Dmitry Adamskiy 16 November 2011

Matlab Tutorial: Basics

1. Welcome. (1) Hello. My name is Dr. Christopher Raridan (Dr. R). (3) In this tutorial I will introduce you to the amsart documentclass.

Contents. Implementing the QR factorization The algebraic eigenvalue problem. Applied Linear Algebra in Geoscience Using MATLAB

Lecture 3 for Math 398 Section 952: Graphics in Matlab

CS/NEUR125 Brains, Minds, and Machines. Due: Wednesday, March 8

LAB #8 Numerical Methods

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

INTERNATIONAL EDITION. MATLAB for Engineers. Third Edition. Holly Moore

TI-89 graphing calculators are loaded with many useful features. With

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

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

ECE 3793 Matlab Project 1

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Software Compare and Contrast

Depending on the computer you find yourself in front of, here s what you ll need to do to open SPSS.

SIAM WORKSHOP: XPP. Software for Simulating Differential Equations. Ian Price 18 October, 2009

Computer Programming in MATLAB

NENS 230 Assignment 4: Data Visualization

Computational Modelling 102 (Scientific Programming) Tutorials

Microsoft Word for Report-Writing (2016 Version)

Introduction to CS graphs and plots in Excel Jacek Wiślicki, Laurent Babout,

APPM 2460 PLOTTING IN MATLAB

A Brief Introduction to MATLAB

1 Introduction to Using Excel Spreadsheets

A Guide to Using Some Basic MATLAB Functions

Assignment 02 (Due: Monday, February 1, 2016)

EOSC 473/573 Matlab Tutorial R. Pawlowicz with changes by M. Halverson

The Mathcad Workspace 7

PART 1 PROGRAMMING WITH MATHLAB

Introduction to Matlab. SAMSI Undergraduate Workshop May 15, 2006

INC151 Electrical Engineering Software Practice. MATLAB Graphics. Dr.Wanchak Lenwari :Control System and Instrumentation Engineering, KMUTT 1

Dealing with Data in Excel 2013/2016

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Introduction to MATLAB

Depending on the computer you find yourself in front of, here s what you ll need to do to open SPSS.

RK4. Version 3.0. (updated 12/1/2006) 2001, 2005, 2006 Tomas Co. Michigan Technological University Houghton, MI 49931

Overview. Lecture 13: Graphics and Visualisation. Graphics & Visualisation 2D plotting. Graphics and visualisation of data in Matlab

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

METU Mechanical Engineering Department ME 582 Finite Element Analysis in Thermofluids Spring 2018 (Dr. C. Sert) Handout 12 COMSOL 1 Tutorial 3

MAT 275 Laboratory 1 Introduction to MATLAB

MATLAB/Octave Tutorial

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

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

MATLAB Introduction to MATLAB Programming

A quick Matlab tutorial

Tutorial 3: Using the Waveform Viewer Introduces the basics of using the waveform viewer. Read Tutorial SIMPLIS Tutorials SIMPLIS provide a range of t

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

LabVIEW MathScript Quick Reference

Introduction to Simulink

Writing MATLAB Programs

Homework Set #2-3, Math 475B

Basic MATLAB Intro III

Pre-Lab Excel Problem

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

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

MATLAB The first steps. Edited by Péter Vass

Mechanical Engineering Department Second Year (2015)

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

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

Math 2524: Activity 1 (Using Excel) Fall 2002

GUI Alternatives. Syntax. Description. MATLAB Function Reference plot. 2-D line plot

Arduino IDE Friday, 26 October 2018

Transcription:

Matlab Handout Nancy Chen Math 19 Fall 2004

Introduction Matlab is a useful program for algorithm development, numerical computation, and data analysis and visualization. In this class you will only need Matlab for visualizing and solving systems of three differential equations. Getting Started You can use Matlab 6.5 in the computer labs (go to Programs Math, Statistics, Chemistry) or you can download it from the software downloads page at www.fas.harvard.edu/computing. Note that you can only use Matlab if you re connected to the Harvard network. Also, if you re a Mac user, installing Matlab on a Mac can be a pain, but there are instructions on the website. The Matlab desktop environment has 3 sub-windows: Current Directory/Workspace, Command History and Command Window. Current Directory/Workspace: You can ignore this window. It displays any files in the directory you re working in and any matrices/variables you ve created. Command History: This window displays all commands you ve typed. If you want to repeat a command or a series of commands, you can highlight those lines in this window and press Enter. Command Window: Here you type everything you want the program to do; in your case you would solve the system of differential equations and plot the trajectories. Command History displayed here Type in commands here Figure 1: The Matlab desktop environment. 2

Defining Your System of Equations Figure 2: the m file window for the function defining a fir-moose-wolf system You need to define your system of differential equations by creating what s called an m file. I will use a sample fir-moose-wolf system. Click on New, and in the new window that just popped up, type in the code for your system: On the first line, type function somename = functionname(variable1, variable2, ) somename will be used to define the function in the m file, while functionname will be used whenever you want to do something with that function (e.g. solve it or plot it). In the following lines, type the function itself. Here the output of the function is a 3 x 1 matrix (or array). The first line sets the output matrix to 0 initially, and the subsequent 3 lines represent the three differential equations. Note that the variable x is also a matrix, so you can think of x(1) as x, x(2) as y and x(3) as z. You may need to tinker with the coefficients for your project. Finally, to make life easier, the commands you will need to use are included as comments (the green lines that start with % ). When you re done, save it as functionname.m. Now you re ready to solve and plot. 3

Graphing Creating the Figure Before you begin, make sure you re working in the directory in which you saved your m file. Browse for the correct directory by clicking the button next to Current Directory on the toolbar. First you need to solve your system given initial conditions. You will use an Ordinary Differential Equation (ODE) solver, ode45, which uses the Runge-Kutta method to solve a system of differential equations. Type [t, x] = ode45( 'functionname', [t 1, t 2 ], [x 0 ; y 0 ; z 0 ]); where [var1, var2] are the variables you want your answer to be, [t 1, t 2 ] specify the desired time interval, and [x 0 ; y 0 ; z 0 ] are the initial values. The command plot(t,x); will open a figure window with the plot of the trajectories. close all; closes all open windows. Adding Legends and Titles To add a legend, type legend('name1', 'name2', 'name3');, labeling each line in order. You could also go to the Insert menu on the figure, select Legend, and then type in the labels. To add a title, type title( title you want ); or select Title from the Insert menu. Figure 3: A plot of the fir-moose-wolf system displaying the Insert menu. 4

Editing Axes You can label the axes using specific functions (xlabel, ylabel, etc.) but it s easiest to select Axes Properties from the Edit menu of the figure window. Here you can change the axes labels, appearance and range. Editing Figure Appearance You will need to be on the cursor mode to do edit the figure lines. You can also add text, lines and arrows to your figure by pressing the appropriate buttons on the toolbar. Again, the appearance of the lines can be programmed, but to keep things simple, right click on the line you want to change and select Properties. From the property window, you can change the color and style of the lines or add markers. You can add a grid (as in Figure 3) to your graph by typing grid on in the command window. grid off will erase the grid. Editor toolbar. Notice how the cursor mode is selected for figure editing Figure 4: (clockwise from top) the line property editor window, a figure window displaying the line being edited and the editing toolbar, and the axes property editor window. 5

Graphing Multiple Solutions on a Figure To add additional plots to an existing graph, use the hold function. After you type hold on, Matlab will add subsequent plots to the selected figure until the command hold off is typed. To select a figure, just click on the figure window or type figure(n), where n designates the desired figure number (the figure number is displayed on the window). Creating Subplots The subplot function allows you to have multiple plots in a single window. Type subplot(m,n,p); followed by plot(t,x); to create a m x n matrix of plots. The plot of (t,x) will be displayed in location p. For example, the set of commands: [t, x] = ode45( 'F', [0, 10], [500; 50; 10]); [t1, x1] = ode45( 'F', [0, 10], [100; 20; 10]); subplot(2,1,1); plot(t,x); subplot(2,1,2); plot(t1,x1); gives the series of subplots displayed in Figure 5 (right). Figure 5: (left) A figure window displaying two plots on one graph. This was done using the hold function. (right) A figure window with two subplots made using the code specified in the text. Last Words This is all you really need to know about Matlab for your project. If you have any further questions or if you d like to learn more about Matlab (it s a pretty neat program), consult the handy Matlab help menu. If that doesn t help you, you re more than welcome to ask me or Prof. Judson for more assistance. 6