Why is there an error when I called the function inputting the points

1 view (last 30 days)
"Undefined function 'f' for input arguments of type 'double'.
Error in linearinterp>refine (line 11) if abs((f(x1)+f(x2))/2-f(xm))<tol
Error in linearinterp (line 3) points = refine(0, 7, 0.6);"
function linearinterp
f = @(x) (exp(-x/2).*sin(x.^2+8));
points = refine(0, 7, 0.6);
plot(points, f(points), 'r+');
hold on
plot(points, f(points));
hold off
end
function points = refine(x1, x2, tol)
xm = (x1+x2)/2;
if abs((f(x1)+f(x2))/2-f(xm))<tol
points=[x1,x2];
else
left=refine(x1, xm, tol);
right=refine(xm, x2, tol);
points=[left, right(2:length(right))];
end
end

Accepted Answer

Mischa Kim
Mischa Kim on 12 Feb 2014
Edited: Mischa Kim on 12 Feb 2014
Ehi, you need to define function f again in refine() to make it work. In other words:
function points = refine(x1, x2, tol)
f = @(x) (exp(-x/2).*sin(x.^2+8));
...
Reason begin, f is only in scope of linearinterp(). It is not in scope for refine() and therefore unknown. Alternatively, you can pass f as an input argument to refine():
points = refine(0, 7, 0.6, f)
...
function points = refine(x1, x2, tol, f)
In that case you do not need to define f again.

More Answers (0)

Categories

Find more on Interpolation 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!