Question on subtracting two graph(surface)

3 views (last 30 days)
Young Chan Jung
Young Chan Jung on 4 Dec 2024
Answered: Divyam on 12 Dec 2024
Hello,
Say I have line 'A' that has coordinate (0,0) , (1,1) and line 'B' that has coordinate (0.5, 0.5) , (1.5, 1.5)
this can be two lines, or parts of two different surfaces.
I want to subtract these two, and the result should give horizontal line at y=0 between x = 0.5~1, right?
However, the only way I can think of subracting is by cooridnates.
I have many data points, so interpolating each line won't work.
Is there a way to subtract the whole line instead of subtracting coordinate by coordinate?
Thank you
  2 Comments
Sandeep Mishra
Sandeep Mishra on 4 Dec 2024
Based on the data points for lines 'A' and 'B', it seems they both lie on the line y = x.
Could you clarify what you mean by subtracting these two lines? What result are you expecting?
Young Chan Jung
Young Chan Jung on 4 Dec 2024
Thank you for the answer.
I have surface data, before and after the wear test. there is some parts that were not worn, which should match to each other's profle. Well, at least should be within certain error.
However, The resolution of each points were not close enough that the points were collected in different places.
Just for example, there is surface asperity profile data that has the slope of y=x. Say the total length of this asperity slop was from x = 0 to x = 1.5.
The problem is, the two data set gave me the cooridnate points that are offsetted, measuing at x=0, x=1 for data 'A' and x=0.5 x=1.5 for data 'B'. In other words, points are separated by 1, but 'A' and 'B' collected by the offset of 0.5.
I cannot just shift B by -0.5, because the profile itself are alinged. just measured points were offsetted
In this case if the lines between the points can be subtracted, it should give y=x for x=0~0.5, 1~1.5, and y=0 for x=0.5~1. I am assuming straight line interpolation between the points.
I cannot find a way to do this, but only choice I have is subtracting point by point, say shift 'B' data by x=-0.5 and subtract each point. This will give y=0 for x= 0~1, which is not what I want.
I heard of using somekind of mesh to kinda interpolate, but not sure how to do it
Thank you

Sign in to comment.

Answers (1)

Divyam
Divyam on 12 Dec 2024
As stated in your comments, you need to interpolate the y-coordinate data for a common set of x-coordinate data before attempting to subtract the two lines to obtain the desired outcome. For the interpolation process, you can use the "interp1" function on the common grid. After that, you need to ensure that the final data for the y-coordinates aligns well with the overlap limits you set for both the A and B surfaces. Here is some sample code to help you with this process:
% Original data points
xA = [0, 1];
yA = [0, 1]; % For line A
xB = [0.5, 1.5];
yB = [0.5, 1.5]; % For line B
% Define a common grid
xCommon = linspace(min([xA, xB]), max([xA, xB]), 1000);
% Interpolate both lines onto the common grid
yA_interp = interp1(xA, yA, xCommon, 'linear', 'extrap');
yB_interp = interp1(xB, yB, xCommon, 'linear', 'extrap');
% Initialize the difference array
yDiff = zeros(size(xCommon));
% Calculate the difference only in the overlapping region
overlapStart = 0.5;
overlapEnd = 1.0;
overlapIndices = (xCommon >= overlapStart) & (xCommon <= overlapEnd);
% In the overlapping region, subtract the interpolated values
yDiff(overlapIndices) = yA_interp(overlapIndices) - yB_interp(overlapIndices);
% Outside the overlapping region, retain the original profile of yA
yDiff(~overlapIndices) = yA_interp(~overlapIndices);
% Plot the result
figure;
plot(xCommon, yDiff, 'b', 'LineWidth', 2);
xlabel('x');
ylabel('Difference (yA - yB)');
title('Difference between Line A and Line B');
axis equal; % Ensure equal scaling for x and y axes
grid on;
For more information regarding the "interp1" function, refer to the following documentation: https://www.mathworks.com/help/matlab/ref/double.interp1.html

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!