Matlab interp1 function x-points for given y-points

Dear Community Members,
Hopefully this finds you safe and well. Using interp1 to acquire x-points for given y-points does not seem to work. Data in this particular instance generated via a function however in reality it is experimental data.
%**main function
x=[3:0.1:6];
y=sin(x);
%***y-array for new x-values
y_array = [-0.9:0.005:0.1];
x_new = interp1(y,x,y_array);
figure
plot(x,y,'k+-',x_new,y_array,'rx-')
grid on
Resutant image see attachment.
Any help on how to extrapolate between current and next data opints would be appreciated.

 Risposta accettata

Inverting nonlinear function using interp1() like this is not correct. Check the following code
%**main function
x=[3:0.1:6];
y=sin(x);
%***y-array for new x-values
y_array = -0.9:0.005:0.1;
interp_x = @(xq) interp1(x, y, xq);
x_new = fsolve(@(xq) interp_x(xq) - y_array, 3+rand(size(y_array)));
figure
plot(x,y,'k+-',x_new,y_array,'rx-')
grid on

4 Commenti

Dear Ameer,
What an amazing solution - thank you. I'm still trying to get my head around it to be honest.
Is there a way to extend this method to the rest of the curve? I guess this problem will occur if I have a nonlinear set of data i.e. 2 or more x-values for a given y-value potentially.
Thanks,
Mike
Yes, for functions with two x values for the same y-value, it can be difficult to find a solution. This is equivalent to finding multiple roots of a system of equations. There is no universal solver that can find all the roots. However, particular problems can have some specific features which can help in solving them. For example, in this case, we can see that there are exactly two y-values. fsolve() converges to the value closest to the initial point. If we give want two solutions, we need to provide two different initial points. For example
%**main function
x=[3:0.1:6];
y=sin(x);
%***y-array for new x-values
y_array = -0.9:0.005:0.1;
interp_x = @(xq) interp1(x, y, xq, 'linear', 'extrap');
x_new1 = fsolve(@(xq) interp_x(xq) - y_array, 3*ones(size(y_array)));
x_new2 = fsolve(@(xq) interp_x(xq) - y_array, 6*ones(size(y_array)));
figure
plot(x,y,'k+-',x_new1,y_array,'rx-',x_new2,y_array,'rx-')
grid on
But here you will notice that the solutions are going beyond the range of . The y-value at x=3 and x=6 are not equal. To only get solution in this range, following code can be used
%**main function
x=[3:0.1:6];
y=sin(x);
%***y-array for new x-values
y_array = -0.9:0.005:0.1;
interp_x = @(xq) interp1(x, y, xq);
x_new1 = fsolve(@(xq) interp_x(xq) - y_array, 3*ones(size(y_array)));
x_new2 = zeros(size(y_array));
valid_sols = zeros(size(y_array));
for i = 1:numel(x_new2)
[x_new2(i), ~, valid_sols(i)] = fsolve(@(xq) interp_x(xq) - y_array(i), 6);
end
idx = valid_sols > 0; % only get the valid solutions
figure
plot(x,y,'k+-',x_new1,y_array,'rx-',x_new2(idx),y_array(idx),'rx-')
grid on
Dear Ameer,
Thank you once again for all your help. In the end decided to write a script to trim the data as I realised I did not need the second part of the curve to reach my ultimate solution. This is effectively cheating by turning the non-linear function into a linear one (only one data point for a given y-value).
Thanks,
Mike
I am glad to be of help!

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by