how to interpolate for increasing - decreasing type of data set

i am trying to get result for following code however I get NaN as result. Please help me correct my code
rpm1=[1000,2000,2500,3000,3500,4000,4500,4750,5320,5800,6000]; %engine rpm from table
theta1=[100;80;60;40;20;1]; %throttle from table
tr1=[117.200000000000,123.600000000000,129.100000000000,132.100000000000,134.300000000000,137.200000000000,134.800000000000,135.200000000000,129,119.300000000000,113.100000000000;115.300000000000,121.900000000000,127.300000000000,131.100000000000,120.500000000000,125.500000000000,124.500000000000,125.100000000000,118.800000000000,109.600000000000,104.700000000000;106,103.600000000000,96.7000000000000,89.6000000000000,86.4000000000000,80.9000000000000,74,69.8000000000000,55.7000000000000,45.8000000000000,41.2000000000000;98,85.9000000000000,77.6000000000000,69.3000000000000,63.8000000000000,56.9000000000000,50.5000000000000,46,32.2000000000000,21.3000000000000,17.4000000000000;65.4000000000000,45.7000000000000,37.2000000000000,27.8000000000000,23.5000000000000,19.1000000000000,15.6000000000000,11.9000000000000,0,0,0;0,0,0,0,0,0,0,0,0,0,0]; %torque from table
rpm1out=[1000;1400;1700;1900;3300;3500;3700;4100;4700;4900;5200;5400]; %sample rpm
tr1out=[95.4929658551372;102.313891987647;101.110199140734;100.518911426460;115.749049521378;122.776670385176;129.044548452888;128.100320049574;121.905913857622;126.674342460896;128.548223266531;127.323954473516]; %sample torque
theta1out=interp2(rpm1,theta1,tr1,rpm1out,tr1out); %desired throttle values for sample rpm and sample torque values

3 Comments

@Aditya Gandhi: in general interpolation will not work, e.g. as shown in all the completely nonsensical answers below. Your independent variables are RPM and throttle, the function of those two variables is torque. Importantly, the torque function is non-monotonic (it increases and then decreases with RPM).
Now you have torque and RPM values, and you want to identify the corresponding throttle angle: this means you have a function value and one independent variable, and you are trying to obtain the value of the other independent variable.
In general there is no simple way to solve this problem. Note that there can be zero, one, a finite number, or infinite values of that independent variable that produce that function value. This I already explained to you:
In the same comment I also gave you an approach you could use to start investigating this. This you have ignored.
You keep trying to interpolate different permutations of the data, hoping that one of them will magically interpolate. It won't.
A much better use of your time would be to
  • understand the problem (which would also show you why interpolation does not work)
  • investigate appropriate solutions (e.g. contour algorithms, joining closest points, etc)
Note that in some limited circumstances (e.g. you take a subset of the function which is monotonic) you might be able to use interpolation, but this would a) not be a general solution, b) take some effort to isolate the required domain (if at all possible).
Thank You. Sir I did not understand the approach you suggested earlier. Please can you refer me to a similar exaple if possible. I am short on time and hence trying everything i can find
"I did not understand the approach you suggested earlier."
I did not suggest a complete approach; I recommended where you could start looking.
"Please can you refer me to a similar exaple if possible."

Sign in to comment.

Answers (3)

Since tr1out is outside the range of theta1, you are doing extropolation as well. For the default interpolation method, the extrapolated values are NaN. To generate some extrapolated data, specify a different interpolation method such as spline. Be careful to use the extropolated data!!
rpm1=[1000,2000,2500,3000,3500,4000,4500,4750,5320,5800,6000]; %engine rpm from table
theta1=[100;80;60;40;20;1]; %throttle from table
tr1=[117.200000000000,123.600000000000,129.100000000000,132.100000000000,134.300000000000,137.200000000000,134.800000000000,135.200000000000,129,119.300000000000,113.100000000000;115.300000000000,121.900000000000,127.300000000000,131.100000000000,120.500000000000,125.500000000000,124.500000000000,125.100000000000,118.800000000000,109.600000000000,104.700000000000;106,103.600000000000,96.7000000000000,89.6000000000000,86.4000000000000,80.9000000000000,74,69.8000000000000,55.7000000000000,45.8000000000000,41.2000000000000;98,85.9000000000000,77.6000000000000,69.3000000000000,63.8000000000000,56.9000000000000,50.5000000000000,46,32.2000000000000,21.3000000000000,17.4000000000000;65.4000000000000,45.7000000000000,37.2000000000000,27.8000000000000,23.5000000000000,19.1000000000000,15.6000000000000,11.9000000000000,0,0,0;0,0,0,0,0,0,0,0,0,0,0]; %torque from table
rpm1out=[1000;1400;1700;1900;3300;3500;3700;4100;4700;4900;5200;5400]; %sample rpm
tr1out=[95.4929658551372;102.313891987647;101.110199140734;100.518911426460;115.749049521378;122.776670385176;129.044548452888;128.100320049574;121.905913857622;126.674342460896;128.548223266531;127.323954473516]; %sample torque
theta1out=interp2(rpm1,theta1,tr1,rpm1out,tr1out, 'spline'); %desire
The function interp2 performs interpolation for 2D gridded data. It has the form Vq = interp2(X,Y,V,Xq,Yq), where X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points. Now, in your example, the 2D function tr1 is defined over rpm1 and theta1, but those two vectors have different sizes! You have stored 11 values in rpm1 and only 6 values in theta1. For interp2 to function properly, you should define the missing 5 theta1 values.
rpm1=[1000,2000,2500,3000,3500,4000,4500,4750,5320,5800,6000]; %engine rpm from table
theta1=[100;80;60;40;20;1]; %throttle from table
tr1=[117.200000000000,123.600000000000,129.100000000000,132.100000000000,134.300000000000,137.200000000000,134.800000000000,135.200000000000,129,119.300000000000,113.100000000000;115.300000000000,121.900000000000,127.300000000000,131.100000000000,120.500000000000,125.500000000000,124.500000000000,125.100000000000,118.800000000000,109.600000000000,104.700000000000;106,103.600000000000,96.7000000000000,89.6000000000000,86.4000000000000,80.9000000000000,74,69.8000000000000,55.7000000000000,45.8000000000000,41.2000000000000;98,85.9000000000000,77.6000000000000,69.3000000000000,63.8000000000000,56.9000000000000,50.5000000000000,46,32.2000000000000,21.3000000000000,17.4000000000000;65.4000000000000,45.7000000000000,37.2000000000000,27.8000000000000,23.5000000000000,19.1000000000000,15.6000000000000,11.9000000000000,0,0,0;0,0,0,0,0,0,0,0,0,0,0]; %torque from table
rpm1out=[1000;1400;1700;1900;3300;3500;3700;4100;4700;4900;5200;5400]; %sample rpm
tr1out=[95.4929658551372;102.313891987647;101.110199140734;100.518911426460;115.749049521378;122.776670385176;129.044548452888;128.100320049574;121.905913857622;126.674342460896;128.548223266531;127.323954473516]; %sample torque
[X,Y] = meshgrid(rpm1,theta1) ;
plot(X,Y,'.r')
hold on
plot(rpm1out,tr1out,'*k')
theta1out=interp2(X,Y,tr1,rpm1out,tr1out); %desired throttle values for sample rpm and sample torque values
If you see, the values which you are trying to seek using interp2 are falling outside the domain. So this comes under extrapolation and results cannot be trusted.

3 Comments

I think I did not ask my question properly.
I have the values of torque(tr1) at different rpm(rpm1) and throttle(theta1) positions as given in the table.
Now i have to find the throttle(theta1out) values at the given sample torque(tr1out) and sample rpm(rpm1out) values.
Please can you check once again as I might have misrepresented in the code I asked in the question
@Aditya Gandhi: you accepted this answer. Does that mean that your problem is resolved?
No Sir. My problem is not resolved. I am still trying to figure out what how this can be done.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 1 Jul 2021

Edited:

on 1 Jul 2021

Community Treasure Hunt

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

Start Hunting!