Take the 2-minute tour ×

How would I go about creating a function in matlab which would calculate the following

$$M_c(f)=h \sum_{i=1}^N f(c_i)$$


$h=\frac{b-a}{N}, \\c_i=a+0.5(2i-1)h,\\ i=1,\ldots,N$

What I have tried so far is

function(M(f))= composite_midpoint(f)

for i=1:1:N
   M(f) = h*(sum + f)

Sorry about not inserting the matlab code directly, I'm not sure how to do it.

share|improve this question
Updated the formatting, you can use four spaces in front of each line to show code. –  Dennis Jaheruddin Nov 13 '12 at 16:41
What types of 'functions' are you expecting to be able to input for f? Symbolic functions, or matlab functions? –  icurays1 Nov 13 '12 at 16:42
It would be a function which is smooth enough for Taylor's theorem and one which it's integral can be calculated exactly. –  Nicky Nov 13 '12 at 16:49
Yes, but with matlab functions are either .m files or "symbolic" functions. Its not going to work the way you have it written if you call it like "composite_midpoint(x^2)". It doesn't know what x^2 means. –  icurays1 Nov 13 '12 at 16:54
If I were to want the function to be x^2 how would I go about altering my code so that it worked for the midpoint rule? –  Nicky Nov 13 '12 at 16:59
show 1 more comment

2 Answers

up vote 1 down vote accepted

First run this outside the function:

a = 6; 
b = 4.234;
N = 10;

Then save this function to a file called compositemidpoint.m (in your current directory)

function M = compositemidpoint(a,b,N)
h = (b-a)/N
i = 1:N
c_i = a+0.5*(2*i-1)*h
f = log(c_i) + c_i.^2 % A sample function
M = h*sum(f);

Then call it by typing:

share|improve this answer
It does not really make sens yet, but this is how i debugged your attempted code to not-crash. –  Dennis Jaheruddin Nov 13 '12 at 16:52
Your code only sums the vector $f$ and multiplies it by $h$. There is no midpoint rule occurring here - you have to create the vector $f$ by evaluating some function at the grid points $c_i$... –  icurays1 Nov 13 '12 at 16:56
I have updated the answer to include an example function. –  Dennis Jaheruddin Nov 13 '12 at 17:01
I tried running this but it wouldn't work. I've put in function out= compositemidpoint at the very begining of the function however it is still coming up with errors. –  Nicky Nov 13 '12 at 17:14
I have included some instructions on how to call it. –  Dennis Jaheruddin Nov 13 '12 at 17:22
add comment

Here's my solution, which is vectorized (for loops are bad in matlab).

function Mf=midpoint_rule(a,b,N,f)

%ci are your evaluation points
%This evaluates the function f, which is another matlab function
%you can just add up the vector y and multiply by h


For example, you can save another .m file Myfunction.m, that might look like:

function y=Myfunction(x)

%The dot means "pointwise"


Then, in the main window, you would evaluate the integral by saying "midpoint_rule(1,2,100,@Myfunction)". The "at" symbol tells matlab you'll be using a matlab function called "Myfunction".

share|improve this answer
If you only need your midpoint rule function to run for a couple test functions, you can also hard-code them in by saying "y=sin(x)" etc instead of "y=f(ci)". But then you would have to change your code every time you have a new function to integrate! –  icurays1 Nov 13 '12 at 17:18
add comment

Your Answer


