Introduction - To - Matlab Programming
Introduction - To - Matlab Programming
MATLAB PROGRAMMING
Engr. Annalyn D. Soria
Script Files
A script file is a list of MATLAB command
lines that you define for your own purposes.
The family name (extension) of a script file is
.M, which is the reason it is often called an M-
file.
Before you use a script file for the first time
you have to specify the path to that file.
File Creation
From the MATLAB Command Window, select
File, New, M-file.
Type the following:
% ex81.m: Test of angles corresponding to sin(x)
format long
x=0.4, s=sin(x)
% Alternative angles...
a1=asin(s), a2=pi-a1, a3=a1-2*pi, a4=a1+2*pi
% Check these angles...
s1=sin(a1), s2=sin(a2), s3=sin(a3), s4=sin(a4)
save the above list by selecting File, Save As
In order to run the file, execute the file by
entering its name after the MATLAB prompt.
One advantage of using a script file is the
overview it brings. In addition one can make
the list easier to understand at a later time by
adding comments.
Finally, the file may serve as a starter for a
longer sequence of calculations.
% ex82.m: Triangle with Two Sides and One Angle Known
format long
disp('The two sides and the angle:')
a=4.1, b=9.1, A=20/180*pi
disp('Use the sine theorem to obtain sin(B), then B and C')
sB=b*sin(A)/a, B=asin(sB), C=pi-A-B
disp('Calculate the side c by the sine theorem')
c=a*sin(C)/sin(A)
disp('Convert the angles to degrees')
Adg=A/pi*180, Bdg=B/pi*180, Cdg=C/pi*180
disp('Repeat to find the second solution')
a=4.1, b=9.1, A=20/180*pi
sB=b*sin(A)/a, B=pi-asin(sB), C=pi-A-B
disp('Convert the angles to degrees')
Adg=A/pi*180, Bdg=B/pi*180, Cdg=C/pi*180
The command disp is used to display the
comments within quotes, and these
comments will be visible on the screen, as
well as serving as reminders in the file.
The option echo on would duplicate these
comments
Function Files
An inline function is stored in primary memory
and only remains valid during the MATLAB
session where it was defined.
The function declaration should stand alone
on its line, since the program does not
continue to read beyond the declaration
statement.
The difference between a script file and a
function file is that the latter accepts
arguments in parentheses after the name.
Example:
% gaussian.m: Gaussian Function with a
Parameter
function F= gaussian(X,a);
F=exp(-X.^2/a^2);
After saving this file, you will be able to call
the function gaussian from any script file.
MATLAB assumes the file name as the name
for the function.
Ex.
X=0:0.5:3, Y=gaussian(X,1)
Function File Yielding Two Output Vectors
A curve may also be described by two
separate functions
where the parameter t is understood to run
through a range of values.
example
% ellipse.m: Function Defining an Ellipse
function [X,Y]=ellipse(T, ax, ay, t0); %
Comma between X and Y
X=ax*cos(T);
Y=ay*cos(T-t0);
Notice that a comma between X and Y is
required in a function definition.
Let us now test this function with different
arguments by the following script file, which
largely speaks for itself. The plot now must
handle three pairs of vectors.
% ex97.m: Plot Ellipses
T=0:1e-3:2*pi; % Values for common variable
[X1 Y1]=ellipse(T, 2, 1, 0);
[X2 Y2]=ellipse(T, 2, 1, pi/3);
[X3 Y3]=ellipse(T, 2, 1, pi/2);
figure(1), plot(X1,Y1, X2,Y2, X3,Y3), grid on, axis equal
legend(t0=0, t0=pi/3, t0=pi/2)