Beruflich Dokumente
Kultur Dokumente
Variable names ARE case sensitive Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores.
ans Default variable name for results pi Value of inf Infinity NaN Not a number e.g. 0/0 i and j i = j = square root of minus one: (-1) (imaginary number) e.g. sqrt(-1) ans= 0 + 1.0000i realmin The smallest usable positive real number realmax The largest usable positive real number
Matlab has some special (reserved) words that you may not use, for example,
for end if while function return elseif case otherwise switch continue else try catch global persistent break
Variable Basics
>> 16 + 24 ans = 40
no declarations needed
>> product = 16 * 23.24 product = 371.84 >> product = 16 *555.24; >> product product = 8883.8
Intro MATLAB
>> clear clear removes all variables; >> product = 2 * 3^3; clear x y removes only x and >> comp_sum = (2 + 3i) + (2 - 3i); y >> show_i = i^2; complex numbers (i or j) require >> save three_things no special handling >> clear >> load three_things >> who save/load are used to Your variables are: retain/restore workspace comp_sum product show_i variables >> product product = 54 use home to clear screen and put >> show_i cursor at the top of the screen show_i = -1
Intro MATLAB
If you include ; at the end of each statement, result will not be shown immediately
they produce output arguments. Scripts are simply MATLAB commands written into a file. They operate on the existing workspace. Functions accept input arguments and produce output variables. All internal variables are local to the function and commands operate on the function workspace. A file containing a script or function is called an m-file If duplicate functions (names) exist, the first in the search path (from path command) is executed.
Intro MATLAB
function [a b c] = myfun(x, y) Write these two lines to a file b = x * y; a = 100; c = x.^2; myfun.m and save it on MATLABs path
>> myfun(2,3) % called with zero outputs ans = 100 >> u = myfun(2,3) % called with one output u = 100 >> [u v w] = myfun(2,3) % called with all outputs u = 100 v = Any return value which is not stored 6 in an output variable is simply w = discarded 4
Intro MATLAB
Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function is
function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2)
You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name
Examples Write a function : out=squarer (A, ind) Which takes the square of the input matrix if the input indicator is equal to 1 And takes the element by element square of the input matrix if the input indicator is equal to 2
Same Name
Another function which takes an input array and returns the sum and product of its elements as outputs
Intro MATLAB
Intro MATLAB
Plot title
1200
Legend
y axis label
1000
Text
800
Tick-mark
INTENSITY (lux)
400
Data symbol
200
10
12
14
x axis label
16 DISTANCE (cm)
18
20
22
24
Tick-mark label
plot(x,y)
where x is a vector (one dimensional array), and y is a vector. Both vectors must have the same number of elements. The plot command creates a single curve with the the abscissa (horizontal axis) and the (vertical axis).
values on
and
coordinates of the
x and y.
If the values of
y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The spacing (difference) between the elements of x must be such that the plotted curve
will show the details of the function.
A plot can be created by the commands shown below. This can be done in the Command Window, or by writing and then running a script file. >> x=[1 2 3 5 7 7.5 8 10]; >> y=[2 6.5 7 7 5.5 4 6 8]; >> plot(x,y) Once the plot command is executed, the Figure Window opens with the following plot.
plot(x,y,line specifiers)
plot(x,y,line specifiers)
Specifier : --.
Line Specifier Marker Specifier Color Type red green blue Cyan magenta yellow y black r g b c m k plus sign circle asterisk point square diamond d + o * . s
plot(x,y)
plot(x,y,r) plot(x,y,--y)
plot(x,y,*)
plot(x,y,g:d)
The points are marked with * (no line between the points.)
A green dotted line connects the points which are marked with diamond markers.
>> year = [1988:1:1994]; >> sales = [127, 130, 136, 145, 158, 178, 211]; >> plot(year,sales,'--r*')