First off, I know I can use the trapz or quad functions on matlab, but I want to derive my own numerical integration code.(adsbygoogle = window.adsbygoogle || []).push({});

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 Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Help with MATLAB Numerical Integration

Loading...

Similar Threads - Help MATLAB Numerical | Date |
---|---|

Matlab MATLab: Not enough inputs for nlinfit | Nov 16, 2017 |

Matlab Help needed to fix a matlab code | Nov 8, 2017 |

New to Matlab, help with vectors | Feb 20, 2016 |

Help Numerical integration in matlab | Jan 6, 2012 |

Matlab help needed: numerical jacobian | Nov 5, 2010 |

**Physics Forums - The Fusion of Science and Community**