Hi all, I was working on a MATLab program and I came to a point where I wanted to define a function such that one of the arguments would be used as a command. To clarify, the function being defined was an implementation of Simpson's rule that would integrate using the technique with given bounds, an even number of intervals, and, finally, the function being integrated. I wanted the user to be able to enter any function of his or her choice, but I'm unaware of any techniques or modules that could help me accomplish that. Although, I'm talking about MATLab I would like to implement a similar function using Python and I take it the technique would be similar for the two. I've included the code I have for MATLab below to better illustrate my query:

function simpsons(a, b, N)

x = linspace(a, b, N + 1);

y = cos(x)./sqrt(1+sin(x)); % This is the function I used for this instance. I want the user to be able to enter any function of their choice and then have MATLab insert the function in place of this one automatically. So instead of "simpsons(a, b, N)", I was thinking about something along the lines of "simpsons(f, a, b, N)", where for 'f' the user can enter expressions such as: "x.^2 + 3.*x + pi^exp(sqrt(pi))", "exp(sin(x))", or "abs(atan(1./x))" to name a few. What would be the best way to do this? I've heard of pointers but I have no idea what they are or if they can even be used for something like this.

s = 0

for k = 2:N

if mod(k, 2) == 0

s += 2*y(k)

else

s += 4*y(k)

end

end

s += y(1) + y(end);

h = (a-b)/N;

Int = h*s/3;

end

*Edited
by Thisisnotanid*: Wrong syntax highlighting.