Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using trap to numerically integrate between bounds

Subject: using trap to numerically integrate between bounds

From: Windell

Date: 14 Feb, 2010 10:56:18

Message: 1 of 2

Hi, I have a set of data points in which I would like to numerically integrate using trap. Rather than just calculate the area under the curve, I would like to plot the integral with respect to time. So what I am trying to do is perform the integral between two times, increment the time step to integrate and populate a vector with the result so I can plot the integral with time.

Here is the section of code in my program where I am trying to do this. This is only a section of my entire code so it may look a bit incomplete.

%calculate rotational speed of motors.
for i=1:length(z)
    for n=1:i
        time_interval(i,1)=t(i,1);
        func(i,1)=ang_accel(i,1);
    end
    react_1(i,1)=trapz(time_interval,func)
end
figure
plot(time_interval,react_1)

Right now my code produces an error:
??? Error using ==> permute
ORDER contains an invalid permutation index.

Error in ==> trapz at 44
  y = permute(y,perm);

Error in ==> solver at 86
    react_1(i,1)=trapz(time_interval,func)

Any suggestions? Thanks!
Windell

Subject: using trap to numerically integrate between bounds

From: John D'Errico

Date: 14 Feb, 2010 12:04:05

Message: 2 of 2

"Windell " <windelljones@gmail.com> wrote in message <hl8ksi$8vp$1@fred.mathworks.com>...
> Hi, I have a set of data points in which I would like to numerically integrate using trap. Rather than just calculate the area under the curve, I would like to plot the integral with respect to time. So what I am trying to do is perform the integral between two times, increment the time step to integrate and populate a vector with the result so I can plot the integral with time.
>
> Here is the section of code in my program where I am trying to do this. This is only a section of my entire code so it may look a bit incomplete.
>

(snip)

> Any suggestions? Thanks!

Only a question. Why reinvent the wheel? Why not
simply use cumtrapz?

If you did not know that cumtrapz exists, read the
help. The help (or doc) for trapz will have pointed
out the existence of cumtrapz.

John

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us