THMMY's "Optimization Techniques" course assignments.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

42 строки
1.3 KiB

  1. function plot3dFun(fun, x_lim, y_lim, size, plot_title, filename)
  2. % 3D plots 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. for i = 1:size
  17. for j = 1:size
  18. % Pass each [x1; x2] as input to fun
  19. Z(i, j) = fun([X(i, j); Y(i, j)]);
  20. end
  21. end
  22. % 3D plot
  23. figure('Name', 'f(x,y)', 'NumberTitle', 'off');
  24. set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
  25. surf(X, Y, Z);
  26. % Customize the plot
  27. xlabel('x1'); % Label for x-axis
  28. ylabel('x2'); % Label for y-axis
  29. zlabel('f(x, y)'); % Label for z-axis
  30. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  31. colorbar;
  32. % save the figure
  33. if strcmp(filename, '') == 0
  34. print(gcf, filename, '-dpng', '-r300');
  35. end
  36. end