MATLAB's definition is
afun = @(x,n)gallery('moler',n)*x;% returns A*x
So you can pass as an argument the size n
of the matrix. If you would run the code for different values of n, you simply need to declare the n and run the code, like:
for n1 = 10:21
% do something
b1 = afun(ones(n1,1),n1);
[x1,flag1,rr1,iter1,rv1] = pcg(@(x)afun(x,n1),b1,tol,maxit,M);
% do something
end
Your definition
afun = @(x)gallery('moler',n2)*x;
clearly specifies to use n2=21
, so it will create a matrix 21x21, and only and exactly a matrix 21x21. If you need to run it for different values of n
, you will have to define the function for every different n
.
for n2 = 10:21
% do something
afun = @(x)gallery('moler',n2)*x;% returns A*x
b2 = afun(ones(n2,1));
[x2,flag2,rr2,iter2,rv2] = pcg(afun,b2,tol,maxit,M);
% do something
end
In the simple example above, the code will declare and create the afun
variable on every loop on the for, while in MATLAB's example, the afun
function is declared only once.
Edit: added execution time.
MATLAB has a good discussion on how to measure the performance of your code, but essentially you can use the timeit or the tic toc functions.
To measure part of the code, you can use the tic
/toc
, but the recommendation to measure execution time of functions is to use timeit
. The doc reads:
Unlike tic and toc, the timeit function calls your code multiple
times, and, therefore, considers first-time costs.
Let's then rewrite our code to check execution time. First, define the functions:
function [] = matlabfun(num)
%% matlab's definition
tol = 1e-6; maxit = 1500;
afun = @(x,n)gallery('moler',n)*x;
for n1 = 10:num
% do something
b1 = afun(ones(n1,1),n1);
[x1,flag1,rr1,iter1,rv1] = pcg(@(x)afun(x,n1),b1,tol,maxit);
% do something
end
end
function [] = myfun(num)
%% my definition
tol = 1e-6; maxit = 1500;
for n2 = 10:num
% do something
afun = @(x)gallery('moler',n2)*x;% returns A*x
b2 = afun(ones(n2,1));
[x2,flag2,rr2,iter2,rv2] = pcg(afun,b2,tol,maxit);
% do something
end
end
Then write your main script to measure the execution time of each function:
clc;clear;
num = 4e+2;%number of tests
matlab_fun = @() matlabfun(num);
my_fun = @() myfun(num);
timeit(matlab_fun)
timeit(my_fun)
The results are:
ans =
19.9332
ans =
19.6177
So, it looks like your definition runs faster than MATLAB's definition. In my computer, though, the difference is not that big.
However, and this I cannot explain for sure why it happens, if you change the order of the statements, you will get a different response:
clc;clear;
num = 4e+2;%number of tests
matlab_fun = @() matlabfun(num);
my_fun = @() myfun(num);
timeit(my_fun)
timeit(matlab_fun)
The results are:
ans =
19.5554
ans =
19.4029
Now it looks like MATLAB's definition is faster. The reason for that, and this is just my personal thoughts, is that the first call to the gallery
/pcg
functions takes longer than the subsequent calls. As if MATLAB has to figure out the path/library/toolbox for these functions and, since it is a first call, it runs some pieces of code to prepare your computer to run it.
This is something I have noticed along the years programming in MATLAB, the first call to a function always takes longer than the rest. So, whichever definition you put first in your code to run is the one that will take longer. I still haven't found a good explanation to why that happens, but it is how MATLAB seems to work. But, hey, just don't worry to much about that.