SQL - Fit data points in X, Y, Z box - tsql

I have a table in SSMS which currently has 3 columns. [x], [y], and [z] (see Table Sample below)
I will be using this data for a 3d model so I'm trying to convert the data points to fit into a 1000x1000x1000 box.
Ideally, my goal is to create a view or new table or at least a select statement that just takes the maximum x, y, and z values, sets those to 1024 and then somehow transforms the remaining data points so that they're still the same relative distance from the surrounding points. (if that makes any sense?) As you can see from the sample, the X's and Y's are well out of the desired range.
Table Sample
X Y Z
191106 438253.98 -1694.4952
189577 436750.98 -1716.5457
191723 439258.98 -1611.1542
192336 435909.98 -1642.1016
194749 437363.98 -1716.1238
194955 439394.98 -1568.7328
196368 438367.98 -1534.8014
196337 437207.98 -1545.7195
197387 437815.98 -1635.2943
196613 439527.98 -1547.7865
197067 438447.98 -1489.4183
196917 436722.98 -1506.2191
198433 437852.98 -1485.5884
200319 438479.98 -1430.4632
199402 436639.98 -1533.8686
199713 436178.98 -1513.1188
200880 440296.98 -1391.5241
200682 439695.98 -1371.639
203323 439027.98 -1421.2597
201380 438782.98 -1445.6019
203340 436885.98 -1448.6549
207446 440284.98 -1491.6267
205145 439002.98 -1479.705
206157 438509.98 -1536.9528
207978 436308.98 -1305.442
209665 437926.98 -1455.1272
207666 436249.98 -1416.6149
208014 436921.98 -1343.94
212075 439817.98 -1374.8002
208679 440630.98 -1323.8376
211192 436402.98 -1279.3944
210452 439760.98 -1386.7478
210866 440111.98 -1446.0699
213576 440283.98 -1438.2461
215415 437210.98 -1343.1045
213434 437566.98 -1350.9467
215575 437170.98 -1323.745
217012 437118.98 -1468.6063
218936 436636.98 -1322.4785
217929 436261.98 -1421.3502
219655 436880.98 -1342.3694
220476 436713.98 -1383.1211
219687 436294.98 -1320.2535
218359 439845.98 -1370.6624
222389 435813.98 -1449.1301
223197 436862.98 -1499.2541
220449 437855.98 -1480.1147
224052 436682.98 -1457.941
225385 439215.98 -1445.2534
224875 439058.98 -1453.1954
224611 438088.98 -1428.7523
224523 438640.98 -1374.0964
225388 439474.98 -1403.2189
228256 439065.98 -1363.5486
229816 437234.98 -1424.3758
230441 438093.98 -1559.9587
228173 436542.98 -1549.3953
229327 438042.98 -1381.3427
230226 436970.98 -1407.8287
233820 437086.98 -1382.2457
232178 436166.98 -1385.9663
234608 439996.98 -1468.6711
232418 438061.98 -1447.3259
235974 436656.98 -1381.7027
237590 436235.98 -1399.9012
235966 438663.98 -1370.2727
234652 436002.98 -1420.8281
235570 439423.98 -1390.8704
235931 438110.98 -1542.1993
238525 440095.98 -1482.6536
241616 435684.98 -1446.2368
240304 436988.98 -1472.9517
239200 436845.98 -1442.8028
239792 436927.98 -1407.7895
243214 440116.98 -1480.0391
241129 436652.98 -1517.4193
244601 438268.98 -1398.0311
246074 437269.98 -1454.5629
243281 440017.98 -1521.1655
245258 435815.98 -1403.9929
247566 438524.98 -1546.8568
246994 440542.98 -1438.6206
246275 440195.98 -1596.2303
249734 440143.98 -1483.694
248584 438937.98 -1423.644
247599 439867.98 -1576.9712
248454 437187.98 -1461.8549
250115 437224.98 -1471.2508
251304 440481.98 -1530.8975
251456 438928.98 -1455.6035
255368 440108.98 -1498.3991
254561 438203.98 -1551.4143
257281 439499.98 -1528.5569
254162 437862.98 -1527.261
255902 438131.98 -1466.7475
258635 439942.98 -1558.8969
259902 436111.98 -1517.8925
256287 437393.98 -1670.2096
259228 436783.98 -1489.4766
259618 437333.98 -1604.9076
260749 438923.98 -1565.3387
263245 438080.98 -1501.8154
261966 440096.98 -1614.651
263158 435987.98 -1605.458
262845 439896.98 -1582.5075
265248 440491.98 -1648.508
266856 440411.98 -1706.7038
267613 437850.98 -1694.7547
267655 436194.98 -1660.7436
265172 437557.98 -1610.8256
268341 437296.98 -1610.4581
270493 437391.98 -1695.3816
271324 439506.98 -1593.1007
269317 436616.98 -1655.3611
268178 440099.98 -1561.5692
272987 438820.98 -1494.7628
271824 439455.98 -1534.2566
270492 436870.98 -1632.1811
275297 438523.98 -1613.9092
275924 439874.98 -1599.9024
273780 439553.98 -1663.6221
277244 440409.98 -1575.6931
276636 438806.98 -1636.9198
276817 439217.98 -1514.7822
278485 436804.98 -1624.9701
278989 440529.98 -1536.7674
279414 437012.98 -1572.2877
278105 436420.98 -1607.4551
279341 437534.98 -1605.5338
281636 437969.98 -1514.9627
282599 440158.98 -1657.814
280493 439286.98 -1534.0811
283804 436881.98 -1738.765
189594 435519.98 -1658.5641
191397 435010.98 -1700.8264
191654 435011.98 -1589.1115
193361 439236.98 -1496.7807
194678 436421.98 -1548.643
194814 435210.98 -1562.5543
195911 439563.98 -1457.9624
197437 437528.98 -1612.1455
194251 439615.98 -1584.9062
196280 437754.98 -1626.5308
196240 435858.98 -1438.134
199846 439333.98 -1433.1658
197226 437268.98 -1626.6341
198871 437307.98 -1458.615
200988 435776.98 -1493.2658
198499 435915.98 -1406.514
199674 438567.98 -1425.0381
204528 436364.98 -1391.2642
204078 438951.98 -1401.7545
204659 436058.98 -1371.4057
206808 435097.98 -1478.2608
204738 437733.98 -1477.4681
207029 438227.98 -1356.5324
205945 438909.98 -1341.7529
206490 436152.98 -1363.2217
208602 436348.98 -1430.9503
209461 435603.98 -1382.5246
209519 436010.98 -1372.4789
208376 439795.98 -1414.4512
212107 437604.98 -1353.8593
210440 435475.98 -1407.1026
209711 435302.98 -1466.2034

From what I understood, you need to find new coordinates for your points. So, you must find ratio between 1024 and difference between max and min value (D). Update your data so that highest value will be (D) and then multiply by ratio.
with cte as (
select
min(val) minVal, 1024/(max(val)-min(val)) k
from (
select x val from myTable
union all select y from myTable
union all select z from myTable
) t
)
select
x, y, z, (x-minVal)*k newX, (y-minVal)*k newY, (z-minVal)*k newZ
from
myTable
cross join cte

Related

Lsode throwing INTDY-- T (=R1) ILLEGAL and invalid input detected

I have my function:
function [result] = my_func(x,y)
result = y^2*(1-3*x)-3*y;
endfunction
Also, my vector with Ts, my function address and my initial variable x_0
load file_with_ts
# Add my limits as I also want to calculate those
# (all values in file_with_ts are within those limits.)
t_points = [-1, file_with_ts, 2]
myfunc = str2func("my_func")
x_0 = 0.9142
I am trying to execute the following line:
lsode_d1 = lsode(myfunc, x_0, t_points)
And expecting a result, but getting the following error:
INTDY-- T (=R1) ILLEGAL
In above message, R1 = 0.7987082301475D+00
T NOT IN INTERVAL TCUR - HU (= R1) TO TCUR (=R2)
In above, R1 = 0.8091168896311D+00 R2 = 0.8280400838323D+00
LSODE-- TROUBLE FROM INTDY. ITASK = I1, TOUT = R1
In above message, I1 = 1
In above message, R1 = 0.7987082301475D+00
error: lsode: invalid input detected (see printed message)
error: called from
main at line 20 column 10
Also, the variable sizes are:
x_0 -> 1x1
t_points -> 1x153
myfunc -> 1x1
I tried transposing the t_points vector
using #my_func instead of the str2func function
I tried adding multiple variables as the starting point (instead of x_0 I entered [x_0; x_1])
Tried changing my function header from my_func(x, y) to my_func(y, x)
Read the documentation and confirmed that my_func allows x to be a vector and returns a vector (whenever x is a vector).
EDIT: T points is the following 1x153 matrix (with -1 and 2 added to the beggining and the end respectively):
-4.9451e-01
-4.9139e-01
-4.7649e-01
-4.8026e-01
-4.6177e-01
-4.5412e-01
-4.4789e-01
-4.2746e-01
-4.1859e-01
-4.0983e-01
-4.0667e-01
-3.8436e-01
-3.7825e-01
-3.7150e-01
-3.5989e-01
-3.5131e-01
-3.4875e-01
-3.3143e-01
-3.2416e-01
-3.1490e-01
-3.0578e-01
-2.9267e-01
-2.9001e-01
-2.6518e-01
-2.5740e-01
-2.5010e-01
-2.4017e-01
-2.3399e-01
-2.1491e-01
-2.1067e-01
-2.0357e-01
-1.8324e-01
-1.8112e-01
-1.7295e-01
-1.6147e-01
-1.5424e-01
-1.4560e-01
-1.1737e-01
-1.1172e-01
-1.0846e-01
-1.0629e-01
-9.4327e-02
-8.0883e-02
-6.6043e-02
-6.6660e-02
-6.1649e-02
-4.7245e-02
-2.8332e-02
-1.8043e-02
-7.7416e-03
-6.5142e-04
1.0918e-02
1.7619e-02
3.4310e-02
3.3192e-02
5.2275e-02
5.5756e-02
6.8326e-02
8.2764e-02
9.5195e-02
9.4412e-02
1.1630e-01
1.2330e-01
1.2966e-01
1.3902e-01
1.4891e-01
1.5848e-01
1.7012e-01
1.8026e-01
1.9413e-01
2.0763e-01
2.1233e-01
2.1895e-01
2.3313e-01
2.4092e-01
2.4485e-01
2.6475e-01
2.7154e-01
2.8068e-01
2.9258e-01
3.0131e-01
3.0529e-01
3.1919e-01
3.2927e-01
3.3734e-01
3.5841e-01
3.5562e-01
3.6758e-01
3.7644e-01
3.8413e-01
3.9904e-01
4.0863e-01
4.2765e-01
4.2875e-01
4.3468e-01
4.5802e-01
4.6617e-01
4.6885e-01
4.7247e-01
4.8778e-01
4.9922e-01
5.1138e-01
5.1869e-01
5.3222e-01
5.4196e-01
5.4375e-01
5.5526e-01
5.6629e-01
5.7746e-01
5.8840e-01
6.0006e-01
5.9485e-01
6.1771e-01
6.3621e-01
6.3467e-01
6.5467e-01
6.6175e-01
6.6985e-01
6.8091e-01
6.8217e-01
6.9958e-01
7.1802e-01
7.2049e-01
7.3021e-01
7.3633e-01
7.4985e-01
7.6116e-01
7.7213e-01
7.7814e-01
7.8882e-01
8.1012e-01
7.9871e-01
8.3115e-01
8.3169e-01
8.4500e-01
8.4168e-01
8.5705e-01
8.6861e-01
8.8211e-01
8.8165e-01
9.0236e-01
9.0394e-01
9.2033e-01
9.3326e-01
9.4164e-01
9.5541e-01
9.6503e-01
9.6675e-01
9.8129e-01
9.8528e-01
9.9339e-01
Credits to Lutz Lehmann and PierU.
The problem lied in the array t_points not being a monotonous array. Adding a sort(t_points) before doing any calculations fixed the error.

Using interp2 - The grid must be created from grid vectors which are strictly monotonically increasing

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] pair, 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?
You need to have the same number of points in Temperature and Pressure as you do in Cv. You can generate these points using meshgrid.
[Temp, Pres] = meshgrid(Temperature, Pressure)
% Temp and Pres are both 6x50 matrices
However, you still have an issue. Temperature and Pressure must be monotonically increasing, as the error message states. This means you can't have a pressure value go down, which it does. You must change the second value in the Pressure array, or for instance you may want to swap columns 1 and 2 of the Pressure and Cv arrays
Pressure = [0.1, 0, 1, 10, 100, 1000]; % original
Pressure = Pressure([2, 1, 3:end]); % swap columns 1 and 2, Pressure = [0,0.1,1,10,...]
Cv = [...]; % All of your data
Cv = Cv(:, [2, 1, 3:end]) % Swap columns 1 and 2
Now you can do your lookup. Note you also had the temperature and pressure values the wrong way around, they must be the same order for inputs 1/2 and inputs 4/5.
[Temp, Pres] = meshgrid(Temperature, Pressure);
out = interp2(Temp, Pres, Cv.', 341, 0.2) % not (..., 0.2, 341) as must be same order
>> out = 30.5468
The only consideration you might want to give is that you're using linear interpolation with interp2, but your pressure data is logarithmic. Check your results are sensible.

Quiver list and labeling vectors of my inputs u,d, and r

How do I label each vector of each input component, say u$_1$..., d$_1$..., on my graph at each vector's respective starting point. I want to see if there is a clustering of certain vectors like u$_30$-u$_50$ are all near each other, for example. Here is my code:
fx = [1.01, 1.0165376460248143, 1.016585505356985,...
1.0166786731186357, 1.0166445649045002, 1.01684528204491,...
1.0168363943981442, 1.0169505828006045, 1.0169903647693619,...
1.0170588800755562, 1.0170214556321182, 1.0171007103163394,...
1.0170611565299144, 1.0171737504115423, 1.0171325089936156,...
1.0173884633568437, 1.0173821295549914, 1.017540453473392,...
1.0176091468862674, 1.0177647297690604, 1.017711866139699,...
1.0177536635811828, 1.0178254876275734, 1.0173994306983212,...
1.0200331803664529, 1.0232411092365432, 1.0232773133875106,...
1.023383936276914, 1.0233275057530007, 1.023510835824228,...
1.0234461433923472, 1.023507118352957, 1.0237210297124908,...
1.0236390252916325, 1.0237007559499636, 1.0239084387662698,...
1.0238131746118633, 1.024266374303865, 1.024212732428539,...
1.02440393427416, 1.0245390401237269, 1.0252178000353167,...
1.0252021019242434, 1.0275875709904758, 1.0275871039342042];
fy = [0.99, 0.99, 0.9899194500620327, 0.9897134368447225,...
0.9899339650105077, 0.9895259027418399, 0.9898115223446341,...
0.9896762515189842, 0.9896129792784014, 0.9894766621994305,...
0.9896189382715079, 0.9894614440540032, 0.9896292673356496,...
0.9894095770062209, 0.989655005387203, 0.9892019096930893,...
0.9894189058876284, 0.9892732425545386, 0.9891916768216495,...
0.9889512723219249, 0.9892071461243063, 0.9891515372181835,...
0.9890346980816267, 0.9901802532401042, 0.9892771992437573,...
0.9881487558751526, 0.9880037699743045, 0.9875669935217211,...
0.9878502051001951, 0.9872010568874899, 0.9875329139453003,...
0.9873775054641964, 0.9868251990627905, 0.9871082986923524,...
0.9869819983991632, 0.9865548473263468, 0.9867867860622922,...
0.9859765136441385, 0.9861731333993694, 0.9859212446482857,...
0.9857475603282838, 0.9848759880952044, 0.9850648602644492,...
0.9821891156159342, 0.9822254068452594];
fz =[0.01, -0.0014683388934621459, -0.0028093690242636917,...
-0.006255424514110392, -0.002405171080788649, -0.009167776104980133,...
0.0003750210183572269, -0.001823375333180016, -0.002906415137850454,...
-0.005227263048381278, -0.0028662319950483552, -0.0055329993182467365,...
-0.0027458980004112996, -0.00644276568444028, -0.00226410433801184,...
-0.009832266892691467, 0.0012478354917326469, -0.001163969711179093,...
-0.0026270200357900887, -0.006946260715800828, -0.00188587841967576,...
-0.002880843788516535, -0.0049636661241180685, 0.015586949435911355,...
0.010368914010693711, -0.0010649331940053245, -0.002328942248654949,...
-0.006634620630021168, -0.0020052485893380344, -0.008543368794125199,...
-0.00044976575279103564, -0.0019790036016751333, -0.008330963679008077,...
-0.0006481277669472506, -0.0020539789179887767, -0.0075781311330381336,...
-0.001294365366809558, -0.011629381859506432, 0.003447063734076782,...
0.0011256038145771368, -0.0008637305140054806, -0.012865086502170518,...
0.005283762238371167, -0.016926299226379265, 0.011993515880473204];
x = [1.01, 1.0165376460248143, 1.016585505356985, 1.0166786731186357,...
1.0166445649045002, 1.01684528204491, 1.0168363943981442,...
1.0169505828006045, 1.0169903647693619, 1.0170588800755562,...
1.0170214556321182, 1.0171007103163394, 1.0170611565299144,...
1.0171737504115423, 1.0171325089936156, 1.0173884633568437,...
1.0173821295549914, 1.017540453473392, 1.0176091468862674,...
1.0177647297690604, 1.017711866139699, 1.0177536635811828,...
1.0178254876275734, 1.0173994306983212, 1.0200331803664529,...
1.0232411092365432, 1.0232773133875106, 1.023383936276914,...
1.0233275057530007, 1.023510835824228, 1.0234461433923472,...
1.023507118352957, 1.0237210297124908, 1.0236390252916325,...
1.0237007559499636, 1.0239084387662698, 1.0238131746118633,...
1.024266374303865, 1.024212732428539, 1.02440393427416,...
1.0245390401237269, 1.0252178000353167, 1.0252021019242434,...
1.0275875709904758, 1.0275871039342042];
y = [0.99, 0.99, 0.9899194500620327, 0.9897134368447225,...
0.9899339650105077, 0.9895259027418399, 0.9898115223446341,...
0.9896762515189842, 0.9896129792784014, 0.9894766621994305,...
0.9896189382715079, 0.9894614440540032, 0.9896292673356496,...
0.9894095770062209, 0.989655005387203, 0.9892019096930893,...
0.9894189058876284, 0.9892732425545386, 0.9891916768216495,...
0.9889512723219249, 0.9892071461243063, 0.9891515372181835,...
0.9890346980816267, 0.9901802532401042, 0.9892771992437573,...
0.9881487558751526, 0.9880037699743045, 0.9875669935217211,...
0.9878502051001951, 0.9872010568874899, 0.9875329139453003,...
0.9873775054641964, 0.9868251990627905, 0.9871082986923524,...
0.9869819983991632, 0.9865548473263468, 0.9867867860622922,...
0.9859765136441385, 0.9861731333993694, 0.9859212446482857,...
0.9857475603282838, 0.9848759880952044, 0.9850648602644492,...
0.9821891156159342, 0.9822254068452594];
z =[0.01, -0.0014683388934621459, -0.0028093690242636917,...
-0.006255424514110392, -0.002405171080788649, -0.009167776104980133,...
0.0003750210183572269, -0.001823375333180016, -0.002906415137850454,...
-0.005227263048381278, -0.0028662319950483552, -0.0055329993182467365,...
-0.0027458980004112996, -0.00644276568444028, -0.00226410433801184,...
-0.009832266892691467, 0.0012478354917326469, -0.001163969711179093,...
-0.0026270200357900887, -0.006946260715800828, -0.00188587841967576,...
-0.002880843788516535, -0.0049636661241180685, 0.015586949435911355,...
0.010368914010693711, -0.0010649331940053245, -0.002328942248654949,...
-0.006634620630021168, -0.0020052485893380344, -0.008543368794125199,...
-0.00044976575279103564, -0.0019790036016751333, -0.008330963679008077,...
-0.0006481277669472506, -0.0020539789179887767, -0.0075781311330381336,...
-0.001294365366809558, -0.011629381859506432, 0.003447063734076782,...
0.0011256038145771368, -0.0008637305140054806, -0.012865086502170518,...
0.005283762238371167, -0.016926299226379265, 0.011993515880473204];
figure
q = quiver3(fx,fy,fz,x,y,z)
The problem is that I want to label the starting point of each vector:
You can use text:
q = quiver3(fx,fy,fz,x,y,z)
text(fx,fy,fz,num2str((1:numel(fx)).'))
The first 3 inputs are the coordinates of the label and the next input is a list (column character array or a cell-array) of the labels. I don't understand how your labeling is working (i.e. what is u$_1$ or d$_1$), so I just numbered the vectors from 1 to 45.

Plot a V-shape function given its points

I have the following matrix which rows are points sampled from a function
f = [ -3.7850 -11.5240
-3.7753 -11.4822
-3.7680 -11.5427
-3.7592 -11.5607
-3.7576 -11.5461
-3.7454 -11.5887
-3.7386 -11.4070
-3.7358 -11.4450
-3.7289 -11.5511
-3.7254 -11.3713
-3.7122 -11.4515
-3.6820 -11.5582
-3.6758 -11.5946
-3.6732 -11.5823
-3.6679 -11.6365
-3.6487 -11.3525
-3.6424 -11.2745
-3.6322 -11.3478
-3.6235 -11.6379
-3.6159 -11.6308
-3.5619 -11.1980
-3.5550 -11.2284
-3.5544 -11.5925
-3.5147 -11.6578
-3.5041 -11.6756
-3.4860 -11.1550
-3.4654 -11.6341
-3.4550 -11.1329
-3.3802 -11.6701
-3.3691 -11.1083
-3.3541 -11.0790
-3.3485 -11.5887
-3.3006 -11.6384
-3.2481 -11.5570
-3.2459 -11.0268
-3.2441 -10.9314
-3.2301 -11.5225
-3.2270 -10.8832
-3.1543 -10.8612
-3.1528 -11.5490
-3.1167 -11.5021
-3.1102 -10.8255
-3.0645 -11.5618
-2.9967 -11.5420
-2.9898 -10.8136
-2.9645 -10.7107
-2.9211 -11.4197
-2.9175 -10.6389
-2.8558 -10.6015
-2.8327 -11.5108
-2.7768 -11.4501
-2.7392 -10.5492
-2.7217 -11.4230
-2.6988 -10.4724
-2.6235 -11.3226
-2.6196 -11.3806
-2.5772 -10.4518
-2.5458 -10.4317
-2.5014 -10.3176
-2.4832 -11.3822
-2.4778 -10.2456
-2.4029 -11.2907
-2.3723 -10.3002
-2.3590 -11.2911
-2.3491 -10.2110
-2.2756 -11.2318
-2.2554 -10.1204
-2.2542 -10.1411
-2.2181 -11.2300
-2.1982 -9.9584
-2.1645 -9.7938
-2.1541 -11.1682
-2.1476 -9.8235
-2.1451 -9.9205
-2.1280 -10.0064
-2.1269 -9.8947
-2.0898 -9.7926
-2.0781 -11.1293
-1.9985 -11.0985
-1.9249 -11.0443
-1.8220 -11.0419
-1.7359 -11.0043
-1.6924 -10.9775
-1.6049 -10.9579
-1.5275 -10.9339
-1.4757 -10.9113
-1.4122 -10.8854
-1.3245 -10.8908
-1.2936 -10.7893
-1.2091 -10.8121
-1.1575 -10.8064
-1.1237 -10.7105
-1.0571 -10.7724
-1.0217 -10.7096
-0.9717 -10.6984
-0.9447 -10.7103
-0.9120 -10.6687
-0.8908 -10.6670]
Plotting by plot(f(:,1),f(:,2),'+') it is clear that the function has a V-shape. However, I need to plot it continuously, but doing plot(f(:,1),f(:,2)) results in a zig-zag function. How can I plot the points as I want to? (beside sorting them manually)
You could try rotating your data, sorting it and rotating it back. e.g:
theta = -1;
R = [cos(theta) -sin(theta);sin(theta) cos(theta)];
f2 = f*R;
f3 = sortrows(f2);
f4 = f3*R';
plot(f4(:,1),f4(:,2),'-',f(:,1),f(:,2),'+')
You can tweak theta to change the angle, which affects the sort order, I just took a guess that -1 is about right.

Length of a border of a POLYGON (or MULTIPOLYGON) in postgis

I have a PostGIS database with some POLYGONs (some are MULTIPOLYGONs). I want to find the length of the border of these polygons. They are shapes of administrative boundaries, and hence I'd like to get the length of the boundary.
For the MULTIPOLYGON shapes, I'd like it to be the sum of the boundary of each of the POLYGONs that make it up.
Here's an example of 2 of the shapes (the SRID is 900913):
POLYGON((-771958.31 6885015.34,-771923.15 6885310.54,-771809.28 6885392.78,-771679.07 6885406.05,-771424.03 6885497.97,-770935.04 6885439.73,-770713.55 6885538.08,-770482.81 6885745.21,-770233.34 6886191.66,-770181.32 6886711.86,-770370.21 6886944.29,-770397.74 6886956.42,-770303.56 6887085.22,-770234.96 6887174.93,-770110.06 6887301.59,-769891.93 6887400.11,-769761.75 6887417.69,-769724.8 6887495.1,-769529.55 6887931.36,-769425.75 6887880.34,-769228.74 6887818.78,-769114.39 6887829.32,-768933.2 6887896.17,-768896.84 6887976.98,-768741.36 6888049.11,-768711.51 6888129.98,-768629.41 6888316.56,-768516.09 6888456.13,-768438.68 6888533.53,-768334.47 6888647.22,-768256.1 6888760.41,-768166.54 6888689.5,-768103.1 6888692.01,-768056.2 6888520.15,-767980.56 6888469.13,-767846.87 6888393.49,-767635.77 6888212.29,-767565.41 6888094.43,-767465.14 6888068.05,-767255.8 6888046.94,-767097.49 6888046.94,-767000.73 6888031.11,-766896.94 6887851.68,-766782.6 6887663.44,-766604.93 6887519.21,-766413.18 6887371.43,-766139.4 6887370.19,-765988.89 6887303.02,-765897.72 6887162.23,-765924.86 6887144.75,-765977.74 6887058.21,-766136.4 6887012.53,-766190.39 6886977.26,-766385.86 6886894.81,-766536.29 6886825.12,-766726.67 6886753.74,-766903.44 6886749.84,-766984.18 6886698.85,-767036.04 6886737.09,-767177.11 6886761.74,-767264.65 6886771.08,-767350.14 6886738.68,-767523.22 6886759.11,-767572.5 6886717.04,-767653.03 6886739.86,-767691.49 6886685.78,-767772.01 6886682.18,-767778.03 6886589.63,-767910.62 6886578.9,-768035.55 6886518.56,-768184.29 6886435.26,-768198.74 6886385.97,-768241.23 6886229.58,-768346.61 6886081.7,-768361.91 6885969.53,-768306.67 6885814.84,-768274.37 6885730.7,-768153.68 6885684.8,-768134.14 6885608.32,-768165.62 6885539.63,-768244.94 6885492.76,-768331.48 6885360.55,-768430.01 6885214.38,-768437.96 6885113.83,-768636.67 6885175.57,-768758.43 6885374.97,-768775.54 6885487.75,-768887.58 6885663.36,-768890.62 6885697.22,-769007.53 6885852.57,-769032.42 6885847.82,-769046.75 6885814.89,-769079.05 6885777.21,-769172.84 6885705.87,-769222.94 6885673.18,-769269.78 6885663.38,-769332.85 6885636.46,-769456.56 6885615.08,-769674.02 6885529.96,-769708.16 6885476.49,-769707.47 6885411.77,-769739.7 6885358.83,-769792.79 6885311.27,-769833.53 6885288.08,-769858.28 6885233.95,-769883.51 6885138.09,-769923.29 6885074.87,-769897.32 6885017.75,-769915.3 6884984.68,-770018.88 6884984.27,-770144.01 6885043.71,-770211.53 6884848.97,-770266.09 6884545.38,-770295.69 6884559.34,-770577.74 6884634.02,-770774.56 6884742.9,-771051.06 6884901.37,-771286.57 6884987.78,-771421.81 6884985.53,-771480.47 6884957.27,-771619.97 6884859.66,-771748.55 6884800.97,-771887.67 6884896.97,-771958.31 6885015.34))
MULTIPOLYGON(((-748402.29 6933642.13,-748200.12 6933723.79,-747125.33 6934322.44,-747080.44 6934234.95,-747015.79 6934158.69,-746940.66 6934083.36,-746869.3 6934016.65,-746810.92 6933906.62,-746778.54 6933811.99,-746718.21 6933728.99,-746628.02 6933648.49,-746546.94 6933494.73,-746397.31 6933370.7,-747345.83 6933019.56,-747417.1 6933252.35,-747583.38 6933428.15,-747668.91 6933432.89,-747801.85 6933366.36,-747906.14 6933234.1,-748173.7 6933683.88,-748399.51 6933622.62,-748402.29 6933642.13)),((-748665.23 6934960.19,-747689.57 6935406.98,-747622.32 6935293.38,-747576.11 6935204.26,-747541.11 6935103.13,-747496.85 6935009.48,-747840.87 6934882.49,-748550.36 6934423.01,-748613.66 6934709.69,-748665.23 6934960.19)))
I'm sure there is a PostGIS function for it in this list ( http://postgis.refractions.net/docs/reference.html ), but I'm not sure which.
I would also like this length in metres. I have the same data in a GEOGRAPHY (as opposed to the older GEOMETRY column type). Is it possible to calculate the border length from that?
If the geometry column was called 'wkb_geometry' on a table called 'counties' for instance, you could try...
select st_length(st_boundary(st_transform(wkb_geometry,2163))) as border_length from counties;
That assumes you are using data in the US; SRID 2163 seems to be a good one for measuring distance in meters. You could choose a more appropriate one if you were dealing with a different area of the world.
Use ST_Perimeter on geography types to return the perimeter of the borders in meters:
WITH data AS
(SELECT 1 AS gid, 'SRID=900913;POLYGON((-771958.31 6885015.34,-771923.15 6885310.54,-771809.28 6885392.78,-771679.07 6885406.05,-771424.03 6885497.97,-770935.04 6885439.73,-770713.55 6885538.08,-770482.81 6885745.21,-770233.34 6886191.66,-770181.32 6886711.86,-770370.21 6886944.29,-770397.74 6886956.42,-770303.56 6887085.22,-770234.96 6887174.93,-770110.06 6887301.59,-769891.93 6887400.11,-769761.75 6887417.69,-769724.8 6887495.1,-769529.55 6887931.36,-769425.75 6887880.34,-769228.74 6887818.78,-769114.39 6887829.32,-768933.2 6887896.17,-768896.84 6887976.98,-768741.36 6888049.11,-768711.51 6888129.98,-768629.41 6888316.56,-768516.09 6888456.13,-768438.68 6888533.53,-768334.47 6888647.22,-768256.1 6888760.41,-768166.54 6888689.5,-768103.1 6888692.01,-768056.2 6888520.15,-767980.56 6888469.13,-767846.87 6888393.49,-767635.77 6888212.29,-767565.41 6888094.43,-767465.14 6888068.05,-767255.8 6888046.94,-767097.49 6888046.94,-767000.73 6888031.11,-766896.94 6887851.68,-766782.6 6887663.44,-766604.93 6887519.21,-766413.18 6887371.43,-766139.4 6887370.19,-765988.89 6887303.02,-765897.72 6887162.23,-765924.86 6887144.75,-765977.74 6887058.21,-766136.4 6887012.53,-766190.39 6886977.26,-766385.86 6886894.81,-766536.29 6886825.12,-766726.67 6886753.74,-766903.44 6886749.84,-766984.18 6886698.85,-767036.04 6886737.09,-767177.11 6886761.74,-767264.65 6886771.08,-767350.14 6886738.68,-767523.22 6886759.11,-767572.5 6886717.04,-767653.03 6886739.86,-767691.49 6886685.78,-767772.01 6886682.18,-767778.03 6886589.63,-767910.62 6886578.9,-768035.55 6886518.56,-768184.29 6886435.26,-768198.74 6886385.97,-768241.23 6886229.58,-768346.61 6886081.7,-768361.91 6885969.53,-768306.67 6885814.84,-768274.37 6885730.7,-768153.68 6885684.8,-768134.14 6885608.32,-768165.62 6885539.63,-768244.94 6885492.76,-768331.48 6885360.55,-768430.01 6885214.38,-768437.96 6885113.83,-768636.67 6885175.57,-768758.43 6885374.97,-768775.54 6885487.75,-768887.58 6885663.36,-768890.62 6885697.22,-769007.53 6885852.57,-769032.42 6885847.82,-769046.75 6885814.89,-769079.05 6885777.21,-769172.84 6885705.87,-769222.94 6885673.18,-769269.78 6885663.38,-769332.85 6885636.46,-769456.56 6885615.08,-769674.02 6885529.96,-769708.16 6885476.49,-769707.47 6885411.77,-769739.7 6885358.83,-769792.79 6885311.27,-769833.53 6885288.08,-769858.28 6885233.95,-769883.51 6885138.09,-769923.29 6885074.87,-769897.32 6885017.75,-769915.3 6884984.68,-770018.88 6884984.27,-770144.01 6885043.71,-770211.53 6884848.97,-770266.09 6884545.38,-770295.69 6884559.34,-770577.74 6884634.02,-770774.56 6884742.9,-771051.06 6884901.37,-771286.57 6884987.78,-771421.81 6884985.53,-771480.47 6884957.27,-771619.97 6884859.66,-771748.55 6884800.97,-771887.67 6884896.97,-771958.31 6885015.34))'::geometry AS geom
UNION SELECT 2 AS gid,
'SRID=900913;MULTIPOLYGON(((-748402.29 6933642.13,-748200.12 6933723.79,-747125.33 6934322.44,-747080.44 6934234.95,-747015.79 6934158.69,-746940.66 6934083.36,-746869.3 6934016.65,-746810.92 6933906.62,-746778.54 6933811.99,-746718.21 6933728.99,-746628.02 6933648.49,-746546.94 6933494.73,-746397.31 6933370.7,-747345.83 6933019.56,-747417.1 6933252.35,-747583.38 6933428.15,-747668.91 6933432.89,-747801.85 6933366.36,-747906.14 6933234.1,-748173.7 6933683.88,-748399.51 6933622.62,-748402.29 6933642.13)),((-748665.23 6934960.19,-747689.57 6935406.98,-747622.32 6935293.38,-747576.11 6935204.26,-747541.11 6935103.13,-747496.85 6935009.48,-747840.87 6934882.49,-748550.36 6934423.01,-748613.66 6934709.69,-748665.23 6934960.19)))'::geometry AS geom)
SELECT gid,
ST_Perimeter(ST_Transform(geom,4326)::geography) AS perimeter_geog
FROM data;
gid | perimeter_geog
-----+------------------
1 | 11439.2519522371
2 | 5225.96374459924
(2 rows)