getting error in line 5 please help

조회 수: 4 (최근 30일)
mohammad massarwe
mohammad massarwe 2021년 7월 2일
답변: Star Strider 2021년 7월 2일
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.
  댓글 수: 1
mohammad massarwe
mohammad massarwe 2021년 7월 2일
can you tell me what the problem ?

댓글을 달려면 로그인하십시오.


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