THMMY's "Optimization Techniques" course assignments.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plotPointsOverContour.m 1.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function plotPointsOverContour(points, contour_fun, x_lim, y_lim, size, plot_title, filename)
  2. % 3D plots a function
  3. % points: The points to plot
  4. % contur_fun: The function for contour plot
  5. % x_lim: The range for x axis. ex: [-2, 2]
  6. % y_lim: The range for y axis. ex: [0, 2]
  7. % size: The number of points for each axis
  8. % plot_title: The latex title for the plot
  9. % filename: The filename to save the plot (if exists)
  10. %
  11. global image_width,
  12. global image_height;
  13. % Generate a grid for x and y
  14. x_space = linspace(x_lim(1), x_lim(2), size);
  15. y_space = linspace(y_lim(1), y_lim(2), size);
  16. [X, Y] = meshgrid(x_space, y_space);
  17. % Evaluate the function on the grid
  18. for i = 1:size
  19. for j = 1:size
  20. % Pass each [x1; x2] as input to fun
  21. Z(i, j) = contour_fun([X(i, j); Y(i, j)]);
  22. end
  23. end
  24. % 2D plot
  25. figure('Name', '(x1,x2) convergence', 'NumberTitle', 'off');
  26. set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
  27. plot(points(1, :), points(2, :), '-or');
  28. hold on
  29. contour(X, Y, Z);
  30. % Customize the plot
  31. xlim(x_lim);
  32. ylim(y_lim);
  33. xlabel('x1'); % Label for x-axis
  34. ylabel('x2'); % Label for y-axis
  35. grid on
  36. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  37. colorbar;
  38. % save the figure
  39. if strcmp(filename, '') == 0
  40. print(gcf, filename, '-dpng', '-r300');
  41. end
  42. end