机械振动加州大学University of CaliforniaMATLAB Commands要点
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MATLAB Commands for Fourier Series
Relational and Logical Operations1
∙Relational Operators
∙Logical Operators
-Logical operators provide a way to combine or negate relational expressions.
-The logical operators make it easy to generate arrays representing signals with discontinuities or signals that are composed of segments of other signals. The
basic idea is to multiply those values in an array that you wish to keep with ones, and multiply all other values with zeros. For example,
» x = linspace(0,10,100); %create data
» y = sin(x); %compute sine
» z = (y >= 0).*y; %set negative values of sin(x) to zero
» z = z + 0.5*(y < 0); %where sin(x) is negative add 1/2
» z = (x <= 8) .*z; %set values past x = 8 to zero
» plot(x,z)
» xlabel('x'), ylabel('z = f(x)')
Control Flow1
FOR LOOPS
-It allows a group of commands to be repeated a fixed, predetermined number of times. The general form of a For Loop is
for x = array
(commands)
end
-For example,
» for n = 1:10
x(n) = sin(n*pi/10);
end
» x
x =
Columns 1 through 7
0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090
Columns 8 through 10
0.5878 0.3090 0.0000
-Any valid MATLAB array is acceptable in the For Loop:
» data = [3 9 45 6;7 16 -1 5]
data =
3 9 45 6
7 16 -1 5
» for n = data
x = n(1) – n(2)
end
x =
-4
x =
-7
x =
46
x =
1
-For Loops should be avoided whenever there is an equivalent array approach to solving a given problem. For example, the first example can be rewritten as » n = 1:10;
» x = sin(n*pi/10)
x =
Columns 1 through 7
0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090
Columns 8 through 10
0.5878 0.3090 0.0000
∙IF-ELSE-END CONSTRUCTION
-The simplest If-Else-End construction is
if expression
(commands)
end
-In case where there are two alternatives, the If-Else-End construction is
if expression
(commands evaluated if True)
else
(commands evaluated if False)
end
-When there are three or more alternatives, the If-Else-End construction takes the form
if expression1
(commands evaluated if expression1 is True)
elseif expression2
(commands evaluated if expression2 is True)
elseif expression3
(commands evaluated if expression3 is True)
elseif …
.
.
.
end
Function M-files1
∙M-File Construction rules:
-The function M-file name and the function name that appears in the first line of the file should be identical. In reality, MATLAB ignores the function name in the first line and executes function based on the file name stored on disk.
-Function M-file names can have up to 31 characters.
-Function names must begin with a letter
-The first line of a function M-file is called the function declaration line and must contain the word function followed by the calling syntax for the function in its
most general form. The input variables identified in the first line are variables
local to the function. The input variables contain data passed to the function and the output variables contain passed back from the function. It is not possible to
pass data back through the input variables.
-The first set of contiguous comment lines after the function declaration line are the help text for the function.
- A function M-file terminates after the last line in the file is executed or whenever
a return statement is encountered.
- A function can abort operation and return control to the command window by calling the function error. This function is useful for flagging improper function
usage:
if length(val) > 1
error(‘VAL must be a scalar.’)
end
∙Input and Output Arguments:
-Function M-files can have zero input and zero output arguments.