logo Your Math Help is on the Way!

More Math Help

Home
Algebraic Symmetries
Radical Expressions and Equation
The Exponential Function
Math 1010-3 Exam #3 Review Guide
MATH 511 ASSIGNMENT SHEET
Rational Numbers Worksheet
Are You Ready for Math 65?
Solving Simultaneous Equations Using the TI-89
Number Theory: Fermat's Last Theorem
algorithms-in-everyday-mathematics
COLLEGE ALGEBRA
Course Syllabus for Intermediate Algebra
Solving Inequalities with Logarithms and Exponents
Introduction to Algebra Concepts and Skills
Other Miscellaneous Problems
Syllabus for Calculus
SYLLABUS FOR COLLEGE ALGEBRA
Elementary Linear Algebra
Adding and Subtracting Fractions without a Common Denominator
Pre-Algebra and Algebra Instruction and Assessments
Mathstar Research Lesson Plan
Least Common Multiple
Division of Polynomials
Counting Factors,Greatest Common Factor,and Least Common Multiple
Fractions
Real Numbers, Exponents and Radicals
Math 115 Final Exam Review
Root Finding and Nonlinear Sets of Equations
Math 201-1 Final Review Sheet
Powers of Ten and Calculations
Solving Radical Equations
INTERMEDIATE ALGEBRA WITH APPLICATIONS COURSE SYLLABUS
EASY PUTNAM PROBLEMS
INTRODUCTION TO MATLAB
Factoring Polynomials
Section 8
Declining Price, Profits and Graphing
Arithmetic and Algebraic Structures
Locally Adjusted Robust Regression
Topics in Mathematics
INTERMEDIATE ALGEBRA
Syllabus for Mathematics
The Quest To Learn The Universal Arithmetic
Solving Linear Equations in One Variable
Examples of direct proof and disproof
ELEMENTARY ALGEBRA
NUMBER THEORY
Algebra I
Quadratic Functions and Concavity
Algebra
More on Equivalence Relations
Solve Quadratic Equations by the Quadratic Formula
Solving Equations and Inequaliti
MATH 120 Exam 3 Information
Rational Number Ideas and Symbols
Math Review Sheet for Exam 3
Polynomials
Linear Algebra Notes
Factoring Trinomials
Math 097 Test 2
Intermediate Algebra Syllabus
How to Graphically Interpret the Complex Roots of a Quadratic Equation
The General, Linear Equation
Written Dialog for Problem Solving
Radian,Arc Length,and Area of a Sector
Internet Intermediate Algebra
End Behavior for linear and Quadratic Functions
Division of Mathematics
161 Practice Exam 2
Pre-calculus
General linear equations
Algebraic Symmetries
Math 20A Final Review Outline
Description of Mathematics
Math 150 Lecture Notes for Chapter 2 Equations and Inequalities
Course Syllabus for Prealgebra
Basic Operations with Decimals: Division
Mathematics Content Expectations
Academic Systems Algebra Scope and Sequence
Syllabus for Introduction to Algebra
Syllabus for Elementary Algebra
Environmental Algebra
Polynomials
More Math Practice Problems
INTERMEDIATE ALGEBRA COURSE SYLLABUS
Intermediate Algebra
Syllabus for Linear Algebra and Differential Equations
Intermediate Algebra
Rational Expressions and Their Simplification
Course Syllabus for Intermediate Algebra
GRE Review - Algebra
Foundations of Analysis
Finding Real Zeros of Polynomial Functions
Model Academic Standards for Mathematics
Visual-Fraction-Addition-Teaching-Method
Study Guide for Math 101 Chapter 3
Real Numbers
Math 9, Fall 2009, Calendar
Final Review Solutions
Exponential and Logarithmic Functions





Try the Free Math Solver or Scroll down to Tutorials!

 

 

 

 

 

 

 

 
 
 
 
 
 
 
 
 

 

 

 
 
 
 
 
 
 
 
 

Please use this form if you would like
to have this math solver on your website,
free of charge.


INTRODUCTION TO MATLAB

1.5 Pause command
A pause command in a script causes execution to stop temporarily. To continue just hit
Enter. You can also give it a time argument like this

pause(.2)

which will cause the script to pause for 0.2 seconds, then continue. And, of course, you can
ask for a pause of any number or fractions of seconds. Note, however, that if you choose
a really short pause, like 0.001 seconds, the pause will not be so quick. Its length will be
determined instead by how fast the computer can run your script.

1.6 Online Help
If you need to find out about something in Matlab you can use help or lookfor at the
>>prompt. There is also a wealth of information under Help Desk in the Help menu
of Matlab's command window. For example maybe you are wondering about the atan2
function mentioned in Sec. 1.2. Type

help atan2

at the >>prompt to find information about how this form of the inverse tangent function
works. Also type

help bessel

to find out what Matlab's Bessel function routines are called. Help will only work if you
know exactly how Matlab spells the topic you are looking for.

Lookfor is more general. Suppose you wanted to know how Matlab handles elliptic
integrals. help elliptic is no help, but typing

lookfor elliptic

will tell you that you should use

help ellipke

to find what you want.

1.7 Making Matlab Be Quiet
Any line in a script that ends with a semicolon will execute without printing to the screen.
Try, for example, these two lines of code in a script or at the >> command prompt.

a=sin(5),
b=cos(5)

Even though the variable a didn't print, it is loaded with sin (5), as you can see by typing
this:

a

1.8 Debugging
When your script fails you will need to look at the data it is working with to see what went
wrong. In Matlab this is easy because after you run a script all of the data in that script
is available at the Matlab >>prompt. So if you need to know the value of a in your script
just type

a

and its value will appear on the screen. You can also make plots of your data in the
command window. For example, if you have arrays x and y in your script and you want to
see what y(x) looks like, just type plot(x,y) at the command prompt.

The following Matlab commands are also useful for debugging:

who lists active variables
whos lists active variables and their sizes
what lists .m files available in the current directory

This information is also available in the workspace window on your screen and described in
the next section.

1.9 Arranging the Desktop
A former student, Lance Locey, went directly from this Introduction to Matlab to doing
research with it and has found the following way of arranging a few of Matlab's windows on
the desktop to be very helpful. (A visual representation of this layout appears in Fig. 1.1.)

1. Make the command window wide and not very tall, stretching across the bottom of
the desktop.
2. Open a script editing window (click on the open-a- le icon on the tool bar) and place
on the left side, just above the command window.
3. Click on View on the tool bar, then on Workspace to open the workspace window,
and place it at the upper right.

Figure 1.1 A convenient arrangement of the desktop

4. In the command window type a=1 so that you have a variable in the workspace
window, then double click on the yellow name icon for a in the workspace window
to make the Array Editor appear. Then place this window just below the workspace
window and above the command window, at the left side of the desktop (see the next
page.)

With these windows in place you can simultaneously edit your script, watch it run in the
command window, and view the values of variables in the array editor. You will find that
this is very helpful later when our scripts become more complicated, because the key to
debugging is to be able to see the sizes of the arrays that your script is using and the
numerical values that they contain. In the next section you will work through a simple
example of this procedure.

1.10 Sample Script
To help you see what a script looks like and how to run and debug it, here is a simple one
that asks for a small step-size h along the x-axis, then plots the function cos x
from x = 0 to x = 20. The script then prints the step-size h and tells you that it is finished.
The syntax and the commands used in this script are unfamiliar to you now, but you will
learn all about them shortly. Type this script into an M- le called test.m using Matlab's
editor with the desktop arranged as shown on the previous page. Save it, then run it by
typing

test

in the command window. Or, alternatively, press F5 while the editing window is active and
the script will be saved, then executed. Run the sample script below three times using these
values of h: 1, 0.1, 0.01. As you run it look at the values of the variables h, x, and f in the
workspace window at the upper right of the desktop, and also examine a few values in the
array editor just below it so that you understand what the array editor does.

Sample Script

clear, % clear all variables from memory
close all, % close all figure windows

h=input(' Enter the step-size h - ') ,
x=0:h:20, % build an array of points [0,h,2h,...,20]
f=exp(-x/6).*cos(x), % build the array [f(0),f(h),...f(20)]

plot(x,f)

fprintf(' Plot completed, h = %g \n',h)

1.11 Breakpoints and Stepping
When a script doesn't work properly, you need to find out why and then x it. It is very
helpful in this debugging process to watch what the script does as it runs, and to help you
do this Matlab comes with two important features: breakpoints and stepping.

To see what a breakpoint does, put the cursor on the x=0:h:20 line in the sample script
above and either click on Breakpoints on the tool bar and select Set/Clear, or press F12.
Now press F12 repeatedly and note that the little red dot at the beginning of the line toggles
on and o , meaning that F12 is just an on-o switch set set a breakpoint. When the red
dot is there it means that a breakpoint has been set, which means that when the script
runs it will execute the instructions in the script until it reaches the breakpoint, and then
it will stop. Make this happen by pressing F5 and watching the green arrow appear on the
line with the breakpoint. Look at the workspace window and note that h has been given a
value, but that x has not. This is because the breakpoint stops execution just before the
line on which it is set.

Now you can click on the Debug icon on the tool bar to see what to do next, but the
most common things to do are to either step through the code executing each line in turn
(F10) while watching what happens to your variables in the workspace and array editor
windows, or to just continue after the breakpoint to the end (F5.) Take a minute now and
use F10 to step through the script while watching what happens in the other windows.

When you write a script to solve some new problem, you should always step through it
this way so that you are sure that it is doing what you designed it to do. You will have lots
of chances to practice debugging this way as you work through the examples in this book.

Chapter 2
Variables

Symbolic packages like Mathematica and Maple have over 100 different data types, Matlab
has just three: the matrix, the string, and the cell array. In both languages variables are
not declared, but are defined on the y as it executes. Note that variable names in Matlab
are case sensitive, so watch your capitalization. Also: please don't follow the ridiculous
trend of making your code more readable by using long variable names with mixed lowerand
upper-case letters and underscores sprinkled throughout. Newton's law of gravitation
written in this style would be coded this way:

Force_of_1_on_2 = G*Mass_of_1*Mass_of_2/Distance_between_1_and_2^2

You are asking for an early end to your programming career via repetitive-stress injury if
you code like this. Do it this way:

F=G*m1*m2/r12^2

2.1 Numerical Accuracy
All numbers in Matlab have 15 digits of accuracy. When you display numbers to the screen,
like this
355/113

you may think Matlab only works to 5 significant figures. This is not true, it's just displaying
five. If you want to see them all type
format long e

The four most useful formats to set are
format short (the default)
format long
format long e
format short e

Note: e stands for exponential notation.

2.2 π

Matlab knows the number π.
pi

Try displaying π under the control of each of the three formats given in the previous section.
Note: you can set pi to anything you want, like this, pi=2, but please don't.

2.3 Assigning Values to Variables
The assignment command in Matlab is simply the equal sign. For instance,
a=20

assigns 20 to the variable a.

2.4 Matrices
Matlab thinks the number 2 is a 1x1 matrix:
N=2
size(N)

The array
a=[1,2,3,4]
size(a)

is a 1x4 matrix (row vectors are built with commas), the array
b=[1;2;3;4] size(b)

is a 4x1 matrix (column vectors are built with semicolons, or by putting rows on separate
lines-see below.)
The matrix
c=[1,2,3,4,5,6;7,8,9]
size(c)

is a 3x3 matrix (row entries separated by commas, different rows separated by semicolons.)
It should come as no surprise that the Mat in Matlab stands for matrix.

When matrices become large the , and , way of entering them is awkward. A more
visual way to type large matrices in a script is to use spaces in place of the commas and to
press the Enter key at the end of each row in place of the semicolons, like this:

This makes the matrix look so nice in a script that you probably ought to use it exclusively.

When you want to access the elements of a matrix you use the syntax A(row,column).
For example, to get the element of A in the 3rd row, 5th column you would use A(3,5).
And if you have a matrix or an array and you want to access the last element in a row or
column, you can use Matlab's end command, like this:
c(end)
A(3,end)

2.5 Strings
A string is a set of characters, like this
s='This is a string'

And if you need an apostrophe in your string, repeat a single quote, like this:
t='Don''t worry'

And if you just want to access part of a string, like the first 7 characters of s (defined above)
use
s(1:7)

Some Matlab commands require options to be selected or set by using strings. Make
sure you enclose them in single quotes, as shown above. If you want to know more about
how to handle strings type help strings.