THMMY's "Optimization Techniques" course assignments.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

43 lines
1.4 KiB

  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. Z = contour_fun(X, Y);
  19. % 2D plot
  20. figure('Name', '(x,y) convergence', 'NumberTitle', 'off');
  21. set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
  22. plot(points(:, 1), points(:, 2), '-or');
  23. hold on
  24. contour(X, Y, Z);
  25. % Customize the plot
  26. xlim(x_lim);
  27. ylim(y_lim);
  28. xlabel('x'); % Label for x-axis
  29. ylabel('y'); % Label for y-axis
  30. grid on
  31. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  32. colorbar;
  33. % save the figure
  34. if strcmp(filename, '') == 0
  35. print(gcf, filename, '-dpng', '-r300');
  36. end
  37. end