0
$\begingroup$

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)$$

where

$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)  h=(b-a)/N for i=1:1:N    c_i=a+0.5*(2i-1)*h    M(f) = h*(sum + f) end 

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

  • 0
    Updated the formatting, you can use four spaces in front of each line to show code.2012-11-13
  • 0
    What types of 'functions' are you expecting to be able to input for f? Symbolic functions, or matlab functions?2012-11-13
  • 0
    It would be a function which is smooth enough for Taylor's theorem and one which it's integral can be calculated exactly.2012-11-13
  • 0
    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.2012-11-13
  • 0
    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?2012-11-13
  • 0
    I have included an example, you can replace f = c_i.^2 if you want your function to be the square function.2012-11-13

2 Answers 2

1

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:

compositemidpoint(a,b,N) 
  • 0
    It does not really make sens yet, but this is how i debugged your attempted code to not-crash.2012-11-13
  • 0
    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$...2012-11-13
  • 0
    I have updated the answer to include an example function.2012-11-13
  • 0
    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.2012-11-13
  • 0
    I have included some instructions on how to call it.2012-11-13
1

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

function Mf=midpoint_rule(a,b,N,f)  h=(b-a)/N; %ci are your evaluation points ci=linspace(a+h/2,b-h/2,N-1); %This evaluates the function f, which is another matlab function y=f(ci); %you can just add up the vector y and multiply by h Mf=h*sum(y);  end 

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

function y=Myfunction(x)  %The dot means "pointwise" y=x.^2  end 

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".

  • 0
    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!2012-11-13
  • 0
    Last argument to `linspace` should be `N`, not `N-1`2016-03-28