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.

plotContour.m 1.1 KiB

3 giorni fa
3 giorni fa
123456789101112131415161718192021222324252627282930313233343536
  1. function plotContour(fun, x_lim, y_lim, size, plot_title, filename)
  2. % plot the contour of a function
  3. % fun: The function to plot
  4. % x_lim: The range for x axis. ex: [-2, 2]
  5. % y_lim: The range for y axis. ex: [0, 2]
  6. % size: The number of points for each axis
  7. % plot_title: The latex title for the plot
  8. %
  9. global image_width,
  10. global image_height;
  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 = fun(X, Y);
  17. % Contour
  18. figure('Name', 'Contours of f(x,y)', 'NumberTitle', 'off');
  19. set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
  20. contour(X, Y, Z);
  21. % Customize the plot
  22. xlabel('x'); % Label for x-axis
  23. ylabel('y'); % Label for y-axis
  24. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  25. colorbar;
  26. % save the figure
  27. if strcmp(filename, '') == 0
  28. print(gcf, filename, '-dpng', '-r300');
  29. end
  30. end