# MATLAB - for loop for numerical integration

p-williams65
MATLAB - "for" loop for numerical integration

Hi. The following bit of MATLAB function is supposed indefinitely integrate successive points:

% Explode triplet format in to vectors
x_in = data_in(:,1)';
y_in = data_in(:,2)';
e_in = data_in(:,3)';

x_left=x_in(1:end-1);
x_right=x_in(2:end);
y_left=y_in(1:end-1);
y_right=y_in(2:end);
e_left=e_in(1:end-1);
e_right=e_in(2:end);

% Do complicated operations here.
x_out = x_left;
y_out = (0.5 * (y_right + y_left) .* (x_right - x_left));
e_out =

% Assemble vectors back into triplet format
data_out = cat(2,x_out',y_out',e_out');

The problem is here:
y_out = (0.5 * (y_right + y_left) .* (x_right - x_left));

(the formula is for trapezoidal approx. for integrals)

It works fine for the first "integration", it finds the area under the first two points. But then it finds the area between the second pair of points, third, etc... without adding them up. That is, the area under the entire curve should be all the previous "areas" added up.

y_out should depend on what's before it, so I need to implement a "for loop" to successively integrate, starting from the first pair of points, up to the last, and to return the right "area". There are 99 right points, 99 left points, so this should be done 99 times... I know this isn't a great explanation, hopefully you understand what I mean. I've never used MATLAB before so I'm really desperately looking for some help here... thanks...

## Answers and Replies

BridgeBuilder

Couldnt you use the following? (I know nothing about MATLAB but I know programming)

y_out += (0.5 * (y_right + y_left) .* (x_right - x_left));

p-williams65

I tried it, no dice, I don't think that's a MATLAB command. Thanks for the post though.

BridgeBuilder

y_out = y_out + (0.5 * (y_right + y_left) .* (x_right - x_left));

Remember to set the initial value of y_out as 0 though.

p-williams65

Doesn't work either. I know a "for" loop would work, summing up the single "areas" to a specific value of x for each x, but I don't know how to implement it.

BridgeBuilder

In a real programmin glanguage you would use:

totalArea=0;

for (int i =0; i<100; i++) {
// set values of leftY, rightY, leftX and rightX here

// Note that their values are based on the current value of i

totalArea=totalArea+0.5*(leftY+rightY)*(leftX-rightX);
}

Mindscrape

Matlab's help is generally very useful. Is all you're asking about how to make a for loop in matlab?

You do it like so

x=0;
for j=1:100
x=x+j
end

this, for example, sums from one to one hundred. If you wanted to sum only odds you would do

x=0;
for j=1:2:99
x=x+j
end

if you wanted to sum certain numbers you would do

x=0;
for j=[1 8 12 4 9]
x=x+j
end

BridgeBuilder: Matlab is a real programming language.

viscousflow

ine for the first "integration", it finds the area under the first two points. But then it finds the area between the second pair of points, third, etc... without adding them up. That is, the area under the entire curve should be all the previous "areas" added up

Sorry I didn't realize your issue earlier. The problem is simple:

to add your integrated results use a code such as this.

Code:
%Code here
npts = end;
y_out = zeros(npts,1);
for i = 2:npts

y_out(i) = y_out(i-1) + (0.5 * (y_right(i-1) + y_left(i-1))* (x_right(i-1) - x_left(i-1)));

end

%Final area is equal to the last value
area = y_out(i);

Or just

Code:
y_out = (0.5 * (y_right + y_left) .* (x_right - x_left));
area=sum(y_out);

Either should work.

p-williams65

Thanks very much -viscous-, your first solution worked perfectly; now to look it over and learn from it :)

Last edited:
Mentor

Thanks very much vicious, your first solution worked perfectly; now to look it over and learn from it :)
He's not vicious, just thick (which is roughly what viscous means).

Last edited:
p-williams65

Haha. Just woke up, haven't had my coffee yet!