# Finding weighted sum of multiple curves

조회 수: 2(최근 30일)
Deepa Maheshvare 2022년 7월 5일
편집: Adam Danz 2022년 7월 7일
I have a dataset with values of multiple curves. An example plot is shown below.
I want to scale the curves (move up/down) so that all curves overlap.
The following is a sample dataset which includes that data points corresponding to 5 curves and coordinate inputs below
scale = 1.5;
x1 = [0,4,6,10,15,20]*scale;
y1 = [18,17.5,13,12,8,10];
x2 = [0,10.5,28]*scale;
y2= [18.2,10.6,10.3];
x3 = [0,4,6,10,15,20]*scale;
y3 = [18,13,15,12,11,9.6];
x4 = [9,17,28]*scale;
y4 = [5,5.5,7];
x5 = [1,10,20]*scale;
y5 = [3,0.8,2];
plot(x1,y1, '*-', x2, y2, '*-', x3, y3, '*-', x4, y4, '*-', x5, y5, '*-')
To scale the curves, I need to find the scale factor by defining a target curve.
I'm not sure of the ways in which the target curve can be defined. Would it be a good
approach to compute the weighted sum? Since the x scale is different for each curve, I am not sure
how to define an average/ target curve.
Suggestions will be really appreciated.
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Deepa Maheshvare 2022년 7월 7일
I a sorry, I don't have a clear idea of how the curve should like. But I am looking for a mean curve like the below

댓글을 달려면 로그인하십시오.

### 채택된 답변

편집: Adam Danz 2022년 7월 7일
The first step of applying a weighted sum is to define the weights so until you know that, you can't use that method.
Instead, another option is to interpolate each curve so that the curves have the same x-coordinates within the range of each curve. Then you can just average the y values.
Data from OP question
scale = 1.5;
x1 = [0,4,6,10,15,20]*scale;
y1 = [18,17.5,13,12,8,10];
x2 = [0,10.5,28]*scale;
y2= [18.2,10.6,10.3];
x3 = [0,4,6,10,15,20]*scale;
y3 = [18,13,15,12,11,9.6];
x4 = [9,17,28]*scale;
y4 = [5,5.5,7];
x5 = [1,10,20]*scale;
y5 = [3,0.8,2];
Plot initial data
fig = figure;
ax = cla(fig);
plot(ax, x1,y1, '*-', x2, y2, '*-', x3, y3, '*-', x4, y4, '*-', x5, y5, '*-', 'markersize', 12)
grid(ax,'on')
Interpolate x and y
xi are the interpolated x values, all curves will have the same xi. You can define xi differently but it should span the range of your data which is currently 0 to 42.
yi are the interpolated y values. NaNs are mising values for curves that do not span the entire xi range.
xi = 0 : 1 : 42; % your choice but should span the range of your data
y1i = interp1(x1,y1,xi);
y2i = interp1(x2,y2,xi);
y3i = interp1(x3,y3,xi);
y4i = interp1(x4,y4,xi);
y5i = interp1(x5,y5,xi);
Plot the interpolated values
This step isn't necessary but it's always good to get visual feedback that the process makes sense. Here, vertical "pipes" are placed at the interpolated points. Compare them to the original points with "*".
hold(ax, 'on')
ax.ColorOrderIndex = 1; % reset color order to use the same colors as above
plot(ax, xi,y1i, '|-', xi, y2i, '|-', xi, y3i, '|-', xi, y4i, '|-', xi, y5i, '|-', 'markersize',4)
Average the interpolated y coordinates
yAvg = mean([y1i;y2i;y3i;y4i;y5i],'omitnan');
Add the average line to the plot
plot(xi, yAvg, 'r--','LineWidth',2)

댓글을 달려면 로그인하십시오.

### 범주

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by