This script demonstrates anonymous functions.
function inductance = getInductance(I, ri, ro1, ro2) % The function gives back a function to calculate the inducance of a coaxial % cable depending on distance from the middle of the cable (radius x). % The function is initialized with its geometry and a steady current. % % (c)2010 by Torsten Reuschel, E-Mail: matlabhelp@fuesika.de % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see <http://www.gnu.org/licenses/>. inductance = @(x) coax_inductance(x); function y = coax_inductance(x) if (0 <= x ) && (x <= ri) y = 4*pi*1e-7 * I*x / (2*pi*ri^2); elseif (ri <= x ) && (x <= ro1) y = 4*pi*1e-7 *I /(2*pi*x); elseif (ro1 <= x ) && (x <= ro2) y = 4*pi*1e-7 * I/(2*pi*x) * (ro2^2-x^2) / (ro2^2-ro1^2); else y = 0; end end end
When using anonymous functions one should be aware of when and where which information is stored.
Given a matrix A with 10000 by 10000 elements we might want to use an anonymous function that stores this matrix for solving reasons.
Without copying the matrix A we can create more than one function-handle using A.
As soon as we change the content of the matrix A, Matlab makes sure the existing function-handles are still able to use the old matrix.
Matlab will store an copy of the old A - even if the new matrix (or handle) is never used.
If we update the old function-handles, Matlab will clear the old A as soon as no more handle is using it.
This can cause unintentional copying of a serious amount of data.
In case the update is supposed to be available to the function-handles, one should clear the old handles before updating A to keep Matlab from copying the old data.