- Add output parameter to methods for objective function calls - Adjust the code to reduce objective function calls to some methods
		
			
				
	
	
		
			65 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Matlab
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Matlab
		
	
	
	
	
	
| %
 | |
| % Calculate and plot the iteration needed for different epsilon values,
 | |
| % keeping lambda (accuracy) fixed.
 | |
| %
 | |
| 
 | |
| 
 | |
| % Load the functions and interval
 | |
| GivenEnv;
 | |
| 
 | |
| fig_dir = 'figures';
 | |
| if ~exist(fig_dir, 'dir')
 | |
|     mkdir(fig_dir);
 | |
| end
 | |
| 
 | |
| % Setup
 | |
| % ========================
 | |
| 
 | |
| % lambda = 0.01
 | |
| % epsilon: e < lambda/2 = 0.005
 | |
| % de: A small step away from zero and lambda/2
 | |
| %     de = 0.0001
 | |
| % N: 50 points (50 epsilon values)
 | |
| 
 | |
| N = 50;
 | |
| lambda = 0.01;
 | |
| de = 0.0001;
 | |
| epsilon = linspace(de, (lambda/2)-de, N);
 | |
| k = zeros(1,N); % preallocate k
 | |
| n = zeros(1,N); % preallocate n
 | |
| 
 | |
| 
 | |
| %
 | |
| % Call the min_bisection method for each epsilon value for each
 | |
| % function and keep the number of iterations needed.
 | |
| % Then plot and save the # of iterations k(epsilon) for each function.
 | |
| %
 | |
| 
 | |
| figure('Name', 'iterations_over_epsilon_min_bisection', 'NumberTitle', 'off');
 | |
| set(gcf, 'Position', [100, 100, 1280, 600]); % Set the figure size to HD
 | |
| 
 | |
| for i = 1:length(funs)
 | |
|     for j = 1:N
 | |
|         [a, b, k(j), n(j)] = min_bisection(funs(i), a_0, b_0, epsilon(j), lambda);
 | |
|     end
 | |
|     fprintf('%20s(%34s ):  [a, b]= [%f, %f], iters(min, max)= (%d, %d), calls(min, max)= (%d, %d)\n', ...
 | |
|             "min_bisection", char(funs(i)), a(end), b(end), k(1), k(N), n(1), n(N) );
 | |
|     subplot(1, length(funs), i);
 | |
|     plot(epsilon, n, '-b', 'LineWidth', 1.0);
 | |
|     title(titles(i), 'Interpreter', 'latex', 'FontSize', 16);
 | |
|     xlabel('epsilon');
 | |
|     ylabel("Calls of f" + i);
 | |
| end
 | |
| 
 | |
| %
 | |
| % Print and save the figures
 | |
| %
 | |
| %fig_epsc = fullfile(fig_dir, "iter_over_epsilon_min_bisection" + ".epsc");
 | |
| fig_png  = fullfile(fig_dir, "iter_over_epsilon_min_bisection" + ".png");
 | |
| 
 | |
| %print(gcf, fig_epsc, '-depsc', '-r300');
 | |
| print(gcf, fig_png, '-dpng', '-r300');
 | |
| 
 | |
| close(gcf);
 | |
| 
 |