Help with MATLAB Numerical Integration

In summary, the conversation involved the speaker wanting to derive their own numerical integration code in MATLAB for a homework assignment. They provided their current code and asked for suggestions and a link to the code for the trapz function. They also mentioned being able to take a numerical derivative easily and being confused about why their code wasn't working. The conversation ended with the speaker figuring out their mistake and providing a revised code with a plot.
  • #1
UR_Correct
37
0
First off, I know I can use the trapz or quad functions on matlab, but I want to derive my own numerical integration code.

The problem is more complicated than this, but basically I need to be able to numerically integrate in MATLAB to continue with the homework assignment.

If y = x^2 is the function of interest to be numerically integrated from 0 to 4, this is what I have so far:

x_step = 0.1;

x = 0:x_step:4;
y = x.^2;
y2 = (x.^3)/3;

N = length(y)

for i=1:N-1

int_y(i) = ((y(i)+y(i+1))./2)*x_step %employing trapezoidal rule

end

plot(x,y), hold on, plot(x(1:N-1),y_int,'r'), hold on, plot(x,y2,'g') %just to see if I'm right

It doesn't work. Any suggestions? Can anyone link me to the code for the trapz function for matlab? I looked for a little bit, but couldn't find it. I'm not sure if we can just use trapz, so just to be safe, I wanted to develop the code myself. I can take a numerical derivative pretty easily with matlab, so I don't understand why this won't work for me!

Thanks for any help.
 
Last edited:
Physics news on Phys.org
  • #2
I figured it out. I forgot to sum the integrations. This thread can be closed.

For anyone interested:

x_step = 0.01;

x = 0:x_step:4;
y = x.^2;
y2 = (x.^3)./3;

N = length(y);
y3 = 0;
for i = 2:N-1
y3 = (y(i)+y(i+1))./2*x_step + y3;
int_y(i) = y3;
endplot(x,y), hold on, plot(x(1:N-1),int_y,'r'), plot(x,y2,'g')
 

Related to Help with MATLAB Numerical Integration

1. What is numerical integration in MATLAB?

Numerical integration in MATLAB is a method for approximating the value of a definite integral by dividing the interval of integration into smaller subintervals and evaluating the function at specific points within each subinterval. This allows for the calculation of integrals that cannot be solved analytically.

2. How do I perform numerical integration in MATLAB?

To perform numerical integration in MATLAB, you can use the built-in function "integral". This function takes in the function to be integrated, the limits of integration, and any additional parameters, and returns an approximate value for the integral.

3. What is the difference between numerical integration and symbolic integration in MATLAB?

Numerical integration in MATLAB involves approximating the value of an integral using a numerical method, while symbolic integration involves finding the exact solution to an integral using algebraic manipulation. Numerical integration is typically used for functions that cannot be integrated symbolically, while symbolic integration is used for simpler functions.

4. Can I use MATLAB to solve improper integrals?

Yes, you can use MATLAB to solve improper integrals by specifying the limits of integration as infinite or as a variable tending towards infinity. The "integral" function will then use numerical methods to approximate the value of the integral.

5. What are the limitations of numerical integration in MATLAB?

Numerical integration in MATLAB can be limited by the accuracy of the numerical method used, as well as the number of intervals used to approximate the integral. Additionally, it may not be able to handle highly oscillatory or discontinuous functions accurately.

Similar threads

  • Calculus and Beyond Homework Help
Replies
10
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
862
  • Calculus and Beyond Homework Help
Replies
3
Views
676
  • Calculus and Beyond Homework Help
Replies
14
Views
716
  • Calculus and Beyond Homework Help
Replies
10
Views
766
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
345
  • Calculus and Beyond Homework Help
Replies
1
Views
793
  • Calculus and Beyond Homework Help
Replies
24
Views
2K
  • Calculus and Beyond Homework Help
Replies
20
Views
767
Back
Top