MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

Similar documents
January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

The roots are found with the following two statements. We have denoted the polynomial as p1, and the roots as roots_ p1.

EEE161 Applied Electromagnetics Laboratory 1

Lecture 5b: Starting Matlab

USE OF MATLAB TO UNDERSTAND BASIC MATHEMATICS

Introduction to Computer Tools and Uncertainties

LAB 1: MATLAB - Introduction to Programming. Objective:

TOPIC 2 Computer application for manipulating matrix using MATLAB

Introduction to Computational Neuroscience

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

2: SIMPLE HARMONIC MOTION

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

ES205 Analysis and Design of Engineering Systems: Lab 1: An Introductory Tutorial: Getting Started with SIMULINK

Computational Chemistry Lab Module: Conformational Analysis of Alkanes

Linear Motion with Constant Acceleration

2: SIMPLE HARMONIC MOTION

Homework 1 Solutions

Sections 6.1 and 6.2: Systems of Linear Equations

Electric Fields and Equipotentials

Chapter 2: Numeric, Cell, and Structure Arrays

courses involve systems of equations in one way or another.

Lab 2: Static Response, Cantilevered Beam

Introduction to GNU Octave

The following steps will help you to record your work and save and submit it successfully.

MATLAB Project 2: MATH240, Spring 2013

AMS 27L LAB #8 Winter 2009

Laboratory handout 1 Mathematical preliminaries

Chapter 1 Linear Equations. 1.1 Systems of Linear Equations

MAT 275 Laboratory 7 Laplace Transform and the Symbolic Math Toolbox

Lab 1: Dynamic Simulation Using Simulink and Matlab

Introduction to GNU Octave

General Physics I Lab. M1 The Atwood Machine

Companion. Jeffrey E. Jones

Lecture 9: Elementary Matrices

Basic Linear Algebra in MATLAB

Math Assignment 3 - Linear Algebra

Signal Processing First Lab 11: PeZ - The z, n, and ˆω Domains

Lab #10 Atomic Radius Rubric o Missing 1 out of 4 o Missing 2 out of 4 o Missing 3 out of 4

Newton's 2 nd Law. . Your end results should only be interms of m

+ MATRIX VARIABLES AND TWO DIMENSIONAL ARRAYS

Lecture 3: Special Matrices

DSP First. Laboratory Exercise #10. The z, n, and ˆω Domains

Lab 11 Simple Harmonic Motion A study of the kind of motion that results from the force applied to an object by a spring

Molecular Modeling and Conformational Analysis with PC Spartan

Lab 15 Taylor Polynomials

DOAS measurements of Atmospheric Species

Unit 8 - Polynomial and Rational Functions Classwork

Computer simulation of radioactive decay

Simulink Tutorial 1 CPE562

Geology Geomath Computer Lab Quadratics and Settling Velocities

EE -213 BASIC CIRCUIT ANALYSIS LAB MANUAL

More Polynomial Equations Section 6.4

BUILDING BASICS WITH HYPERCHEM LITE

Using the Stock Hydrology Tools in ArcGIS

1-D Convection-Diffusion Lab

Physics 212E Spring 2004 Classical and Modern Physics. Computer Exercise #2

Using MATLAB. Linear Algebra

Figure 2.1 The Inclined Plane

Center of Mass. Evaluation copy

MATLAB Ordinary Differential Equation (ODE) solver for a simple example 1. Introduction

MOL410/510 Problem Set 1 - Linear Algebra - Due Friday Sept. 30

POC via CHEMnetBASE for Identifying Unknowns

BCMB/CHEM 8190 Lab Exercise Using Maple for NMR Data Processing and Pulse Sequence Design March 2012

DSP First Lab 11: PeZ - The z, n, and ωdomains

In this LAB you will explore the following topics using MATLAB. Investigate properties of the column space of a matrix.

1 Overview of Simulink. 2 State-space equations

Instructor Notes for Chapters 3 & 4

3. Array and Matrix Operations

Measuring Planck s Constant By Martin Hackworth

Building Inflation Tables and CER Libraries

Lab 1: Numerical Solution of Laplace s Equation

Using Microsoft Excel

Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A i

Using the Budget Features in Quicken 2008

1 Introduction & Objective. 2 Warm-up. Lab P-16: PeZ - The z, n, and O! Domains

(t) ), ( ) where t is time, T is the reaction time, u n is the position of the nth car, and the "sensitivity coefficient" λ may depend on un 1(

Octave. Tutorial. Daniel Lamprecht. March 26, Graz University of Technology. Slides based on previous work by Ingo Holzmann

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Learning Packet Grading Form

Math 1314 Week #14 Notes

To Create a Simple Formula using the Point and Click Method:

Introduction to Matlab

Measuring the time constant for an RC-Circuit

Measurements of a Table

LAB 2: Orthogonal Projections, the Four Fundamental Subspaces, QR Factorization, and Inconsistent Linear Systems

Essential Mathematics

The Geodatabase Working with Spatial Analyst. Calculating Elevation and Slope Values for Forested Roads, Streams, and Stands.

Example 1 (Characteristic Equation, Eigenvalue, and Eigenvector)

Using Tables and Graphing Calculators in Math 11

MAT300/500 Programming Project Spring 2019

Titrator 3.0 Tutorial: Calcite precipitation

Project IV Fourier Series

Representing Polynomials

VECTORS AND MATRICES

ECE2111 Signals and Systems UMD, Spring 2013 Experiment 1: Representation and manipulation of basic signals in MATLAB

Squares & Square Roots. Perfect Squares

POC via CHEMnetBASE for Identifying Unknowns

Lesson 6b Rational Exponents & Radical Functions

Defense Acquisition University

RADIATION PROCEDURES MANUAL Procedure Cover Sheet

Transcription:

MATLAB BASICS Instructor: Prof. Shahrouk Ahmadi 1. What are M-files TA: Kartik Bulusu M-files are files that contain a collection of MATLAB commands or are used to define new MATLAB functions. For the purposes of this class, we will only need very simple M-files. However, you may find them useful for other parts of your homework as well. For example you may put a bunch of MATLAB commands into an M-file and then run all of those commands at the MATLAB prompt simply by typing the name of the M-file. You are also urged to create M-files while going through the handout. i) How to make M-files in General Make sure that the MATLAB command window is active. Choose 'NEW M-file' from the 'File' menu. (This will bring up MATLAB's M-file editor.) Type / Edit your file. Choose 'Save' from the 'File' menu and then click on the 'DESKTOP' button, and then click on the Workspace icon. In the 'SAVE document as:' box, type in the name you want to call the file. (There maybe a name in the box already, if it is ok leave it alone, otherwise change it.) Important: the name of your file should end in '.m', for example 'whatever.m' (without the quotes) is a fine name. Finally click on the SAVE button to save the M-File. In order to use this file please be sure to type 'start' in the MATLAB command window at the beginning of your session. This ensures that MATLAB will be able to find your M-files. 1

2. Matrices The basic building block of MATLAB is the matrix. The fundamental data-type is the array. Special cases of this basic data-type are vectors, scalars, real and complex matrices. An array is a list of numbers or expressions arranged in horizontal rows and vertical columns. When an array has one row or column, it is called a vector. An array with m rows and n columns is a called a matrix of size m n. (Pratap, 1999) Entering matrices into Matlab is the same as entering a vector, except each row of elements is separated by a semicolon (;) or a return: B = [1 2 3 4;5 6 7 8;9 10 11 12] B = 1 2 3 4 5 6 7 8 9 10 11 12 Transpose of a matrix Matrices in Matlab can be manipulated in many ways. For one, you can find the transpose of a matrix using the apostrophe key: C = B' 2

C = 1 5 9 2 6 10 3 7 11 4 8 12 Matrix Multiplication Now you can multiply the two matrices B and C together. Remember that order matters when multiplying matrices. D = B * C D = 30 70 110 70 174 278 110 278 446 D = C * B D = 107 122 137 152 122 140 158 176 137 158 179 200 152 176 200 224 Another option for matrix manipulation is that you can multiply the corresponding elements of two matrices using the.* operator (the matrices must be the same size to do this). E = [1 2;3 4] F = [2 3;4 5] 3

Term by term multiplication (.*) G = E.* F E = 1 2 3 4 F = 2 3 4 5 G = 2 6 12 20 Matrix Exponentiation If you have a square matrix, like E, you can also multiply it by itself as many times as you like by raising it to a given power. E^3 ans = 37 54 81 118 Term by term exponentiation (.ˆ ) If wanted to cube each element in the matrix, just use the element-by-element cubing. E.^3 4

ans = 1 8 27 64 The inverse of a matrix: X = inv(e) Matrix Division X = -2.0000 1.0000 1.5000-0.5000 P = E / F % This is similar to executing P = E * inv(f) E = 1 2 3 4 F = 2 3 4 5 P = 1.5000-0.5000 0.5000 0.5000 Term by term division (./) Q = E./ F 5

E = 1 2 3 4 F = 2 3 4 5 Q = 0.5000 0.6667 0.7500 0.8000 Matrix Equation: Ax = b X=A -1 b Matrix equation is used in solving linear simultaneous equations. For matrices of order 2 or 3, the calculation is simple and can be done on paper. However it is when one encounters matrices of larger orders that this computation becomes tedious and sometimes impossible without computer programs. A simple MATLAB program can be scripted to solve the matrix equation. For example, consider the following set of equations that need to be solved for x, y and z. x + y z = 4 x 2y + 3z = 6 2x + 3y + z = 7 6

In terms of the matrix equation the set of equations above become = 7 6 4 1 3 2 3 2 1 1 1 1 z y x Matlab program will have the following script. A = [1 1-1; 1-2 3; 2 3 1]; B = [4; -6; 7]; X = inv(a) * B The result should print out on the screen as follows: X = 1.0000 2.0000-1.0000 7

3. Handling Graphics i) Line Plots >> t = 0:pi/100:2*pi; % increments the value of t from 0 to 2*pi in steps of pi/100 >> y = sin(t); >> plot(t,y) 8

>> xlabel('t') >> ylabel('sin(t)') >> title('the plot of t vs sin(t)') 9

>> y2 = sin(t-0.25); >> y3 = sin(t+0.25); >> hold on; % <== to keep previously defined stuff >> plot(t,y,t,y2,t,y3) % why not just plot only the 2nd and 3 curves? >> legend('sin(t)','sin(t-0.25)','sin(t+0.25',1) 10

ii) Bar Graphs >> x = magic(3) x = 8 1 6 3 5 7 4 9 2 >> bar(x) >> grid 11

Example: Suppose that you would like to plot the first 100 terms in the sequences a n = ncos(n)/(n 2 +1) and b n = (.99) n /3. This can be accomplished with the following commands: >> n=1:1:100; >> a=n.*cos(n)./(n.^2+1); >> b=(.99).^n/3; >> plot(n,a,'bx',n,b,'r.'),shg >> title('the Example: plots of a_n (the x''s) and b_n (the dots)') The resulting graph is 12

To learn more about the plot command, type the command: >>help plot Remark: The following commands would have worked as well to make the previous example. >>n=1:1:100; >>a=n.*cos(n)./(n.^2+1); >> b=(.99).^n/3; >> plot(n,a,'bx'),shg >> hold on % (This keeps the previous graph from being erased.) >> plot(n,b,'r.'),shg >> title('the Example: plots of a_n (the x''s) and b_n (the dots)') 4. Polynomials i) Entering a Polynomial In Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simply enter each coefficient of the polynomial into the vector in descending order. For instance, let's say you have the following polynomial: To enter this into Matlab, just enter it as a vector in the following manner >> x = [1 3-15 -2 9] x = 1 3-15 -2 9 Matlab can interpret a vector of length n+1 as an nth order polynomial. Thus, if your polynomial is missing any coefficients, you must enter zeros in the appropriate place in the vector. For example, would be represented in Matlab as: 13

>> y = [1 0 0 0 1] ii) Evaluating a Polynomial You can find the value of a polynomial using the polyval function. For example, to find the value of the above polynomial at s=2, >> z = polyval([1 0 0 0 1],2) z = 17 iii) Roots of a Polynomial You can also extract the roots of a polynomial. This is useful when you have a high-order polynomial such as Finding the roots would be as easy as entering the following command; roots([1 3-15 -2 9]) ans = -5.5745 2.5836-0.7951 0.7860 14

In-Class Exercise : 1. Solve x + y + z = 6 x y + z = 2 x + 2y z = 2 for x, y and z using MATLAB 2. Solve x + y z = 1 8x + 3y 6z = 1 4x y + 3z = 1 for x, y and z using MATLAB 3. Evaluate the following polynomial at x = 6 and calculate it s roots. x 6 5 4 3 2 + 3x 100x + 54.286x + x + 3x + 6 15

References: 1. http://scv.bu.edu/scv/tutorials/matlab/index.html 2. http://math.ucsd.edu/courses/math21d/matlabhw/basics/matlab_basics.ht m 3. http://www.engin.umich.edu/group/ctm/basic/basic.html#matrix 4. Pratap, R., Getting Started with MATLAB 5 - A Quick Introduction for Scientists and Engineers, Oxford University Press, 1999. 16