Saturday 3 November 2012

1.6 M-FILES


Normally, when single line commands are entered, MATLAB processes the
commands immediately and displays the results. MATLAB is also capable of
processing a sequence of commands that are stored in files with extension m.
MATLAB files with extension m are called m-files. The latter are ASCII text
files, and they are created with a text editor or word processor. To list m-files
in the current directory on your disk, you can use the MATLAB command
what. The MATLAB command, type, can be used to show the contents of a
specified file. M-files can either be script files or function files. Both script
and function files contain a sequence of commands. However, function files
take arguments and return values.

1.6.1 Script files
Script files are especially useful for analysis and design problems that require
long sequences of MATLAB commands. With script file written using a text
editor or word processor, the file can be invoked by entering the name of the
m-file, without the extension. Statements in a script file operate globally on
the workspace data. Normally, when m-files are executing, the commands are
not displayed on screen. The MATLAB echo command can be used to view
m-files while they are executing. To illustrate the use of script file, a script
file will be written to simplify the following complex valued expression z.
Example 1.2
Simplify the complex number z and express it both in rectangular and polar
form.
z
j j
j j
=
+ + ∠
+ +
( )( )( )
( )( )
3 4 5 2 2 60
3 6 1 2
0
Solution:
The following program shows the script file that was used to evaluate the
complex number, z, and express the result in polar notation and rectangular
form.
MATLAB Script
diary ex1_2.dat


% Evaluation of Z
% the complex numbers are entered
Z1 = 3+4*j;
Z2 = 5+2*j;
theta = (60/180)*pi; % angle in radians
Z3 = 2*exp(j*theta);
Z4 = 3+6*j;
Z5 = 1+2*j;
% Z_rect is complex number Z in rectangular form
disp('Z in rectangular form is'); % displays text inside brackets
Z_rect = Z1*Z2*Z3/(Z4+Z5);
Z_rect
Z_mag = abs (Z_rect); % magnitude of Z
Z_angle = angle(Z_rect)*(180/pi); % Angle in degrees
disp('complex number Z in polar form, mag, phase'); % displays text
%inside brackets
Z_polar = [Z_mag, Z_angle]
diary
The program is named ex1_2.m. It is included in the disk that accompanies
this book. Execute it by typing ex1_2 in the MATLAB command window.
Observe the result, which should be
Z in rectangular form is
Z_rect =
1.9108 + 5.7095i
complex number Z in polar form (magnitude and phase) is
Z_polar =
6.0208 71.4966


No comments:

Post a Comment