Steepest Ascent Method to Find Minimum - Error with syms to logical

조회 수: 8 (최근 30일)
N/A
N/A 2020년 3월 11일
답변: Samatha Aleti 2020년 3월 26일
I am attempting to find a minimum point, but I am receiving the following error for my code. I am unsure of how to work around this.
ERROR:
>> SteepestAscent
Conversion to logical from sym is not possible.
Error in SteepestAscent (line 10)
while es<ea && iter<itermax
MATLAB CODE:
function SteepestAscent()
iter=1;
itermax=5;
ea=5;
es=1;
x0=1;
y0=1;
%while (-6<x0) && (x0<6)
% while (-6<double(subs(y0))) && (double(subs(y0))<6)
while es<ea && iter<itermax
if (-6<x0) && (x0<6)
if (-6<double(subs(y0))) && (double(subs(y0))<6)
fx=4*x0^3+4*x0*y0+2*y0^2-42*x0-14; %Partial Deriv w/ X
fy=4*y0^3+2*x0^2+4*x0*y0-26*y0-22; %Partial Deriv w/ Y
%grad=[fx,fy];
syms h
x=x0+fx*h;
y=y0+fy*h;
f=x^4+y^4+2*x^2*y+2*x*y^2-21*x^2-13*y^2-14*x-22*y+170;
c=diff(f,h);
assume(h,'clear')
h=solve(c==0,h,'PrincipalValue',true);
x_new=x0+fx*h;
y_new=y0+fy*h;
ea=abs((x_new-x0)/x_new)*100;
x0=x_new;
y0=y_new;
end
end
iter=iter+1;
end
fprintf('X coordinate=')
disp(x0)
fprintf('Y coordinate=')
disp(y0)
end

채택된 답변

Samatha Aleti
Samatha Aleti 2020년 3월 26일
This error occurs when comparing a numeric value and a symbolic value. In order to compare these two values, you can convert symbolic value to numeric using “subs” function and “double” type-conversion.
The value "ea" will be symbolic values for the next iteration of while loop(in your code), Hence you can replace :
es < ea;
with
es<double(subs(ea)) ;
You can similarly replace when comparing other symbolic values with numeric values.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by