Matlab function

  • MATLAB
  • Thread starter sara_87
  • Start date
763
0

Main Question or Discussion Point

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


2. Homework Equations



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

Answers and Replies

334
0
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.
 
763
0
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.
 

Related Threads for: Matlab function

  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
2
Views
3K
Replies
1
Views
3K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
3K
Top