Using interpolation with interp1/interp2

조회 수: 18 (최근 30일)
Florian Hölscher
Florian Hölscher 2022년 2월 17일
댓글: Florian Hölscher 2022년 2월 17일
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
Torsten
Torsten 2022년 2월 17일
There is no other way than to loop over the length of the lambda-vector.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 2월 17일
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
Florian Hölscher
Florian Hölscher 2022년 2월 17일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by