MATLAB - for loop for numerical integration

AI Thread Summary
The discussion focuses on implementing a "for" loop in MATLAB for numerical integration using the trapezoidal rule. The initial code correctly calculates the area under the first pair of points but fails to accumulate the areas for subsequent pairs. A solution was proposed to initialize an output vector and use a loop to sum the areas iteratively, ensuring each new area is added to the previous total. Participants shared examples of basic "for" loops in MATLAB to clarify the syntax and structure needed for the integration task. The final solution provided successfully addressed the integration issue, allowing for cumulative area calculation.
p-williams65
Messages
7
Reaction score
0
MATLAB - "for" loop for numerical integration

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

% Explode triplet format into 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...
 
Physics news on Phys.org


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));
 


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


How about

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.
 


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.
 


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);
}
 


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.
 


p-williams65 said:
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.
 


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


p-williams65 said:
Thanks very much vicious, your first solution worked perfectly; now to look it over and learn from it :)
He's not vicious, just thick:wink: (which is roughly what viscous means).
 
Last edited:
  • #11


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

Similar threads

Replies
4
Views
2K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
12
Views
2K
Replies
5
Views
3K
Replies
5
Views
3K
Replies
1
Views
3K
Back
Top