Why does this code give error?

I want to implenet timeit() function to find the machine time for the execution of the m-file 'fpa1.m'. But when I run the attached 'main.m', it gives me the following error:
Error using timeit
First argument must be a function handle that takes no input argument.
Error in main (line 6)
fprintf('time taken by fpa1 = %e seconds', timeit(@fpa1,3))

 Accepted Answer

fcn = @() fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
timeit(fcn, 3)

10 Comments

Thanks a lot dear Walter Roberson for your kind response. And if fpa1 has three outputs like this:
[best,fmin,time]=fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
then how can we use timeit() function?
Regardless of the number of outputs, the code to use timeit() will be the same as Walter mentioned.
Thanks a lot dear Dyuman Joshi for your kind response. You are right. But what I mean is that I want to get the outputs also along with the time i.e., I want to get the two outputs best and fmin also along with time. Then how will we do that?
You want the time taken by the code to run as an output to the code? or is time an output variable from fpa1?
You cannot get the outputs when you do timing using timeit(). timeit() tries to run the code several times to reduce the error estimates of the time.
Thanks a lot dear Dyuman Joshi and Walter Roberson for your kind responses.
Reply to Dyuman Joshi : You can remove time from output arguments when timeit is there i.e.,
[best,fmin]=fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
and find these two outputs as well as the time from timeit() function.
Reply to Walter Roberson: You mean that 1st we will estimate the outputs and then we will have to run it separately for estimating time?
You can use
start = tic;
[best,fmin,time]=fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
duration = toc(start);
to time how long the call took.
The purpose of using timeit() is to try to estimate the execution time more reliably. When you time something using a single tic/toc run, then the situation could be anywhere from "the code ran faster than typical" up to "oh, bad luck, the defragmenter decided to run at the same time that the anti-virus was scanning the drive at the same time someone was emailing you some meme videos." timeit() runs the code several times to try to estimate the amount of time it would take the code to run on a system that was not being interrupted with side quests.
Thanks a lot dear Walter Roberson for your kind response. I had used 'tic-toc' inside 'fpa1.m' but its time was not accurate as can be seen at the following URL:
Then someone told me to use 'timeit()'. It gives the accurate time. As you can see at the above URL, I am estimating the values of vector u using fpa1. There are three cases namely when u has 2 values, when u has 3 values and when u has 4 values. 'fpa1' estimates all the cases. But there ,as you can see, when u has 2 values, fpa1 takes less time as compared to the case when u has 3 values and logically its correct also. But when u has 3 values, then fpa1 takes more time as compared to the case where u has 4 values which is wrong. That's why I tried to use timeit() function.
Now I am stuck what to do? If I use 'tic-toc', then I face that problem but when I want to use timeit() then you say, we cannot estimate the output and time simultaneously. Then what should I do next? Any solution?
The original version of the timeit() function is in the File Exchange. You could load it from there and modify it.
But suppose it gives different output results on different timing iterations, then which version should be output?
Thanks a lot dear Walter Roberson for your kind response. Actually, I have already seached there, the code of the timeit() has been removed from the File Exchange. So, the conclusion is that first, I will run the code for the estimation of outputs and then I will run the code for timeit() because the 'tic-toc' gives wrong timing for estimating different number of values. Thanks a lot once again dear Walter Roberson for your kind response.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!