Beruflich Dokumente
Kultur Dokumente
MATLAB
Plan
• 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
1 2 3
B , X 2 3
4 5 6
• 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
• Let’s compute
0
f ( x)dx
where f is our previously defined function.
• >> quad(@f,0,pi)
Matlab Conditional Structures
Final Thoughts