Basic functionality and first part of the report
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
% Define environment (functions, gradients etc...)
|
||||
GivenEnv
|
||||
|
||||
%
|
||||
% We plot the function in the domain of x in [-3, 3] and y in [-3, 3].
|
||||
% We also plot the contour in order to get a senso of the min and maximum
|
||||
% points in the x-y plane
|
||||
%
|
||||
|
||||
% 3d plot the function
|
||||
plot3dFun(fun, [-3, 3], [-3, 3], 100, title_fun, "figures/FunctionPlot.png");
|
||||
|
||||
% Plot isobaric lines
|
||||
plotContour(fun, [-3, 3], [-3, 3], 100, title_fun, "figures/FunctionContour.png");
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 91 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 401 KiB |
@@ -0,0 +1 @@
|
||||
File to keep directory
|
||||
@@ -1,4 +1,4 @@
|
||||
function [x_vals, f_vals, k] = lev_mar(f, grad_f, hessian_f, m, x0, tol, max_iter, mode)
|
||||
function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, m, x0, tol, max_iter, mode)
|
||||
% f: Objective function
|
||||
% grad_f: Gradient of the function
|
||||
% hessian_f: Hessian of the function
|
||||
@@ -1,4 +1,4 @@
|
||||
function [x_vals, f_vals, k] = newton(f, grad_f, hessian_f, x0, tol, max_iter, mode)
|
||||
function [x_vals, f_vals, k] = method_newton(f, grad_f, hessian_f, x0, tol, max_iter, mode)
|
||||
% f: Objective function
|
||||
% grad_f: Gradient of the function
|
||||
% hessian_f: Hessian of the function
|
||||
@@ -1,4 +1,4 @@
|
||||
function [x_vals, f_vals, k] = steepest_descent(f, grad_f, x0, tol, max_iter, mode)
|
||||
function [x_vals, f_vals, k] = method_steepest_descent(f, grad_f, x0, tol, max_iter, mode)
|
||||
% f: Objective function
|
||||
% grad_f: Gradient of the function
|
||||
% x0: Initial point [x0, y0]
|
||||
@@ -1,42 +0,0 @@
|
||||
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
|
||||
@@ -0,0 +1,34 @@
|
||||
function plot3dFun(fun, x_lim, y_lim, size, plot_title, filename)
|
||||
% 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
|
||||
if strcmp(filename, '') == 0
|
||||
print(gcf, filename, '-dpng', '-r300');
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,33 @@
|
||||
function plotContour(fun, x_lim, y_lim, size, plot_title, filename)
|
||||
% plot the contour of 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);
|
||||
|
||||
% Contour
|
||||
figure('Name', 'Contours of f(x,y)', 'NumberTitle', 'off');
|
||||
set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
|
||||
contour(X, Y, Z);
|
||||
|
||||
% Customize the plot
|
||||
xlabel('x'); % Label for x-axis
|
||||
ylabel('y'); % Label for y-axis
|
||||
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
|
||||
colorbar;
|
||||
|
||||
% save the figure
|
||||
if strcmp(filename, '') == 0
|
||||
print(gcf, filename, '-dpng', '-r300');
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
function plotIterationsOverGamma(gamma, iterations, plot_title, filename)
|
||||
function plotItersOverGamma(gamma, iterations, plot_title, filename)
|
||||
% 3D plots a function
|
||||
% fun: The points to plot
|
||||
% contur_fun: The function for contour plot
|
||||
@@ -12,8 +12,14 @@ function plotIterationsOverGamma(gamma, iterations, plot_title, filename)
|
||||
figure('Name', 'Iterations_over_gamma', 'NumberTitle', 'off');
|
||||
set(gcf, 'Position', [100, 100, 960, 960]); % Set the figure size
|
||||
plot(gamma, iterations, '*r', 'LineWidth', 2);
|
||||
|
||||
% Customize the plot
|
||||
title(plot_title, 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
|
||||
xlabel('\gamma') ;
|
||||
ylabel('Iterations');
|
||||
print(gcf, filename, '-dpng', '-r300');
|
||||
|
||||
% save the figure
|
||||
if strcmp(filename, '') == 0
|
||||
print(gcf, filename, '-dpng', '-r300');
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user