필터 지우기
필터 지우기

Why is there an error? Undefined function 'f' for input arguments of type 'double'.

조회 수: 1 (최근 30일)
Error using arrayfun
Undefined function 'f' for input arguments of type 'double'.
Error in golden (line 3)
plot(t,arrayfun(@f,t));
function golden(a, b)
t=[a:0.01:b];
plot(t,arrayfun(@f,t));
hold on
tol=10^(-12);
r=(3-sqrt(5))/2;
c=a+r*(b-a);
d=b-r*(b-a);
fa=f(a);
fb=f(b);
fc=f(c);
fd=f(d);
while (b-a>tol)
if (fc<fd)
b=d;
fb=fd;
d=c;
fd=fc;
c=a+r*(b-a);
fc=f(c);
plot(c,fc,'r*');
else
a=c;
fa=fc;
c=d;
fc=fd;
d=b-r*(b-a);
fd=f(d);
plot(d,fd,'r*');
end
end
fprintf('Minimum found at x=%.12f \n', (a+b)/2, f(a+b)/2);
hold off
end

채택된 답변

Image Analyst
Image Analyst 2014년 3월 5일
Well, you did not define f, just like it said. What do you think f should be? You have to tell it!
  댓글 수: 2
Ehi Eromosele
Ehi Eromosele 2014년 3월 5일
would the correct method of defining f be?
f(x)=2*sin(ax)-sin(bx);
before plotting...
Image Analyst
Image Analyst 2014년 3월 5일
Look up "anonymous functions" then try it and see. That's the best way to learn.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by