average multiple curves matlab

Hello every body I have multiple xy curves extacted from experiments results (X1 vs Y1, X2 vs Y2,......X5 vs Y5), I looking for method to plot the Xavg vs Yavg. Thanks

 Accepted Answer

Star Strider
Star Strider on 19 Jun 2016
Use interp1.

4 Comments

Hi Star Strider Thank You for your answer, but I am beginner with Matlab i need more clarification (How i can use interp1)
My pleasure.
If you want to average the curves, you have to average them at the same x-values. So set up an appropriate vector for the x-values (none of the curves have to have those values), then use that vector with the x and y values for each curve, and use interp1, to interpolate to create a common set of points on the curves, then average them at those points.
I would do something like this:
x1 = sort(rand(1, 10)); % Create Data
y1 = rand(size(x1)); % Create Data
x2 = sort(rand(1, 15)); % Create Data
y2 = rand(size(x2)); % Create Data
xi = linspace(0, 1, 50); % Create Vector Of Common X-Values
y1i = interp1(x1(:), y1(:), xi(:), 'linear', 'extrap'); % Interploate Or Extrapolate To New ‘x’ Values
y2i = interp1(x2(:), y2(:), xi(:), 'linear', 'extrap'); % Interploate Or Extrapolate To New ‘x’ Values
y_mean = mean([y1i y2i], 2); % Mean Of Y Values
figure(1)
plot(x1, y1, '-b')
hold on
plot(x2, y2, '-g')
plot(xi, y_mean, '-r', 'LineWidth',2)
hold off
grid
legend('Data 1', 'Data 2', 'Data Mean', 'Location','N')
how would you do this for 50 curves?
I would use a for loop.
The code would have to be changed to accommodate that, and to put the curves in a cell array and then index them as such in the loop and then average them (since the point here is that the curves have different dimensions, and so would not be in a matrix).
It also assumes that the ‘x’ vectors cover the same range (initial and final values), so the interpolation would simply equalise the number of elements in each of them, and the associated ‘y’ vectors. If that were not the situation, this would become more complicated, and likelly considerably so.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!