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.

37 lines
1.1 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. Z = fun(X, Y);
  17. % 3D plot
  18. figure('Name', 'f(x,y)', 'NumberTitle', 'off');
  19. set(gcf, 'Position', [100, 100, image_width, image_height]); % Set the figure size
  20. surf(X, Y, Z);
  21. % Customize the plot
  22. xlabel('x'); % Label for x-axis
  23. ylabel('y'); % Label for y-axis
  24. zlabel('f(x, y)'); % Label for z-axis
  25. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  26. colorbar;
  27. % save the figure
  28. if strcmp(filename, '') == 0
  29. print(gcf, filename, '-dpng', '-r300');
  30. end
  31. end