Start a matlab function with a timer

2 views (last 30 days)
Uche Agbogwu
Uche Agbogwu on 7 Jul 2022
Hi,
I would like to run a matlab function(integral2) to compute a 2D integral. Unfortunately, when I use tight error restrictions, it takes long to converge. What I intend to do is;
  1. create an error matrix (here errMat) of possible errors
  2. begin the computation using a time limit of 60 seconds
  3. if integral2 converges before the 60 seconds are up, the loop breaks (hence the commented if statement,. I would like to store the m, so I know what error was successful
  4. if integral2 does not converge, it selects a smaller error and so on until I get convergence in under 60 seconds.
I tried the etime suggesst in a previous post, but it does not seem to exit the loop after 60 seconds even when there is no conergence. Is my idea even plausible or should I abandon hope? thank you. I have posted only a snippet of the entire code, but:
  • surfFunc is a surfceIntrpolanobject, but I assum and 2D function will work
  • xInterval and yInterval are basically vectors containing xmi, xmax, ymin, ymax
errMat = [1e-10 1e-8 1e-8 1e-6 1e-6 1e-4; 1e-6 1e-6 1e-4 1e-4 1e-2 1e-2];
m = 1;
% while m < length(errMat) + 1
initTime = clock;
tLimit = 60;
% tic
while etime(clock,initTime) < tLimit + 1
kL(ii,l) = integral2(surfFunc,xInterval(ii,l,1),xInterval(ii,l,2),...
yInterval(ii,l,1),yInterval(ii,l,2),'Method','iterated','AbsTol',errMat(1,m), 'RelTol',errMat(2,m) );
% if kL(ii,l) ~= 0
% break
% end
end
% if kL(ii,l) ~= 0
% errFin(ii,l,k) = m;
% break
% end
% m = m + 1
% end
timeM(ii,l,k) = toc;
% timeM(ii,l,k) = toc;
% if k(ii,l) == 0
% fprintf('slow convergence, lowering expectations...')
%
% break
% else
%
% end
% end

Answers (1)

Oluwatosin Adeshokan
Oluwatosin Adeshokan on 17 Jan 2023
It is possible to implement a loop that checks for convergence within a time limit using the tic and toc functions in MATLAB. The tic function starts a timer and the toc function returns the elapsed time since the timer was started. You can use this to check if the integral computation has taken longer than your desired time limit and then adjust the error tolerance accordingly. Here is an example of how you can implement this in your code:
errMat = [0.1, 0.01, 0.001, 0.0001]; m = 1; timeLimit = 60; tic; while 1 [integralVal,err] = integral2(surfFunc, xInterval(1), xInterval(2), yInterval(1), yInterval(2), 'RelTol', errMat(m)); if toc > timeLimit m = m + 1; else break; end end
This code will start a timer using tic and then enter a while loop. Within the loop, it will compute the integral using the integral2 function with the current error tolerance from the errMat array. If the elapsed time since starting the timer is greater than the time limit (60 seconds), it will increase the error tolerance by incrementing the m variable. If the elapsed time is less than the time limit, the loop will break and the last successful error tolerance will be stored in the variable m. Please note that this is a simplified example, and you might want to add some more conditions like checking if m is smaller than the length of errMat and if not break the loop. You may also want to consider using the 'AbsTol' option instead of 'RelTol' or both in integral2 function, this options allows you to specify an absolute error tolerance which can be useful in cases where the function being integrated has very small values.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!