# Integration of a curve

229 visualizaciones (últimos 30 días)
jchris14 el 23 de Mayo de 2014
Comentada: Tayyaba Bano el 28 de Mzo. de 2022
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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Mahdi el 23 de Mayo de 2014
You can use the trapz function to give you the area under the curve. In this case,
trapz(x,y)
##### 4 comentariosMostrar 2 comentarios más antiguosOcultar 2 comentarios más antiguos
jchris14 el 23 de Mayo de 2014
I tried verifying it. I plotted a simple function, y=x^2. I used int(y,1,2) and found the area under the curve to be 2.33. now when I took certain points from the same curve. in this case x=[-3 -2 0 2 3],y=[10 4 0 4 10] and used trapz(x,y), I got the value 22. Is there a way around this issue?
thanks
Mahdi el 23 de Mayo de 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.

Iniciar sesión para comentar.

### Más respuestas (1)

Sara el 23 de Mayo de 2014
You can use
trapz
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Tayyaba Bano el 28 de Mzo. de 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;
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;
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;

Iniciar sesión para comentar.

### Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by