Simple Programming in MATLAB PDF
Simple Programming in MATLAB PDF
−0.5
−1
−1.5
−2
−2.5
−3
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
The graph is disappointing. The reason for this strange graph is that we we took a low
value of n. We can take a higher value of n, and repeat all the commands, which will
require a lot of typing. To avoid such situations, we will create and execute a MATLAB
script file, in the next section.
Exercise: Plot the graph of y = sin(x) using 11 equally spaced points in the interval
[0, 2⇡], i.e., with xi = (i 1)h, i = 1, 2, · · · , 11, where h = 2⇡/10.
3
A MATLAB script is an ASCII file, created by the user, which contains a sequence of
MATLAB commands. This file must be saved with an extention “.m”. They are also re-
ferred to as M-files. By typing the name of the script file (without the ‘.m’ extension) at the
command-prompt (>>), we execute the script, i.e., execute all the commands sequentially.
Current Directory: The files created in MATLAB are usually stored in the current
directory (folder) of MATLAB. The current directory is displayed in a window just above
the command window. The default current directory of MATLAB may not be the best
place to save a file for proper organization. For example, we may create a new directory,
called “MatlabTutorial”, make MatlabTutorial as the current directory, and save files in
that directory. One can change the current directory to MatlabTutorial by navigating
through the browse button, located next to the current directory display. A script file may
be executed if it is in the current directory (by changing the MATLAB path, one can access
also other directories).
Creating script file: Select File - New - M-File from the File menu of MATLAB window.
An edit window will appear. Type the appropriate MATLAB commands in the file. In our
case, we type the following:
Now save this script file by selecting File - Save As... frome the File menu of the
edit window. For example, save it as “sc1.m”. Note that we have used n=10, and have
suppressed the output of each command by putting a ‘;’ at the end of the commands. To
execute this script file, we type at the command prompt
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
The graph is still not good. We have to increase the value of n. If you have closed the
edit window, open the file sc1.m by selecting File - Open... and the file sc1.m. Change
n=100, save the file, and execute again. We get the following graph.
1
0.8
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Exercise: Write a MATLAB script to plot the graph of the function y = sin(x) x , 20
x 20. Experiment with the value of n (# of x-values) to get a realistic graph.
5
A function file is just like a script file, but it has well-defined input and output variables.
It is also an M-File. These functions can be used in other scripts. For example, we used
the function
f (x) = e 1.5x sin(8⇡x)
in the script sc1.m, and typed it explicitly. We can define this function in a function file
as follows: First open an edit window and then type
function y = func1(x);
y = exp(-1.5*x) .* sin(8*pi*x);
Save this small file as ‘func1.m’ (use the same name as the name of the function while
saving). Note that the first line of this function file is the function definition line, which
clearly indicates the name of the function and the input/output variables. In this function,
x is the input variable which could be either a scalar or vector. The output variable is y;
it is scalar if x is scalar, and it is a vector if x is a vector. The size of y is same as the size
of x.
The function func1.m can be used in the script sc1.m by slightly changing the script as
follows:
% Script sc2.m; Script to plot a function
% We plot a function in the interval [a,b]
a = 0;
b = 1;
n=100;
x=linspace(a,b,n);
y=func1(x);
plot(x,y)
Save the script as sc2.m. Note that we have used y=func1(x);. Make sure that func1.m
and sc2.m are both in the current directory. Also note that we can assign other values to
the variables a and b (other that 0 and 1 respectively).
In this particular case, we really did not need to define the function func1.m, as it was
a simple function. But functions, used in MATLAB, can be complicated and it is useful
to define functions separately.
6
MATLAB has its own flow-control structures, e.g., (a) the for-loop, (b) the while-loop,
(c) the if-statement, (d) the if-elseif-else statement, and others.
Exercise: For your computer, find the integer i and the real number u = 2 i such that
1 + u 6> 1 (weird – isn’t it?)
8
50
40
30
20
10
0
0 0.5 1 1.5 2 2.5 3 3.5 4
Also the following observation will be useful in writing a MATLAB function for for this
procedure. The (k 1)th term of the infinite series is
x2k 3
ak 1 = ( 1)k .
(2k 3)!
And thus, thus the k th term can be written as
k+1 x2k 1 x2
ak = ( 1) = ak 1 ( 1)
(2k 1)! (2k 1)(2k 2)
We are now ready to write the function.
function y = sinmac(x)
% x could be a vector
ep = 10 ^ (-14);
l=length(x);
for k=1:l
sl = 0;
sn = x(k);
term = x(k);
con = 1;
pm = 1;
while (abs(sn-sl)>ep)
sl = sn;
con = con+2;
pm = -pm;
term = (term.*(x(k) ^ 2))/(con*(con-1));
sn = sn + term*pm;
end
y(k) = sn;
end
We use the following script to use this function.
x = [.5 pi/4 1.2];
sinmac(x)
The output will be
ans =
0.47942553860420 0.70710678118655 0.93203908596723
11
0.8
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
−1
0 1 2 3 4 5 6 7