- #1
p-williams65
- 7
- 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...
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...