Sie sind auf Seite 1von 30

A Mathematician’s Introduction to

MATLAB
Plan

I can’t teach you how to use Matlab


effectively, but I hope that I can show
you enough so that when you need
Matlab you will know how to start.
Contents

• Basics
• Graphing y=f(x)
• User defined functions (M-file)
• Numerical integration
MATLAB Environment

Command Window

Command
History
Some Important Details
• Everything is a matrix.
• Comma or space separates row elements.
Semicolon separates rows.
• Elements are referred to by indices in
parentheses.
• Assignment uses = (not :=)
• Terminal semicolon (;) suppresses output.
• Variable names are Case sensitive.
• Two sets of arithmetic operations – vector,
element-wise.
• Previous inputs can’t be edited directly.
Practice Entering Some Expressions

>>4+3*sqrt(2)
>>sin(pi/6)
>>i^2
>>j^2
>>x=log(10)
>>y=exp(x)
Recalculation with MATLAB

• To edit and/or recalculate a previous line,


press the up arrow to scroll back through
the worksheet until you find what you want
to recalculate.
• Then press Enter
Entering a matrix and assigning it to a variable name

Remember that row


elements are separated by
comma or space and rows
by semicolon or enter
Try the following.

 1 2 3
B  , X   2 3
 4 5 6

X*B , B*X , pi*X , B^2 , X^2,


X.^2 , Y=X’*X , Y^2, Y.^2,
B.*B , B*B’ , B.*B’ ,
sqrt(X) , exp(X) , cos(X)
Two Kinds of Arithmetic Operations

• Matrix operations * and ^

• Element-wise operations .* and .^


Saving your work
• If you exit MATLAB and then start it
again, the history of your previous work
will appear, but none of your variables will
be known in the workspace.
• You can get the variables back either by
recalculating them or by Saving the
workspace.
Help on how to save workspace
Warning: MATLAB Workspace

If you are working on a computer that is


not your own, and if you are using
MATLAB’s default Work folder, be sure to
save a copy of your saved workspace in
your own file space.
The Graph of y = f(x)

• plot(X,Y)
where X is a vector of domain elements and
Y is the vector of corresponding values.
• First example: y = cos2(x) + 1
• Using X = [2,-3] previously defined, let
Y=(cos(X)).^2+1
How to make a domain vector
• A slick way to make the vector
X = [a, a+h, a+2h, … , b-h, b]
is
>> X=a:h:b;
• Let’s plot y = cos2(x)+1 on [-π,π] with h=π/18
(or make one of your own)
• >> X=-pi:pi/18:pi;
>> Y=(cos(X)).^2+1;
>> plot(X,Y)
Creating an M-file for a function

• User defined functions are created in M-


files.
• File>New>M-File
• function y=f(x)
y=(cos(x)).^2+1;
end
• Since the function is named f it must be
saved with the filename f.m
Numerical Integration

• Let’s compute

0
f ( x)dx
where f is our previously defined function.

• Search Help for integration.

• >> quad(@f,0,pi)
Matlab Conditional Structures
Final Thoughts

• Nobody could or would want to know


everything about MATLAB.
• Start with a particular project or problem.
• Use the Help files.
• Be willing to try something.
• Be prepared for frustration, but if you need
MATLAB it is worth the frustration.

Das könnte Ihnen auch gefallen