问题
Is it possible to "invert" a series of data like when a function is inverted?
Let me explain. If I have a function that I can plot in MATLAB, I can always (almost) find the inverse of such function with the finverse
and the plot would provide this:
But I do not have the a function for my data, and I would still like to find a sort of inverse of such series of data, like for example y=f(x)
where
x=[0.0050879 0.0056528 0.0062176 0.0067822 0.0073467 0.0079111 0.0084752 0.0090392 0.0096030 0.0101666 0.0107299 0.0112930 0.0118558 0.0124184 0.0129807 0.0135428 0.0141045 0.0146659 0.0152270 0.0157877 0.0163481];
and
y=[0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 4.4901e-05 4.4901e-05 8.9801e-05 1.3470e-04 1.7960e-04 2.2450e-04 2.6940e-04 3.5920e-04 4.0411e-04 4.9391e-04 5.8371e-04];
Is this kind of operation on a series of data possible in MATLAB? Is there a technique/method/function that handles this?
NOTE: I previously tried to "fit" the series of data with a polynomial via the polyfit
function and then invert it via the finverse
function; but it results in an f^-1
function that is dependent on both x
and y
at the same time. And because x
is not even spaced homogeneously, I do not even know how to plot it...
回答1:
Inverting a function represented by a series of (x, y)
data points is done by changing the position of x
and y
: (y, x)
. This can be visualised in Matlab as follows:
figure
hold on
plot(x, y, 'red')
plot(y, x, 'blue')
from = min(min(x), min(y));
to = max(max(x), max(y));
plot([from to], [from to], 'k--')
legend('Original', 'Inverted')
回答2:
Since the inverse of a function is simply the reflection of the function about axis y=x, you can achieve this by transformation.
clear;close all
x=[0.0050879 0.0056528 0.0062176 0.0067822 0.0073467 0.0079111 0.0084752 0.0090392 0.0096030 0.0101666 0.0107299 0.0112930 0.0118558 0.0124184 0.0129807 0.0135428 0.0141045 0.0146659 0.0152270 0.0157877 0.0163481];
y=[0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 4.4901e-05 4.4901e-05 8.9801e-05 1.3470e-04 1.7960e-04 2.2450e-04 2.6940e-04 3.5920e-04 4.0411e-04 4.9391e-04 5.8371e-04];
x_ori = x; y_ori = y;
figure; hold on;axis equal;grid on;axis tight;
plot(x,y);ls={'ori'};
plot(x,x,'-.');ls{end+1}='y=x';
%rotate x and y about z by -45 degree
rotz1=[cosd(45) sind(45) 0; -sind(45) cosd(45) 0; 0 0 1];
data = [x(:) y(:) ones(size(x(:)))]*rotz1;
x = data(:,1); y = data(:,2);
%plot(x,y,'--');ls{end+1}='rotated';
%flip x
x = -x;
%plot(x,y,'--');ls{end+1}='flipped';
%rotate x and y back
rotz2=[cosd(-45) sind(-45) 0; -sind(-45) cosd(-45) 0; 0 0 1];
data = [x(:) y(:) ones(size(x(:)))]*rotz2;
x = data(:,1); y = data(:,2);
plot(x,y);ls{end+1}='result';
%% Or a transformation matrix.
flipx = eye(3); flipx(1) = -1;
TM = rotz1*flipx*rotz2;
data = [x_ori(:) y_ori(:) ones(size(x(:)))]*TM;
x = data(:,1); y = data(:,2);
plot(x,y,'--');ls{end+1}='result by TM';
legend(ls);
Update
I was so into transformation...Then I found the transformation matrix is simply
| 0 1 0 |
T = | 1 0 0 |
| 0 0 1 |
which is basically swapping x and y....and yes, that is the fundamental thing about the inverse of a function.
So simply:
TM = [0,1,0;1,0,0;0,0,1];
data = [x_ori(:) y_ori(:) ones(size(x(:)))]*TM;
x = data(:,1); y = data(:,2);
and yes, a blow to my pride, @m7913d's answer is much simpler.
来源:https://stackoverflow.com/questions/43690225/invert-a-series-of-data-as-if-inverting-a-function-using-matlab