Integration of a curve
    88 views (last 30 days)
  
       Show older comments
    
hello, I have a X and y data points. ex: x=[2 3 5]; y=[3 6 3];
after plotting this simple curve, how do I integrate this curve? All the syntax i find on matlab integrates symbolically.
thanks in advance
0 Comments
Accepted Answer
  Mahdi
      
 on 23 May 2014
        trapz(x,y)
4 Comments
  Mahdi
      
 on 23 May 2014
				You integration limits aren't the same so the results are different; as expected. In the first case, you're using int(y,1,2) to integrate from the domain of x=1 to x=2. In the second case, you're integrating from x=-3 to x=4.
To get the same values, look at this example:
syms x1
y=x1.^2;
int(y,1,2) % This gives an answer of 2.33 as you said.
% For the second case using trapz
x=1:0.01:2 %Goes from 1 to 2 by a step size of 0.01
y=x.^2; % Generates the y-data
trapz(x,y) %Gives a result of 2.33 as required
Please note that there are round-off and numerical errors that you should be aware of.
More Answers (1)
  Sara
      
 on 23 May 2014
        You can use
trapz
1 Comment
  Tayyaba Bano
 on 28 Mar 2022
				Hi, I need to find the intergal of a curve.
my code involves FOR loop, should I use the trapz command inside the loop or outside the loop to calculate the integral? I tried both but none of them is working.
Moreover, my curve also showing the negative values for the same positive values, how can I avoid these?
I am attaching my curve and the code.
clear all;
close all;
%save 20m3_hr_centre u_original;
%save pos y;
%%load Variables 
LS_pos = 'center';
Q = 10;
x_pos = 35;  %position of vertical line velocity
%% plot velocity profiles at x_pos
figure(1)
for jj=1:1:10
        Q = 10;
        PIV(jj)=load(['250mm_',num2str(Q),'m3h_',LS_pos]);       %load images
        avg(jj) = length(PIV(jj).x);                        %get data length
%% extract average values        
        u_avg(:,:,jj) = PIV(jj).u_component{avg(jj),1}(:,:);
        v_avg(:,:,jj) = PIV(jj).v_component{avg(jj),1}(:,:);       
        u_avg_x_pos(:,jj) = PIV(jj).u_component{avg(jj),1}(:,x_pos);  % extract u-component at x_pos
        x_coord = max(PIV(jj).y{avg(jj),1}(:,x_pos))-PIV(jj).y{avg(jj),1}(:,x_pos); %get and invert vertical coordinate
         if jj>6
            plot(x_coord,u_avg_x_pos(:,jj)*-1);
        else
        plot(x_coord,u_avg_x_pos(:,jj));
         end
         hold on;
end
trapz(x_coord,u_avg_x_pos(:,jj));
  xlabel('x [m]');
ylabel(' u ');
%axis([0 0.12 0 1.2])
hold off;
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!