MATLAB - for loop for numerical integration

Click For Summary

Discussion Overview

The discussion revolves around implementing a "for" loop in MATLAB for numerical integration using the trapezoidal rule. Participants explore how to accumulate areas under a curve by summing successive trapezoidal areas calculated from given data points.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes an initial MATLAB function that calculates areas under a curve using trapezoidal approximation but does not accumulate the areas.
  • Another participant suggests using an increment operator to add areas, but this is not recognized as valid MATLAB syntax by the original poster.
  • A different participant proposes initializing the output variable and using a "for" loop to sum the areas iteratively, emphasizing the need to set the initial value of the output variable.
  • One participant provides a general structure for a "for" loop in programming, illustrating how to sum values, which may not directly address the MATLAB-specific implementation needed.
  • Another participant offers a specific MATLAB code snippet that correctly implements the accumulation of areas using a "for" loop, suggesting two potential methods for achieving the desired result.
  • The original poster expresses gratitude for the provided solution, indicating that it worked as intended.

Areas of Agreement / Disagreement

Participants generally agree on the need for a "for" loop to accumulate areas, but there are multiple approaches suggested, and the discussion reflects varying levels of familiarity with MATLAB syntax.

Contextual Notes

Some participants demonstrate uncertainty regarding MATLAB commands and syntax, leading to different interpretations of how to implement the solution effectively.

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 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
27
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K