I have two column vectors y and x full of data points, they are of the same size. Is it possible to plot the column vector y against the column vector x? Is it possible to integrate y against x?


2 Answers 2


Let's say we have the following data (sampled Sine function):

n = 30;
x = Table[t, {t, 0, 4 π, π/10}];
y = N[Sin[x]];

y against x can be plotted with ListPlot and Transpose can be used to create a list of {xi, yi} coordinates

ListPlot[Transpose[{x, y}], Joined -> True, Mesh -> All]

enter image description here

The integration depends of the above data can be done - for example - with the trapezoidal rule. Assuming {x1, y1} and {x2, y2} are two successive points, the area of the trapezoid is:

TrapezoidArea[{{x1_, y1_}, {x2_, y2_}}] := (y1 + y2)/2/(x2 - x1)

All we need is to create point pairs with Partition, Map (/@) the above function and use Total to get the result:

pointPairs = Partition[Transpose[{x, y}], 2, 1];
Total[TrapezoidArea /@ pointPairs]

As you can imagine, the result will be 0.

Of course, the above can be done in a single function:

TrapezoidalIntegrate[x_List, y_List] := 
(#[[1, 2]] + #[[2, 2]])/2/(#[[2, 1]] - #[[1, 1]])& /@ 
   Partition[Transpose[{x, y}], 2, 1] // Total
 data = MapThread[List,{Data,xData}]
 line = Fit[data, {1, x}, x]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.