MATLAB: Function Error Input Argument

errorfunctionfunctionsmatlab function

Hello, I am trying to make a function to which I can call any arbitrary function and get an approximate value for the sum of my arbitrary function. I keep getting an error though whenever I try and use the function. If you can please help me, it will be much appriciated, thanks. Below is a copy of my code and in the attachments is a sceenshot of the error I am seeing.
function [Approximate_Value] = my_mean(fun,a,b,N)
% The function my mean should return an approximate value for the infinite
% sum of f(x) from a to b with respect to x. Where f(x) represents the
% function fun.
h = (b-a)/(N-1);
x = a+(1i-1)*h;
Approximate_Value = (h/2)*(fun(a)+fun(a+(N-1)*h)+sum(subs(h*f(x),x,1:(N-1))))
end

Best Answer

First ‘f’ does not exist in your function because you do not define it in your code, or pass it as an argument.
Second, the subs function only works in the Symbolic Math Toolbox.
Try this instead (assuming that ‘f’ and ‘fun’ are the same):
Approximate_Value = (h/2)*(fun(a)+fun(a+(N-1)*h)+sum(h*fun(1:(N-1))));
Assuming that ‘a’, ‘h’, and ‘N’ are scalars, that should work.