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

## All 5 Replies

Please push first (CODE) button and then paste your code or paste code, high light and push code.

I could not decipher your code as end is not defined in expression y(end)

This I got:

``````from math import *

def simpson(y, a,b,N):
s = 0
f = lambda x: eval(y)
# N not defined -> put as parameter
for k in range(2,N):
s += 2*f(k) if k % 2 == 0 else 4*f(k)
# end not defined replaced a and b
s += f(a) + f(b)
h = (a-b)/N
return h*s/3.

print(simpson(y='cos(x)/sqrt(1+sin(x))', a=1, b=16, N=16))
``````

But it is not probably correct result is -1.7994533931048027 in Python 3.2, but -1.91941695265 in Python 2.7.1 , from these random values for a,b and N.

It's worthwhile for you to investigate Python maths environment Sage, for example http://wiki.sagemath.org/interact/calculus.

Thanks Tony! I got it working. And, the code is not the correct implementation. You're evaluating at k instead of evaluating at the kth point in the interval which is given by a + (b-a)*k/N for b>a

I only wrote what could be deciphered from your post without trying to do correct implementation, glad that it helped you enough. Remember code-tags next time!

Cool. Is there a way I can make the editor use MatLab highlighting? The code tags automatically default to Python for the Python forum (I think).

``````[/B] seems to work, even your code does not beautify much:

[CODE=matlab]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``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.