# Hello, I need help interpolating data with two lines for each baffle cut percentage in MATLAB. How to select and interpolate the right line automatically based on user input?

6 views (last 30 days)
Subathra Nilamegan on 9 Nov 2023
Hi MATLAB community,
I'm facing a challenge with interpolating data that consists of two lines for different baffle cut percentages. I have a dataset with different baffle cut percentages, such as 15%, 25%, 35%, and 45%, and for each percentage, there are two lines of data.
For a single line dataset, I understand how to perform interpolation. However, I'm unsure how to handle the case where there are two lines for each baffle cut percentage, and I need to interpolate based on the user's selection of the baffle cut percentage. pls help.. thank you..
BC15 = segmentalbafflesgraph1525.curve15;
FRe = segmentalbafflesgraph1525.x;
BC25 = segmentalbafflesgraph1525.curve25;
SRe = segmentalbafflesgraph3545.x;
BC35 = segmentalbafflesgraph3545.Curve35;
BC45 = segmentalbafflesgraph3545.Curve45;
TRe = segmentalbafflesgraphsmalllines.x;
SL15 = segmentalbafflesgraphsmalllines.smallLine15;
SL25 = segmentalbafflesgraphsmalllines.smallLine25;
SL35 = segmentalbafflesgraphsmalllines.smallLine35;
SL45 = segmentalbafflesgraphsmalllines.smallLine45;
% Remove duplicate x values and smooth lines for all data sets
[xUnique1, idxUnique1] = unique(FRe);
yUnique1 = BC15(idxUnique1); % Use BC15 instead of RE15
[xUnique2, idxUnique2] = unique(SRe);
yUnique2 = BC35(idxUnique2); % Use Re35 instead of SRe15
[xUnique3, idxUnique3] = unique(FRe);
yUnique3 = BC25(idxUnique3);
[xUnique4, idxUnique4] = unique(SRe);
yUnique4 = BC45(idxUnique4);
[xUnique5, idxUnique5] = unique(TRe);
yUnique5 = SL15(idxUnique5);
[xUnique6, idxUnique6] = unique(TRe);
yUnique6 = SL25(idxUnique6);
[xUnique7, idxUnique7] = unique(TRe);
yUnique7 = SL35(idxUnique7);
[xUnique8, idxUnique8] = unique(TRe);
yUnique8 = SL45(idxUnique8);
% log-log plot
loglog(FRe, BC15, 'b-', 'DisplayName', 'Baffle Cut 15'); % Blue
hold on;
loglog(SRe, BC35, 'g-', 'DisplayName', 'Baffle Cut 35'); % Green
loglog(xUnique3, yUnique3, 'm-', 'DisplayName', 'Baffle Cut 25 '); % Magenta
loglog(xUnique4, yUnique4, 'k-', 'DisplayName', 'Baffle Cut 45');
loglog(xUnique5, yUnique5, 'b-');
loglog(xUnique6, yUnique6, 'm-');
loglog(xUnique7, yUnique7, 'g-');
loglog(xUnique8, yUnique8, 'k-');
xlabel('X-axis (log scale)');
ylabel('Y-axis (log scale)');
% Create legend
legend('Baffle Cut 15', 'Baffle Cut 25','Baffle Cut 35','Baffle Cut 45', 'Location', 'NorthWest');
xlabel('Reynolds Number (Re)');
ylabel('Y-axis Label');
legend('show');
grid on;
title('Log-Log Plot for Baffle Cut Data');
hold off;
xInterpolate = Res;
selectedDataSet = input('Choose Baffle Cut(%) (15, 25, 35 or 45):');
if selectedDataSet == 15
xData = [xUnique1; xUnique5];
yData = [yUnique1; yUnique5];
dataSetName = 'Data Set 1';
elseif selectedDataSet == 35
xData = [xUnique;2 xUnique7];
yData = [yUnique2; yUnique7];
dataSetName = 'Data Set 2';
elseif selectedDataSet == 25
xData = [xUnique3; xUnique6];
yData = [yUnique3; yUnique6];
dataSetName = 'Data Set 3';
elseif selectedDataSet == 45
xData = [xUnique4; xUnique8];
yData = [yUnique4; yUnique8];
dataSetName = 'Data Set 4';
else
error('Invalid data set selection. Choose 15, 25, 35 or 45.');
end
% Interpolate y-value for the selected data set
yInterpolate = interp1(xData, yData, xInterpolate, 'linear');
fprintf('Interpolated Y-value for %s: %f\n', dataSetName, yInterpolate);
% Plot the original data for all data sets
plot(xUnique1, yUnique1, 'b-', 'DisplayName', 'Data Set 1 (BC15) Line 1'); % Blue line
plot(xUnique1, yUnique5, 'b--', 'DisplayName', 'Data Set 1 (SL15) Line 2'); % Blue dashed line
plot(xUnique2, yUnique2, 'g-', 'DisplayName', 'Data Set 2 (BC35) Line 1'); % Green line
plot(xUnique2, yUnique7, 'g--', 'DisplayName', 'Data Set 2 (SL35) Line 2'); % Green dashed line
plot(xUnique3, yUnique3, 'm-', 'DisplayName', 'Data Set 3 (BC25) Line 1'); % Magenta line
plot(xUnique3, yUnique6, 'm--', 'DisplayName', 'Data Set 3 (SL25) Line 2'); % Magenta dashed line
plot(xUnique4, yUnique4, 'c-', 'DisplayName', 'Data Set 4 (BC45) Line 1'); % Cyan line
plot(xUnique4, yUnique8, 'c--', 'DisplayName', 'Data Set 4 (SL45) Line 2'); % Cyan dashed line
% Plot the interpolated point for the selected data set with a red circle
scatter(xInterpolate, yInterpolate, 50, 'ro', 'filled', 'DisplayName', 'Interpolated Point');
% labels and a legend
xlabel('X-axis');
ylabel('Y-axis');
legend('show');
% Plot lines connecting the original data points to the interpolated point for the selected data set
plot([xInterpolate, xInterpolate], [min(yData), yInterpolate], 'r--', 'LineWidth', 2, 'DisplayName', 'Interpolation Line (X)');
plot([min(xData), xInterpolate], [yInterpolate, yInterpolate], 'r--', 'LineWidth', 2, 'DisplayName', 'Interpolation Line (Y)');
% legend
legend('Data Set 1', 'Data Set 2', 'Data Set 3','Data Set 4', 'Location', 'northeast');
% Label the axes and add a title
xlabel('X-axis');
ylabel('Y-axis');
title('Interpolation Example');
grid on;
hold off
Image Analyst on 9 Nov 2023
What do you mean by "baffle"? To me it means either to
1. stump, confuse, or puzzle someone, or
2. to block or redirect something (such as light or liquid) from going somewhere you don't want it to go.
William Rose on 9 Nov 2023
@Image Analyst, the term apparently refer to a barrier inside a heat exchanger.
@Subathra Nilamegan, Please post the problem in the simplest version necessary to allow others to help you. Please post code that is the minimal code necessary to ilustrate the problem. Please provide a simple data file that contains the data from which you will interpolate.
One-dimension interpolation requires two vectors of input data - the known x values and known y values. It also requires a scalar or vector of x-value(s) where you wish to interpolate y-values. I suspect that your problem has to do with the structure of the input data. Therefore please tell us whatever you can about the structure of the input data.

Mathieu NOE on 9 Nov 2023
Edited: Mathieu NOE on 9 Nov 2023
as far as I understand , when you say your data has two line you mean for example the left and right segments we can see on your plot with a discontinuity at Re = 10^5 in between
I also believe that each curve (two segments) is generated by concatenating two datas as I see here for example for dataser 15% (same for the others datasets):
if selectedDataSet == 15
xData = [xUnique1; xUnique5];
yData = [yUnique1; yUnique5];
in that case it should be fairly simple to use one or the other data set (for each curve) depending whereas you are below or above Re = 10^5
my suggestion so far (nothing fancy !) : I creatd somme dummy data as we don't have yours
% dummy data
xUnique1 = logspace(1,5,100);
yUnique1 = 1./xUnique1;
xUnique5 = max(xUnique1).*logspace(0,1,10)+1; % +1 to have unique x data
yUnique5 = 100./xUnique5;
% combine
x = [xUnique1 xUnique5];
y = [yUnique1 yUnique5];
% select x data to use for interpolation
Rei = 5e5; % try with different values
if Rei>=min(xUnique1) && Rei<=max(xUnique1) % case 1 : x is in the range of xUnique1
yi = interp1(xUnique1,yUnique1,Rei);
elseif Rei>=min(xUnique5) && Rei<=max(xUnique5) % case 2 : x is in the range of xUnique5
yi = interp1(xUnique5,yUnique5,Rei);
else
disp('error : x not in valid range')
end
% plot
loglog(x,y,'*',Rei,yi,'dr');
Result for xi = 10^4 (below the threshold Re = 10^5) :
yi = 1.0016e-04
Result for xi = 5*10^5 (above the threshold Re = 10^5) ::
yi = 2.0256e-04
Subathra Nilamegan on 27 Nov 2023
Thank you so much..yes the problem solved.
Subathra Nilamegan on 27 Nov 2023
thank you for your help. Do you know how to load .mat file in app designer for plotting?

### Categories

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