How to plot a function using gradient descent method?

69 ビュー (過去 30 日間)
Bijoya Bhattacharjee
Bijoya Bhattacharjee 2021 年 12 月 2 日
回答済み: Ayush Aniket 2025 年 1 月 24 日 11:38
-

回答 (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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by