• Support PF! Buy your school textbooks, materials and every day products Here!

MATLAB - for loop for numerical integration

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

Re: MATLAB - "for" loop for numerical integration

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));
 
Re: MATLAB - "for" loop for numerical integration

I tried it, no dice, I don't think that's a MATLAB command. Thanks for the post though.
 
Re: MATLAB - "for" loop for numerical integration

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.
 
Re: MATLAB - "for" loop for numerical integration

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.
 
Re: MATLAB - "for" loop for numerical integration

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);
}
 
1,860
0
Re: MATLAB - "for" loop for numerical integration

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.
 
272
0
Re: MATLAB - "for" loop for numerical integration

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.
 
Re: MATLAB - "for" loop for numerical integration

Thanks very much -viscous-, your first solution worked perfectly; now to look it over and learn from it :)
 
Last edited:
32,935
4,642
Re: MATLAB - "for" loop for numerical integration

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:
Re: MATLAB - "for" loop for numerical integration

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

Related Threads for: MATLAB - for loop for numerical integration

Replies
4
Views
1K
  • Last Post
Replies
3
Views
21K
  • Last Post
Replies
8
Views
1K
  • Last Post
Replies
22
Views
5K
Replies
1
Views
3K
  • Last Post
Replies
0
Views
5K
  • Last Post
Replies
1
Views
1K
Top