How to plot a function using gradient descent method?

70 次查看(过去 30 天)
-

回答(1 个)

Ayush Aniket
Ayush Aniket 2025-1-24,11:38
To plot function and iterative value of the variables in MATLAB using gradient descent, you need to:
  1. Define the function.
  2. Compute the gradient of the function. You can implement this manually or utilize gradient MATLAB function.
  3. Implement the gradient descent algorithm.
  4. Finally, you can plot the gradients.
Refer to the example below:
% Define the function and its gradient
f = @(x) x.^2;
grad_f = @(x) 2*x;
% Gradient descent parameters
alpha = 0.1; % Learning rate
x0 = 10; % Initial guess
max_iter = 100; % Maximum number of iterations
tolerance = 1e-6; % Tolerance for stopping criterion
% Initialize variables
x = x0;
x_history = x; % To store the path
% Gradient descent loop
for iter = 1:max_iter
% Compute the gradient
grad = grad_f(x);
% Update the variable
x = x - alpha * grad;
% Store the history
x_history = [x_history, x];
% Check for convergence
if abs(grad) < tolerance
fprintf('Converged in %d iterations\n', iter);
break;
end
end
Converged in 77 iterations
% Plot the function
x_plot = linspace(-10, 10, 100);
y_plot = f(x_plot);
figure;
plot(x_plot, y_plot, 'b-', 'LineWidth', 2);
hold on;
% Plot the descent path
y_history = f(x_history);
plot(x_history, y_history, 'ro-', 'MarkerFaceColor', 'r');
title('Gradient Descent on f(x) = x^2');
xlabel('x');
ylabel('f(x)');
legend('Function', 'Descent Path');
grid on;
You can use this example and modify it according to your equation.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by