Question on subtracting two graph(surface)
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Sandeep Mishra
2024 年 12 月 4 日
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?
回答 (1 件)
Divyam
2024 年 12 月 12 日
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
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!