Professional Documents
Culture Documents
File 4
File 4
File 4
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
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
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 Functions
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Homework
Mike Renfro
Introduction to MATLAB
Mike Renfro
Introduction to MATLAB
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
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
Starting MATLAB
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
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
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
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
Workspace
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
Command History
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
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
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
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
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
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
Mike Renfro
Introduction to MATLAB
Starting MATLAB, Exiting, Getting Help Major Elements of the MATLAB Environment MATLAB as a Calculator
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
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
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
Variable Example
Mike Renfro
Introduction to MATLAB
Mike Renfro
Introduction to MATLAB
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
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Mike Renfro
Introduction to MATLAB
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Mike Renfro
Introduction to MATLAB
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Mike Renfro
Introduction to MATLAB
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Mike Renfro
Introduction to MATLAB
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
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Mike Renfro
Introduction to MATLAB
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Function Example: Projectile Motion Rules for Writing Functions 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
Function Example: Projectile Motion Rules for Writing Functions Functions With Multiple Returned Variables
Mike Renfro
Introduction to MATLAB
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