Integration of a curve

217 次查看(过去 30 天)
jchris14
jchris14 2014-5-23
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

采纳的回答

Mahdi
Mahdi 2014-5-23
You can use the trapz function to give you the area under the curve. In this case,
trapz(x,y)
  4 个评论
jchris14
jchris14 2014-5-23
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
Mahdi 2014-5-23
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.

请先登录,再进行评论。

更多回答(1 个)

Sara
Sara 2014-5-23
You can use
trapz
  1 个评论
Tayyaba Bano
Tayyaba Bano 2022-3-28
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;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by