Page 827 - Mechatronics with Experiments
P. 827
®
®
MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION 813
ans =
-2147483648
>> realmax
ans =
1.7977e+308
>> realmin
ans =
2.2251e-308
>>
√
For complex numbers, the constant “i” and “j” are both defined as square root of −1, −1,
when used in the context of complex variables.
>> z = 2+3∗i
>> z = x+i∗y
>> z = 2+3j
>> z = x+j∗y
>>
>> z=complex(2,3) ; % equivalent to z = 2+3∗i
>>
>> x = real(z) ; % Assign real parf of z to x
>> y = imag(z) ; % Assign imaginary parf of z to y
>>
>> w = conj(z) ; % if z = 2+3∗ i, then w = 2-3 ∗ i
Note that “i” and “j” are commonly used as loop counters in programming languages such
®
as C and Fortran. In MATLAB , if in a program we use “i” or “j” for loop counters or for
any other purpose on the left hand side of an assignment statement, then we have effectively
√
redefined the meaning of it, and hence can no longer be used as a complex number −1
within the scope of its definition. In other words, if “i” and “j” are redefined in an M-script
file with global visibility, it will effect the redefinition of the “i” and ‘j’ as complex number
globally. However, if ’i’ and ’j’ are redefined in a function as local variable, it will only
effect the definition locally within the scope of that function. M-script files, M-function
files, and variable scopes are discussed later in this chapter.
A.1.2 Program Flow Control Statements in MATLAB ®
The following logic and flow control statements, coupled with operators (arithmetic, logical,
®
and relational operators), allows us to code logic into our MATLAB programs in the form
of loops (iterative and conditional loops) and decision blocks,
% Loops: for and while constructs.
for k=1:10 % Iterative loop: execute the loop 10 times.
...
statements % for i= start_value: increment_value :
end_value
... % increment_value is 1 by default if omitted
end
% Conditional loop:
% Execute the loop while the (condition) is true or non-zero