How to plot the average of the graphs ?

20 views (last 30 days)
Hi,
I have plotted a 3 plots on the same axis and now I want to plot the average of these plots. All my three
vectors that have used have different lenghts. I belive that here we need to interpolate. I want to interpolate my YS3, YS4Q and YS 21Q where I want to use common Lattitude values LatCom, however Im confused becuse I'm not able interploate. Im attaching my plots, code and the error that appeares while interpolating. Im new to matlab and kindly asking for your generous support.
% 3 Combine Plots
P3 = plot(Lat3,YS3, '-r',Lat4Q,YS4Q, '-k',Lat21Q,YS21Q, '-k');
set(P3,{'LineWidth'},{2;1;1});
xlim([-30 30]);
xticks(-30:10:30)
ylim([0 60])
yticks(0:10:50)
grid
box on
ax = gca;
ax.LineWidth = 2;
title('14/01 ~12.20hrs Log +159','FontSize',10)
% Interploate
LatCom = [-30: 0.01: 30]';
y1 = interp1(Lat3,YS3, LatCom); % Im getting a erro as shown below
%Sample points must be unique.
%Error in interp1 (line 188)
%VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);
  2 Comments
the cyclist
the cyclist on 28 Aug 2022
It would be helpful if you uploaded the data in a MAT file. You can use the paperclip icon in the INSERT section of the toolbar.
Vikash Raj
Vikash Raj on 28 Aug 2022
Hi I have attached the data as 'Sample data' please find attached

Sign in to comment.

Accepted Answer

Chunru
Chunru on 28 Aug 2022
Edited: Chunru on 29 Aug 2022
load(websave("SampleData", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1110305/Sample%20data.mat"))
%whos
P3 = plot(Lat3,YS3, '-r',Lat4Q,YS4Q, '-k',Lat21Q,YS21Q, '-k');
set(P3,{'LineWidth'},{2;1;1});
%xlim([-30 30]);
xticks(-30:10:30)
%ylim([0 20])
yticks(0:10:50)
grid
box on
ax = gca;
ax.LineWidth = 2;
title('14/01 ~12.20hrs Log +159','FontSize',10)
LatCom = [-30: 0.01: 30]';
% Interpolation requires unique points
[Lat3u, ia] = unique(Lat3); YS3u = YS3(ia);
y1 = interp1(Lat3u,YS3u, LatCom, 'linear', 'extrap');
[Lat4Qu, ia] = unique(Lat4Q); YS4Qu = YS4Q(ia);
y2 = interp1(Lat4Qu,YS4Qu, LatCom, 'linear', 'extrap');
[Lat21Qu, ia] = unique(Lat21Q); YS21Qu = YS21Q(ia);
y3 = interp1(Lat21Qu,YS21Qu, LatCom, 'linear', 'extrap');
ym = (y1+y2+y3)/3;
hold on
plot(LatCom, ym, 'b', 'LineWidth', 2)

More Answers (0)

Categories

Find more on Graphics 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!