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.

plot3dFun.m 1.1 KiB

2 days ago
2 days ago
12345678910111213141516171819202122232425262728293031323334353637
  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