0

I am given the error

Not enough input arguments.

Error in fun (line 2) f = cos(2*x)./exp(x);

When trying to run my code with the command

simp(fun, 0, 2*pi, 120)

for my code

function I = simp(fun, a,b,n)

%Standard simpson's rule 
tol = 0.0001;

h = (b-a)/(2*n); % The subinterval spacing
x = a:h:b; % The partition points
y = fun(x); % The function values at those points
i = 0:2*n; % Makes a list from 0 to 2*n

coeffs = 3+(-1).^(i+1); % Makes a list of 2s and 4s to use in the Simpson's Rule
coeffs(1) = 1; coeffs(end)=1;

SR = h/3 * sum(coeffs .* y); % This is the Simpson's Rule
disp(SR);

function f = fun(x)
f = cos(2*x)./exp(x);
end

The error is telling me that I did not provide enough input arguments, but I see that I give the function (fun), 'a', 'b', and 'n' values. So why am I getting this error?

0

1 Answer 1

2

You need to pass the function handle with @:

simp(@fun, 0, 2*pi, 120)

You can also use:

simp(@(x) cos(2*x)./exp(x), 0, 2*pi, 120)

When you use only fun, Matlab think you try to call the function with no parameter. @fun is the actual function.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Twisted, thank you for the answer. Why must I use the anonymous function handle?
Function handle let you do things like: f = @myfunction, or f = @(x) anotherfunction(x).
Hm, okay. I'll just have to read more documentation regarding it. Thank you for your time!
it's just a reference to the function instead of the actual function :) Take a look at this: mathworks.com/help/matlab/matlab_prog/…
@Taln : fun is the same as fun(). @fun is a function handle to fun. It’s not an anonymous function. An anonymous function would be the second case (@(x)...), where you define a new function without a name and get its handle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.