MATLAB - for loop for numerical integration

In summary, the author is looking for a way to add the results of a numerical integration to a vector. He has tried three different solutions, all of which have failed. He is now looking for help.
  • #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...
 
Physics news on Phys.org
  • #2


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));
 
  • #3


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


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.
 
  • #5


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.
 
  • #6


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


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.
 
  • #8


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.
 
  • #9


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!
 

What is a for loop in MATLAB?

A for loop in MATLAB is a programming construct that allows for repeated execution of a set of instructions. It is commonly used for tasks that require a certain number of repetitions or iterations, such as numerical integration.

How do you use a for loop for numerical integration in MATLAB?

To use a for loop for numerical integration in MATLAB, you first need to define the function that you want to integrate. Then, you can use a for loop to divide the integration interval into smaller sub-intervals and calculate the area under the curve for each sub-interval. Finally, you can sum up all the sub-intervals to get the total area, which is the numerical approximation of the integral.

What are the advantages of using a for loop for numerical integration in MATLAB?

One advantage of using a for loop for numerical integration in MATLAB is that it allows for flexibility in the choice of integration interval and the number of sub-intervals. This can be especially useful when dealing with complex functions that cannot be integrated analytically. Additionally, using a for loop allows for easy modification and testing of different integration methods.

What are the limitations of using a for loop for numerical integration in MATLAB?

One limitation of using a for loop for numerical integration in MATLAB is that it can be time-consuming for large integration intervals or a high number of sub-intervals. This is because the for loop needs to iterate through each sub-interval, which can slow down the calculation process. Additionally, the accuracy of the approximation depends on the number of sub-intervals used, so a large number of iterations may be necessary for a more precise result.

Are there alternative methods for numerical integration in MATLAB?

Yes, there are alternative methods for numerical integration in MATLAB, such as the built-in functions 'trapz' and 'quad'. These methods use different numerical integration techniques, such as the trapezoidal rule and Simpson's rule, to approximate the integral. They may be more efficient and accurate than using a for loop, especially for complex functions or large integration intervals.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Classical Physics
Replies
13
Views
980
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Atomic and Condensed Matter
Replies
4
Views
1K
Back
Top