How to interpolate column and row?

How can I get the value below the 'Year' row, if my G-value is 2.5 and the year is 4.17 and 4.58?
I mean, I have to interpolate the G-values between 2.4 and 2.6, also I have to interpolate the Year between 2 and 5.
Thanks before.

 Accepted Answer

Stephen23
Stephen23 on 20 Mar 2020
Edited: Stephen23 on 23 Mar 2020
Use interp2, e.g.:
>> Y = [1.0101,1.25,2,5,10,25,50,100];
>> G = [3,2.8,2.6,2.4,2.2,2,1.8,1.6,1.4,1.2,1,0.8,0.6,0.4,0.2,0,-0.2,-0.4,-0.6,-0.8,-1,-1.2,-1.4,-1.6,-1.8,-2,-2.2,-2.4,-2.6,-2.8,-3];
>> M = [-0.667,-0.636,-0.396,0.42,1.18,2.278,3.152,4.051;-0.714,-0.666,-0.384,0.46,1.21,2.275,3.114,3.973;-0.769,-0.696,-0.368,0.499,1.238,2.267,3.071,2.889;-0.832,-0.725,-0.351,0.537,1.262,2.256,3.023,3.8;-0.905,-0.752,-0.33,0.574,1.284,2.24,2.97,3.705;-0.99,-0.777,-0.307,0.609,1.302,2.219,2.192,3.605;-1.087,-0.799,-0.282,0.643,1.318,2.193,2.848,3.499;-1.197,-0.817,-0.254,0.675,1.329,2.163,2.78,3.388;-1.318,-0.832,-0.225,0.705,1.337,2.128,2.706,3.271;-1.449,-0.844,-0.195,0.732,1.34,2.087,2.626,3.149;-1.588,-0.852,-0.164,0.758,1.34,2.043,2.542,3.022;-1.733,-0.856,-0.132,0.78,1.336,1.993,2.453,2.891;-1.88,-0.857,-0.099,0.8,1.328,1.939,2.359,2.755;-2.029,-0.855,-0.066,0.816,1.317,1.88,2.261,2.615;-2.178,-0.85,-0.033,0.83,1.301,1.818,2.159,2.472;-2.326,-0.842,0,0.842,1.282,1.751,2.051,2.326;-2.472,-0.83,0.033,0.85,1.258,1.68,1.945,2.178;-2.271,-0.816,0.066,0.855,1.231,1.606,1.834,2.029;-2.388,-0.8,0.099,0.857,1.2,1.528,1.72,1.88;-3.499,-0.78,0.132,0.856,1.166,1.448,1.606,1.733;-3.605,-0.758,0.164,0.852,1.128,1.366,1.492,1.588;-3.705,-0.732,0.195,0.844,1.086,1.282,1.379,1.449;-3.8,-0.705,0.225,0.832,1.041,1.198,1.27,1.318;-3.889,-0.675,0.254,0.817,0.994,1.116,1.166,1.197;-3.449,-0.643,0.282,0.799,0.945,1.035,1.069,1.087;-3.605,-0.609,0.307,0.777,0.895,0.959,0.98,0.99;-3.705,-0.574,0.33,0.752,0.844,0.888,0.9,0.905;-3.8,-0.537,0.351,0.725,0.795,0.823,0.83,0.832;-3.889,-0.49,0.368,0.696,0.747,0.764,0.768,0.769;-3.973,-0.469,0.384,0.666,0.702,0.712,0.714,0.714;-7.051,-0.42,0.396,0.636,0.66,0.666,0.666,0.667];
>> Yout = [4.17,4.58];
>> Gout = 2.5;
>> Mout = interp2(Y,G,M,Yout,Gout)
Mout =
0.27522 0.39515
And checking:
>> surf(Y,G,M)
>> hold on
>> plot3(Yout,[Gout,Gout],0.1+Mout,'*r')
>> view(-123,45)

8 Comments

Thankyou. It works!
Aditya Gandhi
Aditya Gandhi on 30 Jun 2021
Edited: Aditya Gandhi on 30 Jun 2021
I am getting NaN as the output using this code. Please can you advise me on what might be wrong.
I need to use
Yout=[4.17,4.58]
Mout=[-0.667,-0.334]
Gout=interp2(Y,G,M,Yout,Mout)
Since my application is different would it need a different code ?
Stephen23
Stephen23 on 30 Jun 2021
Edited: Stephen23 on 30 Jun 2021
"Please can you advise me on what might be wrong."
You have swapped Gout and Mout.
"Since my application is different would it need a different code ?"
As you did not describe your "application" I can only hazard a guess that you want to get the value of an independent variable for some function value/s. In the given example there are potentially zero, one, or infinite independent variable values for any specific function value: which of those infinite values would you like?
To get you started, look at the output of this https://www.mathworks.com/help/matlab/ref/contour.html
Aditya Gandhi
Aditya Gandhi on 30 Jun 2021
Edited: Aditya Gandhi on 30 Jun 2021
For my application I need to swap Gout and Mout. However when I do that i get NaN as the result. Should I make any changes to the code in this case ?
i am trying to get result for following code
rpm1=[1000,2000,2500,3000,3500,4000,4500,4750,5320,5800,6000]; %engine rpm
theta1=[100;80;60;40;20;1]; %throttle
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
rpm1out=[1000;1400;1700;1900;3300;3500;3700;4100;4700;4900;5200;5400];
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];
theta1out=interp2(rpm1,theta1,tr1,rpm1out,tr1out);
Stephen23
Stephen23 on 30 Jun 2021
Edited: Stephen23 on 30 Jun 2021
"Should I make any changes to the code in this case ?"
Your data are not monotonic increasing/decreasing with the independent variables, we can trivially see this for both throttle values 80 and 100 the curve first increases and then decreases with RPM.
Basic interpolation is not suitable, you will need to use some other approach (as I wrote in my last comment).
Thank You. The values i am trying to find are very critical for my research. Please can you suggest me a suitable approach / refer to a similar example.
@Aditya Gandhi: ask a new question in a new thread, including your example data.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!