getting error in line 5 please help

7 次查看(过去 30 天)
functin=@(x) (3*(-1-212*x)^2+(-1-212*x)-4*(4-170*x)-7)^2+(4-170*x-1-212*x2)^2;
ta3ot=0.001;
Up_limit=2;
Low_limit=0;
[min_x,y_min]=golden_cut(functin,ta3ot,Up_limit,Low_limit); %%%%%%%%%%% hereeeeeeeeeeeeeeeeeeeeeeeeeeee
function[min_x,y_min]=golden_cut(functin,ta3ot,Up_limit,Low_limit)
x1=Low_limit+0.381966*(Up_limit-Low_limit);
x2=Low_limit+0.618034*(Up_limit-Low_limit);
while(sqrt((x2-x1)^2)>ta3ot)
if(functin(x1)>functin(x2))
Low_limit=x1;
x1=x2;
x2=Low_limit+0.618034*(Up_limit-Low_limit);
else
Up_limit=x2;
x2=x1;
x1=Low_limit+0.381966*(Up_limit-Low_limit);
end
end
min_x=(x1+x2)/2;
y_min=(functin(x1)+functin(x2))/2;
end

回答(3 个)

Yongjian Feng
Yongjian Feng 2021-7-2
Use the matlab debugger. It will tell you what error it is.

Sardar Usama
Sardar Usama 2021-7-2
The error is in line-1.
functin=@(x) (3*(-1-212*x)^2+(-1-212*x)-4*(4-170*x)-7)^2+(4-170*x-1-212*x2)^2;
x2 is undefined in above line.

Star Strider
Star Strider 2021-7-2
The error refers to ‘x2’ not being defined in ‘functin’:
functin=@(x) (3*(-1-212*x)^2+(-1-212*x)-4*(4-170*x)-7)^2+(4-170*x-1-212*x2)^2;
↑ ← HERE
Since it is not obvious whether this is a simple typographical error or if it supposed to be ‘x*2’ or ‘x^2’ (or perhaps something else), you have to determine what you want, and fix it. With that ambiguity resolved, the code runs without error.
% functin=@(x) (3*(-1-212*x)^2+(-1-212*x)-4*(4-170*x)-7)^2+(4-170*x-1-212*x2)^2;
% ta3ot=0.001;
% Up_limit=2;
% Low_limit=0;
% [min_x,y_min]=golden_cut(functin,ta3ot,Up_limit,Low_limit); %%%%%%%%%%% hereeeeeeeeeeeeeeeeeeeeeeeeeeee
min_x = 0.0081
y_min = 10.1141
%
% function[min_x,y_min]=golden_cut(functin,ta3ot,Up_limit,Low_limit)
% x1=Low_limit+0.381966*(Up_limit-Low_limit);
% x2=Low_limit+0.618034*(Up_limit-Low_limit);
% while(sqrt((x2-x1)^2)>ta3ot)
% if(functin(x1)>functin(x2))
% Low_limit=x1;
% x1=x2;
% x2=Low_limit+0.618034*(Up_limit-Low_limit);
% else
% Up_limit=x2;
% x2=x1;
% x1=Low_limit+0.381966*(Up_limit-Low_limit);
% end
% end
% min_x=(x1+x2)/2;
% y_min=(functin(x1)+functin(x2))/2;
% end
.

类别

Help CenterFile Exchange 中查找有关 Signal Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by