THMMY's "Optimization Techniques" course assignments.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plotPointsOverContour.m 1.2 KiB

преди 21 часа
12345678910111213141516171819202122232425262728293031323334353637383940
  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. % Generate a grid for x and y
  12. x_space = linspace(x_lim(1), x_lim(2), size);
  13. y_space = linspace(y_lim(1), y_lim(2), size);
  14. [X, Y] = meshgrid(x_space, y_space);
  15. % Evaluate the function on the grid
  16. Z = contour_fun(X, Y);
  17. % 2D plot
  18. figure('Name', '(x,y)', 'NumberTitle', 'off');
  19. set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
  20. plot(points(:, 1), points(:, 2), '-or');
  21. hold on
  22. contour(X, Y, Z);
  23. % Customize the plot
  24. xlim(x_lim);
  25. ylim(y_lim);
  26. xlabel('x'); % Label for x-axis
  27. ylabel('y'); % Label for y-axis
  28. grid on
  29. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  30. colorbar;
  31. % save the figure
  32. if strcmp(filename, '') == 0
  33. print(gcf, filename, '-dpng', '-r300');
  34. end
  35. end