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.
 
 

42 line
1.4 KiB

  1. function plot3Dfun(fun, x_lim, y_lim, size, plot_title)
  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. % Generate a grid for x and y
  10. x_space = linspace(x_lim(1), x_lim(2), size);
  11. y_space = linspace(y_lim(1), y_lim(2), size);
  12. [X, Y] = meshgrid(x_space, y_space);
  13. % Evaluate the function on the grid
  14. Z = fun(X, Y);
  15. % 3D plot
  16. figure('Name', 'f(x,y)', 'NumberTitle', 'off');
  17. set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
  18. surf(X, Y, Z);
  19. % Customize the plot
  20. xlabel('x'); % Label for x-axis
  21. ylabel('y'); % Label for y-axis
  22. zlabel('f(x, y)'); % Label for z-axis
  23. title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  24. colorbar;
  25. % save the figure
  26. print(gcf, 'FunctionPlot.png', '-dpng', '-r300');
  27. % Contours
  28. figure('Name', 'Contours of f(x,y)', 'NumberTitle', 'off');
  29. set(gcf, 'Position', [100, 100, 1280, 1280]); % Set the figure size
  30. contour(X, Y, Z);
  31. xlabel('x'); % Label for x-axis
  32. ylabel('y'); % Label for y-axis
  33. title(plot_title, 'Interpreter', 'latex', 'FontSize', 20); % Title of the plot
  34. colorbar;
  35. print(gcf, 'ContoursPlot.png', '-dpng', '-r300');
  36. end