Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

Similar documents
General MATLAB Information 1

LAB 1 General MATLAB Information 1

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Introduction to MATLAB

Introduction to MATLAB

Script started on Thu 25 Aug :00:40 PM CDT

AMS 27L LAB #1 Winter 2009

GRAPH 4.4. Megha K. Raman APRIL 22, 2015

A. Matrix-wise and element-wise operations

Dr Richard Greenaway

Ebooks Chemical Engineering

Matlab Workshop I. Niloufer Mackey and Lixin Shen

Introduction to Matlab

MATLAB Workshop Dr. M. T. Mustafa Department of Mathematical Sciences. Introductory remarks

Computer Programming in MATLAB

Introduction to Engineering gii

Matlab Programming Introduction 1 2

The Graphing Calculator

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Matlab as a calculator

Programming in MATLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

MATLAB The first steps. Edited by Péter Vass

Graphing Calculator Tutorial

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

ELEMENTARY MATLAB PROGRAMMING

POLYMATH POLYMATH. for IBM and Compatible Personal Computers. for IBM and Compatible Personal Computers

Section 5.3 Graphs of the Cosecant and Secant Functions 1

Basic stuff -- assignments, arithmetic and functions

A very brief Matlab introduction

Introduction to MATLAB

2.0 MATLAB Fundamentals

The Official Guide to Easy Graphing. ParaGraph. Ver Congratulations Graphing just got easier.

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Chapter 1 MATLAB Preliminaries

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

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

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

A very short introduction to Matlab and Octave

Built-in Types of Data

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.

Lab 1 Intro to MATLAB and FreeMat

Lecture 7 Symbolic Computations

PART 1 PROGRAMMING WITH MATHLAB

A General Introduction to Matlab

Introduction to GNU-Octave

MATLAB QUICK START TUTORIAL

A Guide to Using Some Basic MATLAB Functions

Programming in Mathematics. Mili I. Shah

# Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). use Math::Trig ':pi';

Consider this m file that creates a file that you can load data into called rain.txt

Introduction to MatLab. Introduction to MatLab K. Craig 1

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to MATLAB

1 Introduction to Matlab

Dr Richard Greenaway

9 Using Equation Networks

A Brief Introduction to MATLAB

Welcome. Please Sign-In

Introduction to MATLAB

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Introduction to Classic Maple by David Maslanka

ANSI C Programming Simple Programs

Chapter 1 Introduction to MATLAB

Introduction to Matlab

Julia Calculator ( Introduction)

Introduction to MATLAB

Introduction to MATLAB

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

Welcome to EGR 106 Foundations of Engineering II

EGR 111 Introduction to MATLAB

Precalculus: Graphs of Tangent, Cotangent, Secant, and Cosecant Practice Problems. Questions

Laboratory 1 Octave Tutorial

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

MATLAB: a Brief Introduction. by Robert L. Rankin

AMS 27L LAB #2 Winter 2009

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

MATLAB Project: Getting Started with MATLAB

EE 350. Continuous-Time Linear Systems. Recitation 1. 1

Introduction to MATLAB

Green Globs And Graphing Equations

Finding, Starting and Using Matlab

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

AP Calculus Summer Review Packet

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. Padmanabhan Seshaiyer

Matlab Introduction. Scalar Variables and Arithmetic Operators

Introduction to Programming

Graphics calculator instructions

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

10 Using the PCFL Editor In this chapter

Math 2250 MATLAB TUTORIAL Fall 2005

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

MAT 275 Laboratory 1 Introduction to MATLAB

Digital Image Analysis and Processing CPE

Graphing Calculator Scientific Calculator Version 2.0

Transcription:

What is MATLAB? MATLAB (short for MATrix LABoratory) is a language for technical computing, developed by The Mathworks, Inc. (A matrix is a rectangular array or table of usually numerical values.) MATLAB provides a single platform for computation, visualization, programming and software development. All problems and solutions in MATLAB are in a notation very similar to the way mathematics is written. In addition, you can use MATLAB to build Graphical User Interfaces (GUIs) so that you can develop user-friendly custom software. MATLAB is widely used in all areas of applied mathematics in education and research at universities and in industry. MATLAB is a great tool for solving a wide variety of problems in science, engineering, and finance. In addition, MATLAB has powerful graphic tools and can produce nice pictures in both 2D and 3D. It is also a programming language (similar to C) and is one of the easiest programming languages for writing mathematical programs. Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below. This is where you enter commands. The MATLAB environment consists of menus, buttons and a writing area similar to an ordinary word processor. There are plenty of help functions that you are encouraged to use. The writing area that you will see when you start MATLAB, is called the command window. In this window you give the commands to MATLAB. For example, when you want to enter a command or run a program you have written for MATLAB you start in the command window by typing its name after the prompt. (MATLAB s prompt is >>.) Once you have typed the command you wish MATLAB to perform, press Enter. The command window is also useful if you just want to use MATLAB as a scientific calculator or as a graphing tool. Quitting MATLAB: To quit MATLAB, at a MATLAB prompt type command quit or exit, then press Enter.

Using MATLAB as a (scientific) calculator MATLAB s computational engine uses (about) 15 decimal digits in computations (unless directed to do otherwise). So it can be used as a powerful calculator for arithmetic expressions and the calculation of values of function expressions. Arithmetic Operations MATLAB command + (addition) + a + b - (subtraction) - a - b x (multiplication) * a*b (a times b) / (division) / a/b, b 0 ^ (exponentiation) ^ a^b (a raised to the b power a b ) WARNING: You must indicate multiplication using the *. Examples: In expressions it good practice to use parentheses as illustrated below. This is particularly important for fractions which are used in MATLAB. MATLAB expressions Value shown in format long e Value in format short (1.5-4.67)*(9/3.1) -9.203225806451613e+000-9.2032 (5.2-3.72)^(1/2) 1.216552506059644e+000 1.2166 (7.02 + 12.3)/(4.25 3.125) 1.717333333333333e+001 17.1733 In MATLAB you can control how a numerical value is displayed by using what are called format commands. After a MATLAB prompt type command format long e or format short to get the displays above. Other such commands are format long and format short e. The number π (pi) in MATLAB can be accessed by using the name pi. Here is what is displayed for π in various formats. Format short display for pi 3.1416 Format long display for pi 3.141592653589793 Format short e display for pi 3.1416e+000 Format long e display for pi 3.141592653589793e+000 Note that the e formats display in scientific notation; 5.6782e+002 means 5.6782 10 2. Also note that the last decimal digit in a short display is obtained by rounding. Regardless of the display format used to show values on the screen computations are done with 15 digit arithmetic. Use command help format for the description of other display formats. Special case: A value which is exactly zero will be displayed as a single zero. If you see 0.0000, the value is not exactly zero. Change to a long format and re-display it.

Illustrations of ALGEBRA Expressions and their MATLAB form. An important convention: Multiplication must use an asterisk Standard Algebra Form 3x 2-5x + 1 2x 3 4 7x x 2 + 1 e -x MATLAB Representation 3*x^2-5*x+1 multiplication must be indicated using * and exponents require an ^ (2*x-3)/(4-7*x) the numerator and denominator must be enclosed in parentheses if they are more than 1 term sqrt(x^2+1) or (x^2+1)^.5 or (x^2+1)^(1/2) using sqrt is preferred; fractional exponents must be enclosed in parentheses exp(-x) ln(x) cos 2 (x - π) sin(x) e -2.3x tan(x) log(x) the natural log function is denoted log; log base 10 is denoted log10 cos(x-pi)^2 constant π is denoted pi; all arguments of trig. functions are considered radians sin(x)*exp(-2.3*x) note that the exponent is in parentheses and that we must use * to indicate multiplication abs(tan(x)) the absolute value is denoted by abs; parentheses must be used Matlab has a wide variety of commands for common functions and operations we provide a list of some of the basic commands next.

Operators, Elementary Functions, & General Purpose Commands Arithmetic operators. plus + minus - times * power ^ divide \ Relational operators. Equal == Not equal ~= Exponential exp - Exponential. log - Natural logarithm. log10 - Common (base 10) logarithm. log2 - Base 2 logarithm and dissect floating point number. pow2 - Base 2 power and scale floating point number. Less than < Greater than > Less than or equal <= Greater than or equal >= Logical operators. and & or not ~ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Trigonometric & Hyperbolic sqrt - Square root. sin - Sine. nextpow2 - Next higher power of 2. sinh - Hyperbolic sine. asin - Inverse sine. Complex asinh - Inverse hyperbolic sine. abs - Absolute value. cos - Cosine. angle - Phase angle. cosh - Hyperbolic cosine. complex - Construct complex data acos - Inverse cosine. from real and imaginary parts. acosh - Inverse hyperbolic cosine. conj - Complex conjugate. tan - Tangent. imag - Complex imaginary part. tanh - Hyperbolic tangent. real - Complex real part. atan - Inverse tangent. atan2 - Four quadrant inverse tangent. Calculus Operators diff -Difference and approximate atanh - Inverse hyperbolic tangent. derivative when working on a vector. sec - Secant. diff -Differentiate when working on a sech - Hyperbolic secant. string or symbolic expression; will do asec - Inverse secant. higher derivatives & partial asech - Inverse hyperbolic secant. derivatives. csc - Cosecant. int -Integrate; indefinite or definite csch - Hyperbolic cosecant. depending upon the arguments. acsc - Inverse cosecant. taylor -Taylor series expansion. acsch - Inverse hyperbolic cosecant. cot - Cotangent. Basic Plotting Tools coth - Hyperbolic cotangent. acot - Inverse cotangent. plot -Plots a set of ordered pairs. acoth - Inverse hyperbolic cotangent. plot3 -Plots a set of ordered triples. ezplot -Easy to use function plotter.

General Purpose Commands home -Moves the cursor to the upper left corner of the Command Window and clears the visible portion of the window. You can use the scroll bar to see what was on the screen previously. clc - Clear command window; scrollbar not available. demo - Run demonstrations. who - List current variables. whos - List current variables, long form. clear - Clear variables and functions from memory. load - Load workspace variables from disk. save - Save workspace variables to disk. saveas - Save Figure or model to desired output format. quit - Quit MATLAB session. exit - Exit from MATLAB. what - List MATLAB-specific files in directory. type - List M-file. which - Locate functions and files. path - Get/set search path. addpath - Add directory to search path. rmpath - Remove directory from search path. diary - Save text of MATLAB session. Diary File: At times you will need to record the results of your MATLAB session as part of an assignment. Have a flash drive ready to use so you can save your MATLAB work. Method 1: Change the Current Directory field in the desktop toolbar to the drive letter associated with the flash drive. You can also use the name of a folder on the flash drive. Now type command diary filename.txt followed by the Enter key. Each computation (but not graphics) you make in MATLAB will be saved in your directory in a text file named filename.txt. Method 2: Use a path name to the flash drive. Example: diary f:\numanalhw\section2_1problem2 This saves things to the f-drive into folder numanalhw using file name section2_1problem2 You can then edit the saved file using your favorite text editor. When you have finished your MATLAB session you can turn off the recording by typing diary off at the MATLAB prompt. If you want to stop your MATLAB session for any reason, you can reopen the diary file the next time you start MATLAB. If you use the same file name, the results of your new MATLAB session will be written at the end of the old diary file. You may want to use different names for each session on an assignment, and then merge the files.

Calculus operations in MATLAB You can differentiate and integrate functions in MATLAB. The simplest way to do this is to first name the function whose formula is constructed using the rules above and enclosed in single quotes. Examples: f = 3*x^2+2*x-5 g1 = exp(-x)*sin(x) func = sqrt(4*x+x^2) Another way for a user to define functions is by using the inline command. The following is a typical inline function: f = inline('3*x.^2 + 2*x - 5','x') Note that we need to specify the variable. After pressing enter MATLAB displays f = Inline function: f(x) = 3*x.^2 + 2*x - 5 The advantage to using inline is that you can use the name of the function for evaluation. For example to evaluate f at x = 3.6 use command f(3.6) to which MATLAB responds ans = 41.0800 The inline command can also declare functions with several variable or parameters. For example g = inline('cos(2*pi*x + ang)', 'x', ang') After pressing enter MATLAB displays g = Inline function: g(x,ang) = cos(2*pi*x + ang) To compute the first derivative use a command like diff(f) or diff(func) diff(f) gives the result 6*x + 2 diff(func) gives the result (2*x + 4)/(2*(x^2 + 4*x)^(1/2)) To compute the second derivative use a command like diff(f,2) or diff(g1,2) (Note the second argument is a 2. To compute the n th derivative use positive integer n as a second argument.) MATLAB can also compute partial derivatives. Again we use the diff command but we must indicate the variable we want to differentiate with respect to. Example: Define w = x^2*y^3 cos(x+y) then diff(w, y ) computes the partial with respect to y. The result is sin(x + y) + 3*x^2*y^2. To get the mixed partial first with respect to y, then with respect to x use command ww=diff(diff(w,'y'),'x'). The result is cos(x + y) + 6*x*y^2. To integrate or find an antiderivative use the command int. Examples: (using the functions defined above) int(f) gives the result x*(x^2 + x - 5) int(g1) gives the result -(cos(x) + sin(x))/(2*exp(x)) To compute a definite integral over a finite interval we must include the interval in the int command.

Example: int(f,-1, 3) gives the numerical result 16. (Here -1 was the lower limit and 3 the upper limit.) To use diff or int on inline functions you must convert it to a character string. For example command char(g) displays g = cos(2*pi*x + ang) and then diff(g) will compute the derivative with respect to x. Plotting equations in MATLAB exp(-x) sin(x) A simple way to plot a function given by a formula is to use the command ezplot. Usually you want to specify an interval for the domain. 0-0.5 Example: For function g1 = exp(-x)*sin(x) to graph it over -1 [-2, 10] we use command ezplot(g1,[-2,10]). The figure produced is -1.5 By default the title at the top of the graph is the expression -2 0 2 4 for the function. You can change the title. For instance if x you want to put your name on the graph use command 6 8 10 title( YOUR NAME ). You can also label the axes using commands xlabel and ylabel in a similar fashion. For example, xlabel( X-Axis ) and ylabel( Vertical Axis ). (Note the use of single quotes.) There are times when you want to graph more than one function on the same set of axes using the same domain [a, b]. To plot two functions, f and g which you have defined previously between single quotes the following set of commands will do the job. ezplot (f, [a,b]) hold on ezplot(g,[a,b]) hold off %<== this command holds the graph so we can plot a second time %<== this command release the graph so new plot can be generated To generate two graphs and have them both be viewable: ezplot (f, [a,b]) figure,ezplot(g,[a,b]) %<== the figure command generates a second graphics window for the second plot If you omit the figure command the second plot will be the only one viewable. A drawback to this approach is that the two curves will be in the same color. To print a graph you have generated use the File drop down menu at the top of the plot.

To copy a graph you have generated so you can paste it into a document use the Edit drop down menu. To close or erase all the graphs currently generated us command close all. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

General Information Once you initiate the MATLAB software, you will see the MATLAB logo appear and then the MATLAB prompt >>. The prompt >> indicates that MATLAB is awaiting a command. MATLAB is case sensitive; all built-in MATLAB commands are LOWER CASE. There are certain MATLAB features you should be aware of before you begin working with MATLAB. Variables, Expressions and Statements. MATLAB statements typically take one of two forms: variable = expression or an expression Examples val = cos(pi/7) exp(2.13) All variable (and function) names consist of a letter followed by any number of numbers, letters and underscores. MATLAB is case sensitive and only the first 19 characters of any name are significant. The equal sign = is called the assignment operator. Expressions are composed from operators, function calls and variable names. Pressing ENTER normally signifies the end of a statement, causing MATLAB to interpret the command and print its result. If the last character of a statement is a; (semicolon), however, display of the result is suppressed. This feature may be especially useful when the result of a computation is a large matrix. Finally, several statements separated by commas may be placed on a single line. When an expression is not explicitly assigned to a variable with the assignment operator (=), MATLAB automatically stores the result in the special variable ans. During a MATLAB session you may forget the names of variables stored in your workspace. The command who lists the names of all your variables. If you want to know their size as well, use the command whos. By default MATLAB stores all variables until the session is terminated. To remove a variable from the workspace use the command clear var_name. WARNING: clear with no arguments removes all variables from the workspace.

Numbers. MATLAB uses conventional decimal notation with an optional decimal point and minus sign for negative values. Scientific notation uses the letter e to specify a power of ten scale factor. Some valid numbers are: 34, -7, 0.0017, 6.3457, 2.718281828459046, 3.060196847852814e+002. All numbers are stored internally using the long format specified by the IEEE floating point standard. Floating point numbers have about 16 significant decimal digits and a finite range of about 10-308 to 10 308. Starting execution of a command. After you have typed a command name and any arguments or data required, you must press ENTER before it will begin to execute. Search Path. To determine how to execute commands MATLAB uses a search path to find m-files and other data sets. Any file you want to execute must reside in the current directory or in a folder that is on the search path. By default, the files supplied with MATLAB and MathWorks toolboxes are included in the search path. To see which directories/folders are on the search path type the command path. To change the search path select Set Path from the File menu in the desktop, and use the Set Path dialog box. The command addpath can be used to add directories to the path, and command rmpath can be used to remove directories from the path, Getting help. If you know the name of command >> help sin typing help followed by the name SIN Sine of argument in radians. displays information about the SIN(X) is the sine of the elements of X. command. >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive. The command stack. As you enter commands, MATLAB saves a number of the most recent commands in a stack. Previous commands saved on the stack can be recalled using the up arrow key. The number of commands saved on the stack varies depending on the length of the commands and other factors. Editing commands. If you make an error or mistype something in a command, you can use the left arrow and right arrow keys to position the cursor for corrections. The home key moves the cursor to the beginning of a command, and the end key moves the cursor to the end. The backspace and delete keys can be used to remove characters from a command line. The insert key is used to initiate the insertion of characters. Pressing the insert key a second time exits the insert mode. If MATLAB recognizes an error after you have pressed ENTER, then MATLAB responds with a beep and a message that helps define the error. You can recall the command line using the up arrow key in order to edit the line. To delete an entire command line press ESC, escape.

Continuing commands. MATLAB commands that do not fit on a single line can be continued to the next line using an ellipsis, which is three consecutive periods, followed by ENTER. Stopping a command. To stop execution of a MATLAB command, press Ctrl and C simultaneously, then press ENTER. Sometimes this sequence must be repeated. Suppressing Displays: When you place a semicolon at the end of a command, the command will be executed but the result will not be displayed on the screen. This is very useful when you do not want to reveal intermediate steps of a calculation. Try typing the command w = linspace(1,20,100); Note the semicolon to create a row vector with equally spaced entries. The contents of w will not be displayed. To see the contents just type w. To enter a string: type the contents of the string between single quotes '... ' command st = 'Hi there. ' displays st = Hi there. Strings are not commands. Strings supply information. Strings are used for messages and for formulas as previously illustrated.