Hello I'm new to matlab coding and here Im getting this parse error and I cannot figure it out. From this code I need to find the minimum of the function using steepest descent method. And I would like to know this code correct or not please.
this is my func : F (x,y) = 10x2−4xy+7y2−455x−y−16
and the starting point is : [ 0,10 ]
thanks for help!
%steepest descent method
function f = target_function(x, y)
f = 10 * x.^2 - 4 * x .* y + 7 * y.^2 - 20 * sqrt(5) * x + 4 * sqrt(5) * y - 16;
end
%parameters
x0 = [0 10];
eps = 0.01;
%initialization
point_min = x0';
target_func_min = target_function(point_min(1), point_min(2));
w = antigradient(point_min);
nw = norm(w);
tau = (1 + sqrt(5)) / 2;
%statistics
points(:, 1) = point_min;
target_func_values(1) = target_func_min;
iter_count = 0;
target_func_calls_count = 0;
%One-Dimensional Minimization Using the Golden Section Method
while nw > eps
iter_count = iter_count + 1;
kp = 0;
step = 0;
while abs(kp - step) < eps
a = step;
b = a + 2;
step = step + 2;
kp1 = ((b * (tau - 1) + a) / tau);
kp2 = ((a * (tau - 1) + b) / tau);
X = point_min + kp1 * w;
f_X1 = target_function(X(1), X(2));
X = point_min + kp2 * w;
f_X2 = target_function(X(1), X(2));
target_func_calls_count = target_func_calls_count + 2;
while norm(b - a) > eps
if (f_X1 > f_X2)
kp = kp2;
a = kp1;
kp1 = kp2;
kp2 = ((a * (tau - 1) + b) / tau);
f_X1 = f_X2;
X = point_min + kp2 * w;
f_X2 = target_function(X(1), X(2));
else
kp = kp1;
b = kp2;
kp2 = kp1;
kp1 = ((b * (tau - 1) + a) / tau);
f_X2 = f_X1;
X = point_min + kp1 * w;
f_X1 = target_function(X(1), X(2));
end
target_func_calls_count = target_func_calls_count + 1;
end
end
point_min = point_min + kp * w;
w = antigradient(point_min);
target_func_min = target_function(point_min(1), point_min(2));
nw = norm(w);
points(:, iter_count + 1) = point_min;
target_func_values(iter_count + 1) = target_func_min;
end
Min = min(min(points(1, :)), min(points(2, :)));
Max = max(max(points(1, :)), max(points(2, :)));
space = linspace(Min - 5, Max + 5, 500);
[gridX, gridY] = meshgrid(space, space);
gridJ = target_function(gridX, gridY);
figure;
contour(gridX, gridY, gridJ, target_func_values, 'black', 'ShowText', 'on');
hold on;
plot(points(1, :), points(2, :), 'r.-');
fprintf(func count: %.0f\n',target_func_calls_count);
fprintf(iter: %.0f\n\n', iter_count);
fprintf(min point: (%2.2f; %2.2f)\n', point_min);
fprintf(min value: %2.2f\', target_func_min);

 采纳的回答

Looks like you are missing a single-quote at the beginning of each of your fprintf calls (and an "n" is missing in the last one):
fprintf(func count: %.0f\n',target_func_calls_count);
fprintf(iter: %.0f\n\n', iter_count);
fprintf(min point: (%2.2f; %2.2f)\n', point_min);
fprintf(min value: %2.2f\', target_func_min);
Should be:
fprintf('func count: %.0f\n',target_func_calls_count);
fprintf('iter: %.0f\n\n', iter_count);
fprintf('min point: (%2.2f; %2.2f)\n', point_min);
fprintf('min value: %2.2f\n', target_func_min);

4 个评论

oh yes thanks!! And what about the x0 parameter? why its getting error?
I wasn't able to reproduce that particular error, but I think it's because function definitions need to be at the end of scripts, so you need to move the target_function definition down to the end of the script.
Like this:
%steepest descent method
%parameters
x0 = [0 10];
eps = 0.01;
%initialization
point_min = x0';
% ...
% ...
% (the rest of your code)
% ...
% ...
fprintf('func count: %.0f\n',target_func_calls_count);
fprintf('iter: %.0f\n\n', iter_count);
fprintf('min point: (%2.2f; %2.2f)\n', point_min);
fprintf('min value: %2.2f\n', target_func_min);
function f = target_function(x, y)
f = 10 * x.^2 - 4 * x .* y + 7 * y.^2 - 20 * sqrt(5) * x + 4 * sqrt(5) * y - 16;
end
I did that. But there are still errors. (
%steepest descent method
%parameters
x0 = [0 10];
eps = 0.01;
%initialization
point_min = x0';
target_func_min = target_function(point_min(1), point_min(2));
w = antigradient(point_min);
Unrecognized function or variable 'antigradient'.
nw = norm(w);
tau = (1 + sqrt(5)) / 2;
%statistics
points(:, 1) = point_min;
target_func_values(1) = target_func_min;
iter_count = 0;
target_func_calls_count = 0;
%One-Dimensional Minimization Using the Golden Section Method
while nw > eps
iter_count = iter_count + 1;
kp = 0;
step = 0;
while abs(kp - step) < eps
a = step;
b = a + 2;
step = step + 2;
kp1 = ((b * (tau - 1) + a) / tau);
kp2 = ((a * (tau - 1) + b) / tau);
X = point_min + kp1 * w;
f_X1 = target_function(X(1), X(2));
X = point_min + kp2 * w;
f_X2 = target_function(X(1), X(2));
target_func_calls_count = target_func_calls_count + 2;
while norm(b - a) > eps
if (f_X1 > f_X2)
kp = kp2;
a = kp1;
kp1 = kp2;
kp2 = ((a * (tau - 1) + b) / tau);
f_X1 = f_X2;
X = point_min + kp2 * w;
f_X2 = target_function(X(1), X(2));
else
kp = kp1;
b = kp2;
kp2 = kp1;
kp1 = ((b * (tau - 1) + a) / tau);
f_X2 = f_X1;
X = point_min + kp1 * w;
f_X1 = target_function(X(1), X(2));
end
target_func_calls_count = target_func_calls_count + 1;
end
end
point_min = point_min + kp * w;
w = antigradient(point_min);
target_func_min = target_function(point_min(1), point_min(2));
nw = norm(w);
points(:, iter_count + 1) = point_min;
target_func_values(iter_count + 1) = target_func_min;
end
Min = min(min(points(1, :)), min(points(2, :)));
Max = max(max(points(1, :)), max(points(2, :)));
space = linspace(Min - 5, Max + 5, 500);
[gridX, gridY] = meshgrid(space, space);
gridJ = target_function(gridX, gridY);
figure;
contour(gridX, gridY, gridJ, target_func_values, 'black', 'ShowText', 'on');
hold on;
plot(points(1, :), points(2, :), 'r.-');
fprintf('func count: %.0f\n',target_func_calls_count);
fprintf('iter: %.0f\n\n', iter_count);
fprintf('min point: (%2.2f; %2.2f)\n', point_min);
fprintf('min value: %2.2f\', target_func_min);
function f = target_function(x, y)
f = 10 * x.^2 - 4 * x .* y + 7 * y.^2 - 20 * sqrt(5) * x + 4 * sqrt(5) * y - 16;
end
I also got the error about "antigradient" being unrecognized. What is it?

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Contour Plots 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by