Steepest Ascent Method to Find Minimum - Error with syms to logical
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!