function plot3Dfun(fun, x_lim, y_lim, size, plot_title) % 3D plots a function % fun: The function to plot % x_lim: The range for x axis. ex: [-2, 2] % y_lim: The range for y axis. ex: [0, 2] % size: The number of points for each axis % plot_title: The latex title for the plot % % Generate a grid for x and y x_space = linspace(x_lim(1), x_lim(2), size); y_space = linspace(y_lim(1), y_lim(2), size); [X, Y] = meshgrid(x_space, y_space); % Evaluate the function on the grid Z = fun(X, Y); % 3D plot figure('Name', 'f(x,y)', 'NumberTitle', 'off'); set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size surf(X, Y, Z); % Customize the plot xlabel('x'); % Label for x-axis ylabel('y'); % Label for y-axis zlabel('f(x, y)'); % Label for z-axis title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot colorbar; % save the figure print(gcf, 'FunctionPlot.png', '-dpng', '-r300'); % Contours figure('Name', 'Contours of f(x,y)', 'NumberTitle', 'off'); set(gcf, 'Position', [100, 100, 1280, 1280]); % Set the figure size contour(X, Y, Z); xlabel('x'); % Label for x-axis ylabel('y'); % Label for y-axis title(plot_title, 'Interpreter', 'latex', 'FontSize', 20); % Title of the plot colorbar; print(gcf, 'ContoursPlot.png', '-dpng', '-r300'); end