Using interpolation with interp1/interp2

So at the moment, I have a piece of code that can be simplified to this:
v = linspace(0,20,200);
lambda = 7;
x_set = 0.4;
x = v+2*v.^2+3*(lambda-v.^3);
v_set = interp1(x,v,x_set,'nearest');
Now I want to change it in such a way, that it can work for lambda being an array, say
lambda = [5 3 7 10];
and x_set being an array of the same length. Of course this doesn't work because x must be 1-dimensional.
So basically, my code works for one modelled machine, represented by one lambda, but I want it to work for a multitude of machines, each with their own lambda value, picking a value v_set for each of them. All without loops of course, because this process is iterated many times.
Maybe I could calculate x not only for every possible value of v, but also every possible value of lambda, say
lambda = linspace(0,10,100);
and then use interp2, putting x and lambda in the x-y plane, and then search for the corresponding value v?
v_set = interp2(x,lambda,v,x_set,lambda_set);
But also there, x is 2-dimensional... Any help would be greatly appreciated!

1 commentaire

Torsten
Torsten le 17 Fév 2022
There is no other way than to loop over the length of the lambda-vector.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 17 Fév 2022
v = linspace(0,20,200);
lambda = [5 3 7 10] .'; %transpose!
x_set = 0.4;
x = v+2*v.^2+3*(lambda-v.^3);
Now x would be length(lambda) x length(v) and could be used with interp2 .
But look at your code. Your interp1() code is using x (the calculated variable!) as the independent variable, and using v (the fixed variable) as the dependent variable. If you are expecting to be able to pass in an x and have it figure out which (fixed!) v it corresponds to, then you are going to have problems. Your x is clearly non-linear in v, and that means that x will not be monotonic increasing or decreasing, and so cannot be used as the control variables for interp1() or interp2() purposes.

1 commentaire

Florian Hölscher
Florian Hölscher le 17 Fév 2022
Ah right, that was supposed to be transposed of course. But how do I use this with interp2 then?
About dependant and independent variables: I'm aware of that problem, but due to the chosen range, it's semi linear. So one value of x will always correlate to one v(x). The given equation is an example of course, but the same holds true for the original equation.

Connectez-vous pour commenter.

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by