steepest descent using secant method

조회 수: 1 (최근 30일)
Hacer Busra KELES
Hacer Busra KELES 2021년 5월 4일
댓글: SALAH ALRABEEI 2021년 6월 21일
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
SALAH ALRABEEI 2021년 6월 21일
your function depends on X1 and X2 variables, while you differentiated wrt X and Y, HOW COME!
SALAH ALRABEEI
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 CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by