Help with MATLAB Numerical Integration

Click For Summary
SUMMARY

The forum discussion focuses on deriving a custom numerical integration code in MATLAB for the function y = x^2 over the interval from 0 to 4. The user initially attempted to implement the trapezoidal rule but encountered issues due to not summing the integrations correctly. After correcting this oversight, the user successfully integrated the function using a step size of 0.01 and confirmed the results by plotting the integrated values against the expected analytical solution.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of numerical integration techniques, specifically the trapezoidal rule
  • Basic knowledge of plotting functions in MATLAB
  • Concept of step size in numerical methods
NEXT STEPS
  • Explore MATLAB's built-in functions for numerical integration, such as 'trapz' and 'quad'
  • Learn about error analysis in numerical integration methods
  • Investigate other numerical integration techniques, such as Simpson's rule
  • Study MATLAB's vectorization techniques to optimize performance in numerical computations
USEFUL FOR

This discussion is beneficial for students and professionals in engineering, mathematics, or any field requiring numerical analysis using MATLAB, particularly those interested in custom implementations of numerical integration methods.

UR_Correct
Messages
36
Reaction score
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
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')
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
27
Views
4K