Matlab Fonnction PDF
Matlab Fonnction PDF
الجامعة التكنلوجية
العمليات الكيمياوية
MATLAB FUNCTION
To open a new m-file: In the MATLAB command window, go to FILE on the toolbar, select NEW,
then select M-FILE. This opens the MATLAB editor/debugger and gives an empty file in which you
can create whatever m-file you want.
where Out_1,Out_2,…,Out_N are the N output variables and In_1,In_2,…,In_M are the M
input variables;
function function_name(In_1,In_2,…,In_M)
1
Code in an m-file will be shown here using the Arial font, e.g., x = cos(0.1*pi*n)
1
When you are writing the lines that make up your function you can use the names of the input
variables defined in the first line just like they are previously created variables. So if In_1 is one of
the input variables you could then do something like this:
y=In_1.^2;
This takes the values in In_1, squares them, and assigns the result to the variable y.
Note: putting a semicolon at the end of an expression stops the display of the result – a good idea
unless you really WANT to see the result (sometimes useful when debugging or verifying a
function).
Out_1=cos(y);
will compute the cosine of the values in the variable y and then assigns the result to the variable
Out_1, which will then be output by the function (assuming that Out_1 was specified as an output
variable name).
2
>> [Out_1,Out_2,…,Out_N] = function_name(In_1,In_2,…,In_M)
Then, once you have it running without any syntax errors or warnings, you need to test it to verify
that it really does what you intended it to do. Obviously, for simple functions you may be able to
verify that it works by running a few examples and checking that the outputs are what you expect.
Usually you need to do quite a few test cases to ensure that it is working correct. For more complex
functions (or when you discover that the outputs don’t match what you expect) you may want to
check some of the intermediate results that you function computes to verify that they are working
properly. To do this you set “breakpoints” that stop the operation of the function at a specified line
and allow you then view from the command:
Once you have it stopped, any variable that has been computed up to that point can be inspected (plot
it, look at its values, check its size, check if it is row or column, etc. You can even modify a
3
variable’s contents to correct a problem). Note that the ONLY variable you have access to are those
that have been created inside the function or have been passed to it via the input arguments.
Note that you can set multiple breakpoints at a time – once you have stopped at the first one you can
click on DEBUG and then click on CONTINUE and it will pick up execution immediately where it
left off (but with the modified variables if you changed anything).
Note also that once you have a function stopped in debug mode you can “single-step” through the
next several lines: click on DEBUG and click on SINGLE STEP.
3. It is often helpful to put several comment lines right after the function definition line. These
comments explain what the function does, what the inputs and outputs are, and how to call the
function.
4. Putting in lots of comments helps you and others understand what the function does – from a
grading point of view you will have a higher probability of getting full credit if you write
comments that tell me what your code is doing
An Example
Here is an example of a MATLAB function. Suppose you would like to write a general routine that
will compute the value of some arbitrary polynomial at an arbitrary set of equally-spaced x values.
This turns out to be something that is best done using loops (or at least, I couldn’t find a clever way
to avoid loops – this is good because it at least gives an example of how to do loops). The function
might look like this:
function [y,x] = poly_equi(power,coeffs,xmin,xmax,del_x)
%
% USAGE: [y,x] = poly_equi(power,coeffs,xmin,xmax,del_x);
%
% Inputs: power = an integer specifying the highest power of the polynomial
% coeffs = a row vector of the polynomial's coefficients
% xmin = the desired minimum x value for evaluation
% xmax = the desired maximum x value for evaluation %
del_x = the desired spacing between the x values
4
%
% Output: x = a row vector of the x values
% y = a row vector of the y values
%
% Operation: if coeffs = [C_0 C_1 C_2 ... C_p], where p = power, this function
computes
% In equation form, not in matlab syntax: y = C_0 + C_1x + C_2x^2 + ... +
C_px^p
» [y_0,x_0] = poly_equi(0,3,-5.1,5.0,0.2);
» [y_1,x_1] = poly_equi(1,[3 2],-5.1,5.0,0.2);
» [y_2,x_2] = poly_equi(2,[3 2 2],-5.1,5.0,0.2);
» subplot(3,1,1) »
plot(x_0,y_0)
» xlabel('x values')
» ylabel('y values') »
subplot(3,1,2) »
plot(x_1,y_1)
» xlabel('x values') »
ylabel('y values') »
subplot(3,1,3) »
plot(x_2,y_2)
» xlabel('x values')
» ylabel('y values')
Note that we ran the function three times – each time we put in different values for the input
variables and called the output variables something different each time so that we could store the
three different results. We then plotted the results on three different subplots and labeled the axes.
5
And label the UNITS of the values if it makes sense!!!!!!
References
• Gilat, Amos (2004). MATLAB: An Introduction with Applications 2nd Edition. John Wiley & Sons. ISBN 978-0-
471-69420-5.
• Quarteroni, Alfio; Saleri, Fausto (2006). Scientific Computing with MATLAB and Octave. Springer. ISBN 978-3-
540-32612-0.
• Ferreira, A.J.M. (2009). MATLAB Codes for Finite Element Analysis. Springer. ISBN 978-1-4020-9199-5.
• Lynch, Stephen (2004). Dynamical Systems with Applications using MATLAB. Birkhäuser. ISBN 978-0-8176-
4321-8.