Composite Trapez-ium Rule Approximation of Integral f(x)dx

Click For Summary
SUMMARY

The discussion focuses on implementing the Composite Trapezium Rule (CTR) in MATLAB to approximate the integral of a function f(x). The algorithm requires inputs a, b, and N, where a and b define the interval and N is the number of subintervals. The provided MATLAB function has a logical structure but contains errors in defining and calling the function f. Additionally, the user seeks clarification on using the feval command to apply the function to a specific linear equation, f=3x+2, but encounters errors when executing the CTR function.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with numerical integration techniques, specifically the Composite Trapezium Rule
  • Knowledge of function handles in MATLAB
  • Basic understanding of error handling in MATLAB functions
NEXT STEPS
  • Review MATLAB function handles and their usage
  • Learn about error handling in MATLAB to debug function calls
  • Explore numerical integration methods beyond the Composite Trapezium Rule
  • Investigate the feval command in MATLAB for dynamic function evaluation
USEFUL FOR

Students and professionals in mathematics, engineering, and computer science who are working with numerical methods for integration in MATLAB, particularly those implementing the Composite Trapezium Rule.

sara_87
Messages
748
Reaction score
0

Homework Statement



Write an algorithm and Matlab function JN, which uses the Composite Trapez-
ium Rule (CTR) to compute an approximation of the integral f(x) dx for an arbitrary
function f of one variable. The inputs should be a, b and N (the number of
subintervals), and f (the name of a Matlab function of one variable). The
output should be the approximate integral value JNf, i.e. the result of the
Composite Trapezium Rule for f.


Homework Equations





The Attempt at a Solution



function JN=CTR(a,b,N)
h=(b-a)/N;
JN=0;
for i=1:N
ti=a+i*h;
ti1=a+(i-1)*h;
f=f(ti);
JN=JN+h/2*(f(ti1)+f(ti))
end

Most of it looks right but I'm sure i have done something wrong. is there something missing with defining f.
Thank you
 
Physics news on Phys.org
Did you define f(x) somewhere? What's the point of f=f(ti) if you end up calling the function again anyway? The idea looks ok, it's just that you don't have all the specifics for MATLAB to me.
 
Oh i think i get it now. no worries.
i have another question:
i have a function; like the one above, how do i apply it in another m file. i mean i want to apply this to the function f=3x+2 using feval command. this is what i did:
JN=feval(N,x);
y=3*x+2;
but if i type in the command: CTR(0,0.9,20); an error comes up.
How do we use the feval command?
Thank you.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 3 ·
Replies
3
Views
18K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
6
Views
2K