File 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Outlines

Introduction to MATLAB
Mike Renfro September 9, 2004

Mike Renfro

Introduction to MATLAB

Outlines

Part I: Review of Previous Lecture Part II: Introduction to MATLAB Part III: Writing MATLAB Functions

Review of Previous Lecture

Mike Renfro

Introduction to MATLAB

Outlines

Part I: Review of Previous Lecture Part II: Introduction to MATLAB Part III: Writing MATLAB Functions

Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help
Starting MATLAB Exiting MATLAB Getting Help

Major Elements of the MATLAB Environment


Command Window Workspace Command History Current Directory Selector

MATLAB as a Calculator
Real Values Complex Values Trigonometric Functions Function Reference Variables
Mike Renfro Introduction to MATLAB

Outlines

Part I: Review of Previous Lecture Part II: Introduction to MATLAB Part III: Writing MATLAB Functions

Writing MATLAB Functions

Writing Functions
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Homework

Mike Renfro

Introduction to MATLAB

Part I Review of Previous Lecture

Mike Renfro

Introduction to MATLAB

Review of Previous Lecture

General forms of the problem: f (x ) = 0, g (x ) = C , f (x ) = g (x ) Types of nonlinear equations: polynomial, transcendental (trigonometric, hyperbolic, exponential, logarithmic) Graphical interpretation (places where equations intersect, or where one equation contacts the x axis) Incremental search method

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Part II Introduction to MATLAB

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Starting MATLAB Exiting MATLAB Getting Help

Starting MATLAB

Start Programs Engineering MATLAB 6.5 MATLAB 6.5

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Starting MATLAB Exiting MATLAB Getting Help

Exiting MATLAB

File Exit or type exit and press Enter at the MATLAB command prompt.

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Starting MATLAB Exiting MATLAB Getting Help

Getting Help
Help MATLAB Help This is a very good starting point for the most commonly-used MATLAB documentation. Major topic areas include: Getting Started Using MATLAB Examples MATLAB Functions Listed by Category MATLAB Functions Listed Alphabetically

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Command Window Workspace Command History Current Directory Selector

Command Window

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Command Window Workspace Command History Current Directory Selector

Workspace

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Command Window Workspace Command History Current Directory Selector

Command History

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Command Window Workspace Command History Current Directory Selector

Current Directory Selector

The rst thing you should do after starting MATLAB is to set the current directory to either your U: drive, your Zip disk, or somewhere else you can save les permanently. Some labs may start MATLAB in C:\TEMP or some other temporary area.

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Basic Mathematical Operators


>> 1+3 ans = 4 >> 2 -5 ans = -3 >> 1.5*2.5 ans = 3.7500 >> 4.5/3 ans = 1.5000 >>

The symbols +, -, *, and / act the same as in most languages (addition, subtraction, multiplication, and division).

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

More Mathematical Operators


The parentheses symbols, ( and ), are used to group mathematical terms together.
>> 1+2*3 ans = 7 >> (1+2)*3 ans = 9 >>

The caret (^) is used for raising a number to a power: 2^3 in MATLAB is the same as 23 in mathematics.
>> 2^3 ans = 8 >>

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Complex Values
MATLAB handles real and complex numbers equally easily. C, at least, does not have any standard facility for complex math. C++ and FORTRAN handle complex numbers as easily as they handle real numbers.
>> i * i ans = -1 >> 2i -3 j ans = 0 - 1.0000 i >>

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Basic Trigonometric Functions


All arguments to MATLAB trigonometric functions are in units of radians, not degrees. sin(), cos(), tan(), cot(), sec(), csc()
>> sin (0) ans = 0 >> tan ( pi /4) ans = 1.0000 >> cos (60*( pi /180)) ans = 0.5000 >>

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Inverse Trigonometric Functions


All results from MATLAB inverse trigonometric functions are in units of radians, not degrees. asin(), acos(), atan(), acot(), atan2(), asec(), acsc()
>> asin (.5) ans = 0.5236 >> asin (.5)*(180/ pi ) ans = 30.0000 >> atan2 ( -1 , -1) ans = -2.3562 >>

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Hyperbolic and Inverse Hyperbolic Trigonometric Functions

sinh(), cosh(), tanh(), coth(), sech(), csch(), asinh(), acosh(), atanh(), acoth(), asech(), acsch()

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Reminder: MATLAB Function Reference

Help MATLAB Help MATLAB Functions Listed By Category Help MATLAB Help MATLAB Functions Listed Alphabetically

Mike Renfro

Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Variable Usage
The rst thing well show that separates MATLAB from a basic scientic calculator is its ability to store numbers as variables for later usage. This lets you start building up equations out of meaningful names instead of less-obvious numbers. MATLAB variable names must begin with a letter, which may be followed by any combination of letters, digits, and underscores. MATLAB distinguishes between uppercase and lowercase characters, so A and a are not the same variable. MATLAB only uses the rst 63 characters of a variable name, so make sure your variable names are unique within the rst 63 characters.
Mike Renfro Introduction to MATLAB

Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator

Real Values Complex Values Trigonometric Functions Function Reference Variables

Variable Example

>> circleRadius =2 circleRadius = 2 >> circleArea = pi * circleRadius ^2 circleArea = 12.5664

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Part III Writing MATLAB Functions

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Writing Functions

MATLAB functions are reusable bits of MATLAB code that perform a series of calculations or other tasks. Normally, a function takes one or more input arguments and returns one or more results into output variables. A function is saved in its own le with a .m extension, such as computeprojectilepositionx.m

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Function Example: Projectile Motion


Given: a projectile is launched with an initial velocity v0 and initial angle . Calculate: the projectiles horizontal and vertical positions relative to launch at a given time t . Governing equations: x = (v0 cos ) t 1 y = (v0 sin ) t gt 2 2

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Mathematics to MATLAB Translation

x = (v0 cos ) t computeprojectilepositionx.m


function x = c o m p u t e p r o j e c t i l e p o s i t i o n x ( v0 , theta , t ) % Compute the relative horizontal position % of a projectile % % Usage : c o m p u t e p r o j e c t i l e p o s i t i o n x ( v0 , theta , t ) x =( v0 * cos ( theta ))* t ;

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Mathematics to MATLAB Translation


1 y = (v0 sin ) t gt 2 2 computeprojectilepositiony.m
function y = c o m p u t e p r o j e c t i l e p o s i t i o n y ( v0 , theta , t ) % Compute the relative vertical position % of a projectile % % Usage : c o m p u t e p r o j e c t i l e p o s i t i o n y ( v0 , theta , t ) g =9.808; % m / s ^2 y =( v0 * sin ( theta ))* t -0.5* g * t ^2;

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Procedure for Creating Functions


Change directories to somewhere you can save les permanently (U:\, your Zip disk, etc.) At the MATLAB command prompt, type edit computeprojectilepositionx.m, and click the Yes button when it asks you if you want to create a new le. Enter the six lines of MATLAB code into the editor window that pops up. Save the le, and make sure you save it as computeprojectilemotionx.m Repeat the previous two steps for computeprojectilemotiony.m

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

function x = c o m p u t e p r o j e c t i l e p o s i t i o n x ( v0 , theta , t ) % Compute the relative horizontal position % of a projectile % % Usage : c o m p u t e p r o j e c t i l e p o s i t i o n x ( v0 , theta , t ) x =( v0 * cos ( theta ))* t ; function y = c o m p u t e p r o j e c t i l e p o s i t i o n y ( v0 , theta , t ) % Compute the relative vertical position % of a projectile % % Usage : c o m p u t e p r o j e c t i l e p o s i t i o n y ( v0 , theta , t ) g =9.808; % m / s ^2 y =( v0 * sin ( theta ))* t -0.5* g * t ^2;

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Test The Functions

Test out how your functions work with a variety of inputs:


>> >> >> >> >> >> help c o m p u t e p r o j e c t i l e p o s i t i o n x c o m p u t e p r o j e c t i l e p o s i t i o n x (5 , pi /4 ,0) c o m p u t e p r o j e c t i l e p o s i t i o n x (5 , pi /4 ,2) c o m p u t e p r o j e c t i l e p o s i t i o n y (5 , pi /4 ,0.1) c o m p u t e p r o j e c t i l e p o s i t i o n y (5 , pi /4 ,0.25) c o m p u t e p r o j e c t i l e p o s i t i o n y (5 , pi /4 ,1)

Do all the results make sense?

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Rules for Writing Functions


The rst part of the function lename must match the name of the function given on line 1. The variable on the left-hand side of the equal sign on line 1 must be given a value before the function ends. In general, the function cannot use any predened variables except for the ones listed in parentheses on line 1. There are exceptions to this rule, but you have to take special steps to get around it. The names of the variables listed in parentheses dont have to match anything previously dened, but their meanings have to match.
Mike Renfro Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Functions With Multiple Returned Variables

MATLAB functions can return as many results as needed, instead of the single return variable shown in previous examples. The only change required is for you to specify multiple return variables on the left hand side of the equal sign on line 1 of your function.

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables

Projectile Motion Example with Multiple Returned Variables


function [x , y ]= c o m p u t e p r o j e c ti l e p o s i t i o n ( v0 , theta , t ) % Compute the relative horizontal and vertical % position of a projectile % % Usage : [x , y ]= c o m p u t e p r oj e c t i l e p o s i t i o n ( v0 , theta , t ) g =9.808; % m / s ^2 x =( v0 * cos ( theta ))* t ; y =( v0 * sin ( theta ))* t -0.5* g * t ^2;

Test this as follows:


>> [x , y ]= c o m p u t e p r o j e c t i l e p o s it i o n (5 , pi /4 ,0.25)

Mike Renfro

Introduction to MATLAB

Writing Functions Homework

Homework
Write MATLAB functions to calculate results related to projectile motion as follows: Calculate x and y positions relative to a xed origin (0, 0) assuming the projectile is red from a position (x0 , y0 ) according to the equations x = x0 + v0 cos t 1 y = y0 + v0 sin t gt 2 2 Calculate x and y components of velocity according to the equations vx = v0 cos vy = v0 sin gt
Mike Renfro Introduction to MATLAB

You might also like