Save this file as: myfunc.m in your working directory. This file can now be used in the command window just like any predefined MATLAB function; in the command window enter:
>> x = -2:.1:2; % Produces a vector of x values
>> y = myfunc(x); % Produces a vector of y values
>> plot(x,y)
Note that the fact we used x and y in both the function program and in the command window was just a coincidence. In fact, it is the name of the file myfunc.m that actually mattered, not what anything in it was called. We could just as well have made the function
function nonsense = yourfunc(inputvector)
nonsense = 2*inputvector.^2 - 3*inputvector + 1;
end
Look back at the program. All function programs are like this one, the essential elements are:
Although myfunc returned a value, we did not capture it. By default MATLAB captured it as ans so we can use it in our next computation. However, MATLAB always uses ans (for answer), so the result is likely to get overwritten.
function myfuncnoreturn(x)
y = 2*x.^2 - 3*x + 1
end
and try:
>> myfuncnoreturn(4)
>> ans^2
>> y^2
Although the value of y was printed within the function, it was not returned, so neither the value of y nor the value of ans was changed. Thus we cannot use the result from the function.
In general, the best way to use a function is to capture the result it returns and then use or print this result. Printing within functions is bad form; however, for understanding what is happening within a function it is useful to print, so many functions in this book do print.
Many people use script programs for routine calculations that would require typing more than one command in the command window. They do this because correcting mistakes is easier in a program than in the command window.
For programs that have more than a couple of lines it is important to include comments. Comments allow other people to know what your program does and they also remind yourself what your program does if you set it aside and come back to it later. It is best to include comments not only at the top of a program, but also with each section. In MATLAB anything that comes in a line after a % is a comment.
For a function program, the comments should at least give the purpose, inputs, and outputs. A properly commented version of the function with which we started this section is:
function y = myfunc(x)
% Computes the function 2x^2 -3x +1
% Input: x -- a number or vector;
% for a vector the computation is elementwise
% Output: y -- a number or vector of the same size as x
y = 2*x.^2 - 3*x + 1;
end
For a script program, there should be an initial comment stating the purpose of the script. It is also helpful to include the name of the program at the beginning. For example:
% mygraphs
% plots the graphs of x, x^2, x^3, and x^4
% on the interval [-1,1]
% fix the domain and evaluation points
x = -1:.1:1;
% calculate powers
% x1 is just x
x2 = x.^2;
x3 = x.^3;
x4 = x.^4;
% plot each of the graphs
plot(x,x,'+-',x,x2,'x-',x,x3,'o-',x,x4,'--')
The MATLAB command help prints the first block of comments from a file. If we save the above as mygraphs.m and then do
>> help mygraphs
it will print the first block of comments into the command window:
>> help mygraphs
mygraphs
plots the graphs of x, x^2, x^3, and x^4
on the interval [-1,1]
Write a well-commented function program for the function \(x^{2} e^{-x^2}\text{,}\) using component-wise operations (such as .* and .^). To get \(e^{x}\) use exp(x). Plot the function on \([-5,5]\) using enough points to make the graph smooth. Turn in the program and the graph.
Write a well-commented script program that graphs the functions \(\sin x\text{,}\)\(\sin 2x\text{,}\)\(\sin 3x\text{,}\)\(\sin 4x\text{,}\)\(\sin 5x\) and \(\sin 6x\) on the interval \([0, 2\pi]\) on one plot. (\(\pi\) is pi in MATLAB.) Use a sufficiently small step size to make all the graphs smooth. Turn in the program and the graph.