MATLAB: Using interp2 – The grid must be created from grid vectors which are strictly monotonically increasing.

interpolation

I am using MATLAB_R2016b I have data of this format
Temperature = [310:10:800];
Pressure = [0.1 0 1.0 10.0 100.0 1000.0];
Cv =
[ 73.6400 25.3290 73.5920 73.1260 69.4500 61.8600
72.8060 25.3810 72.7640 72.3450 68.9780 61.7040
71.9230 25.4380 71.8850 71.5070 68.4230 61.3140
71.0060 25.4990 70.9710 70.6290 67.8040 60.8160
70.0680 25.5640 70.0360 69.7270 67.1400 60.2840
69.1220 25.6340 69.0940 68.8140 66.4460 59.7550
68.1800 25.7070 68.1540 67.9000 65.7350 59.2500
27.6640 25.7840 67.2240 66.9940 65.0150 58.7780
27.3630 25.8640 66.3120 66.1040 64.2950 58.3390
27.1700 25.9480 65.4220 65.2330 63.5820 57.9340
27.0440 26.0340 64.5570 64.3850 62.8790 57.5600
26.9660 26.1230 63.7210 63.5640 62.1900 57.2130
26.9240 26.2150 62.9130 62.7700 61.5170 56.8890
26.9110 26.3090 62.1360 62.0050 60.8620 56.5870
26.9200 26.4050 61.3890 61.2690 60.2250 56.3020
26.9460 26.5030 33.1250 60.5620 59.6080 56.0320
26.9870 26.6030 31.8460 59.8850 59.0090 55.7750
27.0390 26.7050 31.0570 59.2360 58.4290 55.5290
27.1010 26.8080 30.5000 58.6170 57.8680 55.2920
27.1700 26.9120 30.0840 58.0280 57.3240 55.0630
27.2460 27.0170 29.7670 57.4700 56.7980 54.8410
27.3280 27.1240 29.5260 56.9450 56.2900 54.6250
27.4140 27.2320 29.3430 56.4560 55.7970 54.4150
27.5040 27.3410 29.2080 56.0070 55.3210 54.2090
27.5980 27.4500 29.1110 55.6040 54.8600 54.0080
27.6940 27.5610 29.0460 55.2610 54.4150 53.8100
27.7930 27.6720 29.0060 54.9970 53.9840 53.6160
27.8950 27.7840 28.9870 54.8470 53.5670 53.4260
27.9980 27.8970 28.9870 51.7540 53.1650 53.2390
28.1030 28.0110 29.0020 47.2710 52.7760 53.0550
28.2100 28.1250 29.0290 44.3160 52.4010 52.8750
28.3180 28.2400 29.0670 42.1390 52.0390 52.6980
28.4270 28.3550 29.1150 40.4520 51.6910 52.5230
28.5380 28.4710 29.1710 39.1070 51.3570 52.3520
28.6500 28.5880 29.2340 38.0170 51.0350 52.1840
28.7630 28.7060 29.3040 37.1240 50.7260 52.0200
28.8770 28.8240 29.3780 36.3870 50.4300 51.8580
28.9920 28.9420 29.4580 35.7750 50.1460 51.7000
29.1080 29.0610 29.5420 35.2640 49.8730 51.5440
29.2250 29.1810 29.6290 34.8380 49.6100 51.3930
29.3420 29.3010 29.7200 34.4810 49.3570 51.2440
29.4610 29.4220 29.8150 34.1820 49.1120 51.0990
29.5800 29.5440 29.9120 33.9330 48.8720 50.9570
29.6990 29.6660 30.0110 33.7250 48.6360 50.8190
29.8200 29.7880 30.1130 33.5540 48.4000 50.6830
29.9410 29.9110 30.2170 33.4130 48.1630 50.5520
30.0630 30.0340 30.3230 33.3000 47.9210 50.4230
30.1850 30.1580 30.4310 33.2100 47.6720 50.2990
30.3080 30.2820 30.5400 33.1400 47.4140 50.1770
30.4310 30.4070 30.6510 33.0890 47.1430 50.0590];
When I try to query a new temperature, pressure, for example 0.2, 341 by doing this
interp2(Temperature, Pressure, Cv, 0.2, 341)
I get the following error:
Error using griddedInterpolant
The grid vectors must be strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 229)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 129)
F = makegriddedinterp({X, Y}, V, method,extrap);
What am I doing wrong? And how can I get the desired result which I want?

Best Answer

The non-increasing sequence of the pressures is the first problem, and obviously the reason the code failed.
There is also the issue of using linear interpolation with a data sequence that is clearly exponential.
For example, suppose the Pressure axis is actually
Pressure = [0.01 0.1 1 10 100 1000];
Linear interpolation on such an axis will perform poorly. Instead, you are far better off by taking the log10 of Pressure.
So now we have
Pressurelog = log10(Pressure)
It will look like [-2 -1 0 1 2 3].
Linear interpolation will now be more appropriate. This is almost always what you want to do when a variable varies by many orders of magnitude.