getting error in line 5 please help

10 views (last 30 days)
mohammad massarwe
mohammad massarwe on 2 Jul 2021
Answered: Star Strider on 2 Jul 2021
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

Answers (3)

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

Sardar Usama
Sardar Usama on 2 Jul 2021
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 on 2 Jul 2021
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
.

Categories

Find more on Signal Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!