Octave: Help with implementing midpoint rule

  • Thread starter MaxManus
  • Start date
  • Tags
    Octave
In summary, the midpoint rule in Octave is a numerical method for approximating definite integrals by dividing the interval into smaller subintervals and using the midpoint of each subinterval to calculate the area under the curve. To implement it, one must define the function, specify the limits of integration, and use a for loop to calculate the midpoint of each subinterval. The advantages of this method include its simplicity, accuracy, and adaptability to complex integrals. However, it may not provide accurate results for rapidly changing or discontinuous functions, and other methods may be more suitable in those cases. The midpoint rule can also be extended to higher dimensions in Octave for calculating double or triple integrals.
  • #1
MaxManus
277
1
I got the code from my textbook and it is supposed to work in matlab
Code:
function r = midpoint(a,b,f,n)
f = fcnchk(f);
h = (b-a)/n;
x = (a+h*0.5):h:(b-h*0.5);
y = feval(f,x);
r = h*sum(y(1:n));

The code fails on feval

I go tcnchk from wikipedia
Code:
function f=fcnchk(x, n)
  f = x;
end

and I use f = 'x.^2' as f

Can someone help me?
I don't know how to copy the error from Octave
 
Physics news on Phys.org
  • #2
How about you look at the error and write it down, then post it here?
 

What is the midpoint rule in Octave?

The midpoint rule in Octave is a numerical method used for approximating the value of a definite integral. It involves dividing the interval of integration into smaller subintervals and using the midpoint of each subinterval to calculate the area under the curve.

How do I implement the midpoint rule in Octave?

To implement the midpoint rule in Octave, you will first need to define the function you want to integrate and specify the limits of integration. Then, you can use a for loop to divide the interval into subintervals and calculate the midpoint of each subinterval. Finally, you can use the formula for the midpoint rule to calculate the approximate value of the integral.

What are the advantages of using the midpoint rule in Octave?

The midpoint rule is relatively simple to implement in Octave and does not require advanced mathematical knowledge. It also tends to provide more accurate results compared to other numerical methods such as the trapezoidal rule. Additionally, it can be easily adapted to handle integrals with varying degrees of complexity.

Are there any limitations to using the midpoint rule in Octave?

Yes, there are some limitations to using the midpoint rule in Octave. It may not provide accurate results for integrals with rapidly changing functions or for functions with discontinuities. In such cases, other numerical methods may be more suitable.

Can the midpoint rule be used for higher dimensions in Octave?

Yes, the midpoint rule can be extended to higher dimensions in Octave by dividing the integration region into smaller subregions and using the midpoint of each subregion to approximate the integral. This can be useful for calculating double or triple integrals.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top