Help with MATLAB Numerical Integration

In summary, the conversation is about a person wanting to develop their own numerical integration code in MATLAB. They are having trouble with their code and are seeking help in finding the code for the trapz function. After some discussion, they figure out their mistake and successfully integrate the function.
  • #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.
 
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. How do I perform numerical integration in MATLAB?

To perform numerical integration in MATLAB, you can use the integral function. This function takes in an expression or an anonymous function as its first input, and the limits of integration as the second and third inputs. It will then use various numerical integration methods to approximate the integral and return the result.

2. What are the different numerical integration methods available in MATLAB?

Some of the commonly used numerical integration methods in MATLAB include trapz (trapezoidal rule), quad (adaptive Simpson's rule), and quadl (adaptive Lobatto quadrature). You can also specify a specific method to use in the integral function by using the 'Method' parameter.

3. How can I improve the accuracy of my numerical integration in MATLAB?

There are a few ways to improve the accuracy of numerical integration in MATLAB. You can increase the number of integration points by specifying the 'AbsTol' and 'RelTol' parameters in the integral function. You can also use a more accurate integration method or try a change of variable to better fit the integrand.

4. Can I perform multidimensional numerical integration in MATLAB?

Yes, you can perform multidimensional numerical integration in MATLAB by using the integral2 function for double integrals, integral3 function for triple integrals, and so on. These functions work similarly to the integral function but take in multiple limits of integration as inputs.

5. Is there a way to visualize the results of my numerical integration in MATLAB?

Yes, you can use the plot function to plot the integrand and the area under the curve. You can also use the area function to create a shaded area under the curve plot. Additionally, you can use the fplot function to plot the integrand and the approximated integral over a range of values.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
941
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Back
Top