Why is there an error? Unbalanced or unexpected parenthesis or bracket.
2 次查看(过去 30 天)
显示 更早的评论
Error: File: golden.m Line: 1 Column: 21
Unbalanced or unexpected parenthesis or bracket.
function golden(sqrt(2), sqrt(28))
t=[sqrt(2):0.01:sqrt(28)];
plot(t, arrayfun(@f,t));
hold on
tol=10^(-12);
r=(3-sqrt(5))/2;
c=sqrt(2)+r*(sqrt(28)-sqrt(2));
d=sqrt(28)-r*(sqrt(28)-sqrt(2));
fa=f(sqrt(2));
fb=f(sqrt(28));
fc=f(c);
fd=f(d);
while (sqrt(28)-sqrt(2)>tol)
if (fc<fd)
sqrt(28)=d;
fb=fd;
d=c;
fd=fc;
c=sqrt(2)+r*(sqrt(28)-sqrt(2));
fc=f(c);
plot(c,fc,'r*');
else
sqrt(2)=c;
fa=fc;
c=d;
fc=fd;
d=sqrt(28)-r*(sqrt(28)-sqrt(2));
fd=f(d);
plot(d,fd,'r*');
end
end
fprintf('Minimum found at x=%.12f \n', (sqrt(2)+sqrt(28))/2, f(sqrt(2)+sqrt(28))/2);
hold off
end
0 个评论
采纳的回答
Jos (10584)
2014-3-5
sqrt(2) and sort(28) are not a valid variable name
replace sqrt(2) and sqrt(28) every with A and B
function golden (A,B)
t = A:0.01:B ;
.. etc ..
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!