steepest descent using secant method
2 次查看(过去 30 天)
显示 更早的评论
I am trying the sovee function the steepest descent algrot using secant method but ı guess my code is wrong how can fixed;
syms X1 x2;
f = x1.^4/8 + x2.^2/4 -2*x1-4*x1*x2;
x1(1) = 2;
x2(1) = 2;
df_dx1 = diff(f, X);
df_dx2 = diff(f, Y);
g0 = [subs(df_dx1,[X,Y], [x1(1),x2(1)]) subs(df_dx2, [X1,X2], [x1(1),x2(1)])];
d = -(g0);
for i = 1:5
I = [x1(i),x1(i)];
syms t
g = subs(f, [X,Y], [x1(i)+d(1)*t,x2(i)+t*d(2)]);
dg_dt = diff(g,t);
t = solve(dg_dt, t);
x1(i+1) = I(1)+t*d(1);
x2(i+1) = I(2)+t*d(2);
g_o = [subs(df_dx,[X,Y], [x1(i),y(i)]) subs(df_dy, [X1,X2], [x(i),x2(i)])];
i = i+1;
g_1 = [subs(df_dx1,[X1,X2], [x1(i),X2(i)]) subs(df_dx2, [X1,X2], [x1(i),x2(i)])];
d=-(g_1);
end
Iter = 1:i;
X_coordinate = x1';
Y_coordinate = x2';
Iterations = Iter';
T = table(Iterations,X_coordinate,Y_coordinate);
fcontour(f, 'Fill', 'On');
hold on;
plot(x1,x2,'*-r');
fprintf('Initial Objective Function Value: %d\n\n',subs(f,[X1,X2], [x1(1),x2(1)]));
fprintf('Number of Iterations for Convergence: %d\n\n', i);
fprintf('Point of Minima: [%d,%d]\n\n', x1(i), x2(i));
fprintf('Objective Function Minimum Value after Optimization: %d\n\n', subs(f,[X1,X2], [x1(i),x2(i)]));
disp(T)
2 个评论
SALAH ALRABEEI
2021-6-21
your function depends on X1 and X2 variables, while you differentiated wrt X and Y, HOW COME!
SALAH ALRABEEI
2021-6-21
You have to double chceck your code first, your function has X1 and X2 variables, so whenever you use diff or subs, you have to keep them. here you have also y and x2 as both number of variable.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!