least-squares method with a constraint - matlab

I have 37 linear equations with 36 variables in the form of matrix: A x = b. (A has 37 rows and 36 columns.) The equations don't have an exact solution so I have used Matlab to find the closest answer using x = A \ b.
The problem is that I also have a condition, all elements of x should be positive: xi > 0 for all i. x = A \ b gives negative values for some elements. How can I apply this constraint ?
Here are the concrete values of A and b that I'm working with:
A = [0.83 0.17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0.02 0.63 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0.02 -0.37 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0.02 -0.37 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0.02 -0.37 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0.02 -0.2 0 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0.15 0 0 0 0 0 -0.32 0.17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0.15 0 0 0 0 0.02 -0.52 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0.15 0 0 0 0 0.02 -0.52 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0.15 0 0 0 0 0.02 -0.52 0.17 0 0 0 0.18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0.15 0 0 0 0 0.02 -0.52 0.17 0 0 0 0.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0 0 0 0 0 0 0.15 0 0 0 0 0.02 -0.17
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
b = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]';

You want to find an approximate solution x to A x = b in the least-squares sense, i.e. you want to minimize
||A x - b||2 = xT AT A x
+ bT b - 2 xT AT b.
Disregarding the constant term bTb and dividing by a factor 2, this fits the form of a quadratic programming problem, which is to minimize
1/2 xT H x + fT x,
if we choose H = AT A and f = - AT b.
The corresponding use of quadprog is:
H = A' * A;
f = - A' * b;
x = quadprog(H, f)
You also want the elements of x to be positive. A non-negativity constraint can be introduced using the additional parameters to quadprog, A and b (not to be confused with your matrices!):
n = size(A, 2);
x = quadprog(H, f, -eye(n), zeros(n, 1))
A positivity constraint does not make sense, because if the optimal solution involves one or more elements of x to be exactly 0, then a strictly positive solution will be the better the smaller the corresponding elements are: 0.01 will be better than 0.1, 0.001 will be better than 0.01, etc. etc. – there is no natural bound. If you want to make sure that the solution is all-positive, you have to set a finite bound yourself:
x = quadprog(H, f, -eye(n), zeros(n, 1) + 0.001)
Now the smallest possible value of an element of x is 0.001.
Update after the question was supplemented with the actual data of A and b: Using the code
H = A' * A;
f = - A' * b;
n = size(A, 2);
x = quadprog(H, f, -eye(n), zeros(n, 1))
I get the result:
Minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
x =
0.000380906335150292
3.90638261088393e-05
0.0111196970167585
0.0227055107206744
0.0318402514628274
0.0371743514880516
0.000800900221354844
0.00746652476710186
0.0180511534370576
0.0282423767946842
0.0362606972021829
0.0417582260990786
0.00860220929402253
0.0174105435824309
0.0265771677458008
0.0343071472371469
0.0395176470725881
0.0419494410289298
0.0187719294637544
0.0268976053211278
0.0336818044612046
0.0382365751296441
0.0398823076542831
0.0391016682549663
0.0279383031707377
0.0339393563379992
0.0377917413001034
0.0382731422972829
0.0338557405807941
0.0217568643500703
0.0343698083354502
0.0381554349806972
0.0392353941260779
0.0368010570888738
0.031271868401718
0.0258232230013864

This would be a linear optimization problem when you have x>0 condition. The best algorithm to solve that is simplex algorithm. The idea is that each linear equation aix=bi provide a line and the combination of these lines provide a polygon/polyhedron and the answer is one of the vertices of this polyhedron/polygon. Simplex algorithm is pretty standard and there are many available functions and libraries tha can calculate that.

The Matlab functions lsqlin or lsqnonneg can be used to solve your issue.
e.g:
x=lsqnonneg(A,b)
will give you what you are looking for.

Related

Expanding a matrix according to elements in lists

I have currently created a function which takes in two arguments.
function p = matr(x, phi)
x_dir = linspace(0, x, 1);
r = linspace(0, phi, 1);
p = zeros(800, 2);
p(1:400, 1) = p(1:400, 1) + x_dir.';
p(401:800, 2) = p(401:800, 2) + r.';
end
Which returns this matrix for the given input:
path = trajectory(10, pi/2)
path =
0 0
0.0251 0
0.0501 0
0.0752 0
0.1003 0
0.1253 0
0.1504 0
0.1754 0
0.2005 0
0.2256 0
0.2506 0
0.2757 0
0.3008 0
0.3258 0
0.3509 0
0.3759 0
0.4010 0
0.4261 0
0.4511 0
0.4762 0
0.5013 0
0.5263 0
0.5514 0
0.5764 0
0.6015 0
0.6266 0
0.6516 0
0.6767 0
0.7018 0
0.7268 0
0.7519 0
0.7769 0
0.8020 0
0.8271 0
0.8521 0
0.8772 0
0.9023 0
0.9273 0
0.9524 0
0.9774 0
1.0025 0
1.0276 0
1.0526 0
1.0777 0
1.1028 0
1.1278 0
1.1529 0
1.1779 0
1.2030 0
1.2281 0
1.2531 0
1.2782 0
1.3033 0
1.3283 0
1.3534 0
1.3784 0
1.4035 0
1.4286 0
1.4536 0
1.4787 0
1.5038 0
1.5288 0
1.5539 0
1.5789 0
1.6040 0
1.6291 0
1.6541 0
1.6792 0
1.7043 0
1.7293 0
1.7544 0
1.7794 0
1.8045 0
1.8296 0
1.8546 0
1.8797 0
1.9048 0
1.9298 0
1.9549 0
1.9799 0
2.0050 0
2.0301 0
2.0551 0
2.0802 0
2.1053 0
2.1303 0
2.1554 0
2.1805 0
2.2055 0
2.2306 0
2.2556 0
2.2807 0
2.3058 0
2.3308 0
2.3559 0
2.3810 0
2.4060 0
2.4311 0
2.4561 0
2.4812 0
2.5063 0
2.5313 0
2.5564 0
2.5815 0
2.6065 0
2.6316 0
2.6566 0
2.6817 0
2.7068 0
2.7318 0
2.7569 0
2.7820 0
2.8070 0
2.8321 0
2.8571 0
2.8822 0
2.9073 0
2.9323 0
2.9574 0
2.9825 0
3.0075 0
3.0326 0
3.0576 0
3.0827 0
3.1078 0
3.1328 0
3.1579 0
3.1830 0
3.2080 0
3.2331 0
3.2581 0
3.2832 0
3.3083 0
3.3333 0
3.3584 0
3.3835 0
3.4085 0
3.4336 0
3.4586 0
3.4837 0
3.5088 0
3.5338 0
3.5589 0
3.5840 0
3.6090 0
3.6341 0
3.6591 0
3.6842 0
3.7093 0
3.7343 0
3.7594 0
3.7845 0
3.8095 0
3.8346 0
3.8596 0
3.8847 0
3.9098 0
3.9348 0
3.9599 0
3.9850 0
4.0100 0
4.0351 0
4.0602 0
4.0852 0
4.1103 0
4.1353 0
4.1604 0
4.1855 0
4.2105 0
4.2356 0
4.2607 0
4.2857 0
4.3108 0
4.3358 0
4.3609 0
4.3860 0
4.4110 0
4.4361 0
4.4612 0
4.4862 0
4.5113 0
4.5363 0
4.5614 0
4.5865 0
4.6115 0
4.6366 0
4.6617 0
4.6867 0
4.7118 0
4.7368 0
4.7619 0
4.7870 0
4.8120 0
4.8371 0
4.8622 0
4.8872 0
4.9123 0
4.9373 0
4.9624 0
4.9875 0
5.0125 0
5.0376 0
5.0627 0
5.0877 0
5.1128 0
5.1378 0
5.1629 0
5.1880 0
5.2130 0
5.2381 0
5.2632 0
5.2882 0
5.3133 0
5.3383 0
5.3634 0
5.3885 0
5.4135 0
5.4386 0
5.4637 0
5.4887 0
5.5138 0
5.5388 0
5.5639 0
5.5890 0
5.6140 0
5.6391 0
5.6642 0
5.6892 0
5.7143 0
5.7393 0
5.7644 0
5.7895 0
5.8145 0
5.8396 0
5.8647 0
5.8897 0
5.9148 0
5.9398 0
5.9649 0
5.9900 0
6.0150 0
6.0401 0
6.0652 0
6.0902 0
6.1153 0
6.1404 0
6.1654 0
6.1905 0
6.2155 0
6.2406 0
6.2657 0
6.2907 0
6.3158 0
6.3409 0
6.3659 0
6.3910 0
6.4160 0
6.4411 0
6.4662 0
6.4912 0
6.5163 0
6.5414 0
6.5664 0
6.5915 0
6.6165 0
6.6416 0
6.6667 0
6.6917 0
6.7168 0
6.7419 0
6.7669 0
6.7920 0
6.8170 0
6.8421 0
6.8672 0
6.8922 0
6.9173 0
6.9424 0
6.9674 0
6.9925 0
7.0175 0
7.0426 0
7.0677 0
7.0927 0
7.1178 0
7.1429 0
7.1679 0
7.1930 0
7.2180 0
7.2431 0
7.2682 0
7.2932 0
7.3183 0
7.3434 0
7.3684 0
7.3935 0
7.4185 0
7.4436 0
7.4687 0
7.4937 0
7.5188 0
7.5439 0
7.5689 0
7.5940 0
7.6190 0
7.6441 0
7.6692 0
7.6942 0
7.7193 0
7.7444 0
7.7694 0
7.7945 0
7.8195 0
7.8446 0
7.8697 0
7.8947 0
7.9198 0
7.9449 0
7.9699 0
7.9950 0
8.0201 0
8.0451 0
8.0702 0
8.0952 0
8.1203 0
8.1454 0
8.1704 0
8.1955 0
8.2206 0
8.2456 0
8.2707 0
8.2957 0
8.3208 0
8.3459 0
8.3709 0
8.3960 0
8.4211 0
8.4461 0
8.4712 0
8.4962 0
8.5213 0
8.5464 0
8.5714 0
8.5965 0
8.6216 0
8.6466 0
8.6717 0
8.6967 0
8.7218 0
8.7469 0
8.7719 0
8.7970 0
8.8221 0
8.8471 0
8.8722 0
8.8972 0
8.9223 0
8.9474 0
8.9724 0
8.9975 0
9.0226 0
9.0476 0
9.0727 0
9.0977 0
9.1228 0
9.1479 0
9.1729 0
9.1980 0
9.2231 0
9.2481 0
9.2732 0
9.2982 0
9.3233 0
9.3484 0
9.3734 0
9.3985 0
9.4236 0
9.4486 0
9.4737 0
9.4987 0
9.5238 0
9.5489 0
9.5739 0
9.5990 0
9.6241 0
9.6491 0
9.6742 0
9.6992 0
9.7243 0
9.7494 0
9.7744 0
9.7995 0
9.8246 0
9.8496 0
9.8747 0
9.8997 0
9.9248 0
9.9499 0
9.9749 0
10.000 0
0 0
0 0.0039
0 0.0079
0 0.0118
0 0.0157
0 0.0197
0 0.0236
0 0.0276
0 0.0315
0 0.0354
0 0.0394
0 0.0433
0 0.0472
0 0.0512
0 0.0551
0 0.0591
0 0.0630
0 0.0669
0 0.0709
0 0.0748
0 0.0787
0 0.0827
0 0.0866
0 0.0905
0 0.0945
0 0.0984
0 0.1024
0 0.1063
0 0.1102
0 0.1142
0 0.1181
0 0.1220
0 0.1260
0 0.1299
0 0.1339
0 0.1378
0 0.1417
0 0.1457
0 0.1496
0 0.1535
0 0.1575
0 0.1614
0 0.1653
0 0.1693
0 0.1732
0 0.1772
0 0.1811
0 0.1850
0 0.1890
0 0.1929
0 0.1968
0 0.2008
0 0.2047
0 0.2087
0 0.2126
0 0.2165
0 0.2205
0 0.2244
0 0.2283
0 0.2323
0 0.2362
0 0.2401
0 0.2441
0 0.2480
0 0.2520
0 0.2559
0 0.2598
0 0.2638
0 0.2677
0 0.2716
0 0.2756
0 0.2795
0 0.2835
0 0.2874
0 0.2913
0 0.2953
0 0.2992
0 0.3031
0 0.3071
0 0.3110
0 0.3149
0 0.3189
0 0.3228
0 0.3268
0 0.3307
0 0.3346
0 0.3386
0 0.3425
0 0.3464
0 0.3504
0 0.3543
0 0.3583
0 0.3622
0 0.3661
0 0.3701
0 0.3740
0 0.3779
0 0.3819
0 0.3858
0 0.3897
0 0.3937
0 0.3976
0 0.4016
0 0.4055
0 0.4094
0 0.4134
0 0.4173
0 0.4212
0 0.4252
0 0.4291
0 0.4331
0 0.4370
0 0.4409
0 0.4449
0 0.4488
0 0.4527
0 0.4567
0 0.4606
0 0.4645
0 0.4685
0 0.4724
0 0.4764
0 0.4803
0 0.4842
0 0.4882
0 0.4921
0 0.4960
0 0.5000
0 0.5039
0 0.5079
0 0.5118
0 0.5157
0 0.5197
0 0.5236
0 0.5275
0 0.5315
0 0.5354
0 0.5393
0 0.5433
0 0.5472
0 0.5512
0 0.5551
0 0.5590
0 0.5630
0 0.5669
0 0.5708
0 0.5748
0 0.5787
0 0.5827
0 0.5866
0 0.5905
0 0.5945
0 0.5984
0 0.6023
0 0.6063
0 0.6102
0 0.6141
0 0.6181
0 0.6220
0 0.6260
0 0.6299
0 0.6338
0 0.6378
0 0.6417
0 0.6456
0 0.6496
0 0.6535
0 0.6575
0 0.6614
0 0.6653
0 0.6693
0 0.6732
0 0.6771
0 0.6811
0 0.6850
0 0.6889
0 0.6929
0 0.6968
0 0.7008
0 0.7047
0 0.7086
0 0.7126
0 0.7165
0 0.7204
0 0.7244
0 0.7283
0 0.7323
0 0.7362
0 0.7401
0 0.7441
0 0.7480
0 0.7519
0 0.7559
0 0.7598
0 0.7637
0 0.7677
0 0.7716
0 0.7756
0 0.7795
0 0.7834
0 0.7874
0 0.7913
0 0.7952
0 0.7992
0 0.8031
0 0.8071
0 0.8110
0 0.8149
0 0.8189
0 0.8228
0 0.8267
0 0.8307
0 0.8346
0 0.8385
0 0.8425
0 0.8464
0 0.8504
0 0.8543
0 0.8582
0 0.8622
0 0.8661
0 0.8700
0 0.8740
0 0.8779
0 0.8819
0 0.8858
0 0.8897
0 0.8937
0 0.8976
0 0.9015
0 0.9055
0 0.9094
0 0.9133
0 0.9173
0 0.9212
0 0.9252
0 0.9291
0 0.9330
0 0.9370
0 0.9409
0 0.9448
0 0.9488
0 0.9527
0 0.9567
0 0.9606
0 0.9645
0 0.9685
0 0.9724
0 0.9763
0 0.9803
0 0.9842
0 0.9881
0 0.9921
0 0.9960
0 1.0000
0 1.0039
0 1.0078
0 1.0118
0 1.0157
0 1.0196
0 1.0236
0 1.0275
0 1.0315
0 1.0354
0 1.0393
0 1.0433
0 1.0472
0 1.0511
0 1.0551
0 1.0590
0 1.0629
0 1.0669
0 1.0708
0 1.0748
0 1.0787
0 1.0826
0 1.0866
0 1.0905
0 1.0944
0 1.0984
0 1.1023
0 1.1063
0 1.1102
0 1.1141
0 1.1181
0 1.1220
0 1.1259
0 1.1299
0 1.1338
0 1.1377
0 1.1417
0 1.1456
0 1.1496
0 1.1535
0 1.1574
0 1.1614
0 1.1653
0 1.1692
0 1.1732
0 1.1771
0 1.1810
0 1.1850
0 1.1889
0 1.1929
0 1.1968
0 1.2007
0 1.2047
0 1.2086
0 1.2125
0 1.2165
0 1.2204
0 1.2244
0 1.2283
0 1.2322
0 1.2362
0 1.2401
0 1.2440
0 1.2480
0 1.2519
0 1.2558
0 1.2598
0 1.2637
0 1.2677
0 1.2716
0 1.2755
0 1.2795
0 1.2834
0 1.2873
0 1.2913
0 1.2952
0 1.2992
0 1.3031
0 1.3070
0 1.3110
0 1.3149
0 1.3188
0 1.3228
0 1.3267
0 1.3306
0 1.3346
0 1.3385
0 1.3425
0 1.3464
0 1.3503
0 1.3543
0 1.3582
0 1.3621
0 1.3661
0 1.3700
0 1.3740
0 1.3779
0 1.3818
0 1.3858
0 1.3897
0 1.3936
0 1.3976
0 1.4015
0 1.4054
0 1.4094
0 1.4133
0 1.4173
0 1.4212
0 1.4251
0 1.4291
0 1.4330
0 1.4369
0 1.4409
0 1.4448
0 1.4488
0 1.4527
0 1.4566
0 1.4606
0 1.4645
0 1.4684
0 1.4724
0 1.4763
0 1.4802
0 1.4842
0 1.4881
0 1.4921
0 1.4960
0 1.4999
0 1.5039
0 1.5078
0 1.5117
0 1.5157
0 1.5196
0 1.5236
0 1.5275
0 1.5314
0 1.5354
0 1.5393
0 1.5432
0 1.5472
0 1.5511
0 1.5550
0 1.5590
0 1.5629
0 1.5669
0 1.5708
But i want to modify this function so that it can two lists like:
trajectory([x1, x2, x3, ... xn], [phi1, phi2, phi3, ..., phin])
and create a matrix like this:
trajectory =
x1 0
x1+k 0
. 0
. 0
x1_n 0
0 phi1
0 phi1+k
0 .
0 .
0 phi1_n
x2 0
x2+k 0
. 0
. 0
x2_n 0
0 phi2
0 phi2+k
0 .
0 .
0 phi2_n
and so on. So I was wandering if there was a more automatic way expanding the matrix, so that two lists can be provided as an input argument and the matrix expanding according to the elements of the list.
function p = matr(x, phi)
p = zeros(800*length(x), 2);
for ii = 1:length(x)
x_dir = linspace(0, x(ii), 400);
r = linspace(0, phi(ii), 400);
p((800*(ii-1)+1):(800*(ii-1)+400), 1) = x_dir;
p((800*ii-399):(800*ii), 2) = r;
end
end
OR
function p2 = matr_compound(x, phi)
p2 = [];
for ii = 1:length(x)
p2 = [p2; matr(x(ii), phi(ii))];
end
end

Project schedule plot in Matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to make some changes in the plot done by my senior, in Java. And since I do not know that language, I want to do it in Matlab. I have attached both the data (text format) and plot here. It is the output of project schedule where the value represents the cost associated with task (link) and their continuation in time (say task 2 is done from 6th to 9th month and cost is 0.665m $).
Value 0 indicate that there is no work in that time period for a particular task (row). The plot in Java, shows the variation in time and cost for each task.
I have been trying to make similar kind of plot (bar in case of the circles) in Matlab but could not.
Link# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 0 0 0 0 0 0 0 0 0 0 0 0 0.34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0.665 0.665 0.665 0.665 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0.34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0.36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0.14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0.57 0.57 0.57 0.57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 0.64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0.58 0.58 0.58 0.58 0.58 0.58 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 0.24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11 0.56 0.56 0.56 0.56 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 0.2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
13 0 0 0 0 0 0 0 0 0.56 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.62 0 0 0 0 0 0 0 0 0 0 0
15 0 0 0 0.582857143 0.582857143 0.582857143 0.582857143 0.582857143 0.582857143 0.582857143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16 0.536 0.536 0.536 0.536 0.536 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
17 0.2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18 0 0.6 0.6 0.6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
19 0 0 0 0 0.0000 0 0 0 0 0 0 0 0.606666667 0.606666667 0.606666667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
20 0 0 0 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0.633333333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
21 0 0 0.606666667 0.606666667 0.606666667 0.606666667 0.606666667 0.606666667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
22 0 0 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0.643076923 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
23 0 0 0 0 0 0 0 0.58 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
24 0 0 0 0 0 0 0 0 0 0.651428571 0.651428571 0.651428571 0.651428571 0.651428571 0.651428571 0.651428571 0 0 0 0 0 0 0 0 0 0 0 0 0 0
25 0 0 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
26 0 0 0 0 0 0 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
27 0.546666667 0.546666667 0.546666667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
28 0 0.655 0.655 0.655 0.655 0.655 0.655 0.655 0.655 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
29 0.5 0.5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
30 0 0 0 0 0 0 0 0 0 0.44 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
31 0.54 0.54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
32 0 0.64 0.64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
33 0 0 0 0 0 0 0 0 0 0.611428571 0.611428571 0.611428571 0.611428571 0.611428571 0.611428571 0.611428571 0 0 0 0 0 0 0 0 0 0 0 0 0 0
34 0 0 0 0 0 0 0 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0.62 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
35 0.36 0.36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
36 0 0 0 0 0 0 0 0 0 0 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0 0 0 0 0 0 0 0 0 0 0 0
37 0.38 0.38 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
38 0.613333333 0.613333333 0.613333333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
39 0.58 0.58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
40 0 0 0 0 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0.647272727 0 0 0 0
java plot
Here is an example for a solution, assuming data is all what is within the chart (i.e., no axis tick labels):
Links_num = size(data,1);
[y,x] = ndgrid(1:Links_num,1:size(data,2));
c = repelem(1:Links_num,size(data,2),1).';
scatter(x(:),y(:),data(:)*100+1,c(:),'filled')
clr = lines(Links_num);
colormap(clr)
title('Time Period')
ax = gca;
ax.XAxisLocation = 'top';
ax.XAxis.TickValues(1) = [];
clr = lines(Links_num); % make a color map
ax.YAxis.Direction = 'reverse';
links = [repmat('Link ',Links_num,1) num2str((1:Links_num).')];
x = repelem(ax.XAxis.Limits(1)-0.05,Links_num); % make an x position vector
for k = 1:Links_num
text(ax.XAxis.Limits(1)-0.05,k,links(k,:),'Color',clr(k,:),...
'HorizontalAlignment','right');
end
ax.YAxis.Visible = 'off'; % remove the original labels
The result:

Numerical errors when solving Ax=b in MATLAB

I am attempting to solve the system Ax = b in MATLAB, where A is a 30x30 triangular matrix with (nonzero) values ranging from 1e-14 to 0.7, and b is a 30-element column vector with values ranging from 1e-3 to 1e3. When I enter x = A\b, I get an answer and no warning messages, but the answer is not reasonable (looks like just random numbers at the bottom of the vector). I presume this is due to numerical errors.
Message 5 on this page suggests decomposing/scaling the matrix in order to avoid numerical errors, but I haven't been able to figure out how to calculate the scaling matrices.
So the question is: Is this indeed an example of numerical instability, and if so, how can I rescale my matrix A, or change how MATLAB is performing the calculation, to avoid it?
Here is the matrix and vector that are producing the issue:
A =
Columns 1 through 15
0.69 0.4278 0.19893 0.082223 0.031861 0.011852 0.0042866 0.0015187 0.00052965 0.00018243 6.221e-05 2.1038e-05 7.0653e-06 2.3587e-06 7.8344e-07
0 0.4761 0.44277 0.27452 0.14183 0.065953 0.028624 0.011831 0.0047156 0.0018273 0.00069233 0.00025755 9.4356e-05 3.4126e-05 1.2206e-05
0 0 0.32851 0.40735 0.3157 0.19573 0.10618 0.052668 0.02449 0.010846 0.004623 0.0019108 0.00077007 0.00030383 0.00011773
0 0 0 0.22667 0.35134 0.32675 0.23635 0.14653 0.081766 0.042246 0.02058 0.0095696 0.0042851 0.0018597 0.00078615
0 0 0 0 0.1564 0.29091 0.31564 0.26093 0.182 0.11284 0.064129 0.03408 0.017168 0.0082788 0.0038496
0 0 0 0 0 0.10792 0.23418 0.29039 0.27006 0.2093 0.14274 0.088499 0.05095 0.02764 0.014281
0 0 0 0 0 0 0.074464 0.18467 0.25761 0.2662 0.22694 0.16884 0.1134 0.070311 0.040868
0 0 0 0 0 0 0 0.05138 0.14335 0.22219 0.25256 0.23488 0.18931 0.13694 0.090965
0 0 0 0 0 0 0 0 0.035452 0.1099 0.18738 0.23235 0.2341 0.2032 0.15748
0 0 0 0 0 0 0 0 0 0.024462 0.083415 0.15515 0.20842 0.22614 0.21031
0 0 0 0 0 0 0 0 0 0 0.016879 0.062789 0.12652 0.18303 0.21277
0 0 0 0 0 0 0 0 0 0 0 0.011646 0.046935 0.10185 0.15786
0 0 0 0 0 0 0 0 0 0 0 0 0.008036 0.034876 0.081087
0 0 0 0 0 0 0 0 0 0 0 0 0 0.0055448 0.025783
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0038259
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 16 through 30
2.5906e-07 8.5327e-08 2.8007e-08 9.1646e-09 2.9906e-09 9.7342e-10 3.1613e-10 1.0246e-10 3.3142e-11 1.0702e-11 3.4504e-12 1.1108e-12 3.5709e-13 1.1465e-13 3.6767e-14
4.3246e-06 1.5194e-06 5.2988e-07 1.8359e-07 6.3236e-08 2.1667e-08 7.3883e-09 2.5085e-09 8.4833e-10 2.8585e-10 9.5998e-11 3.214e-11 1.073e-11 3.5726e-12 1.1866e-12
4.492e-05 1.6909e-05 6.2902e-06 2.3156e-06 8.445e-07 3.0543e-07 1.0963e-07 3.9084e-08 1.3847e-08 4.8779e-09 1.7094e-09 5.9615e-10 2.0698e-10 7.1568e-11 2.4651e-11
0.00032494 0.00013173 5.2503e-05 2.0616e-05 7.9887e-06 3.0592e-06 1.1591e-06 4.3497e-07 1.6181e-07 5.9715e-08 2.1877e-08 7.9615e-09 2.8794e-09 1.0354e-09 3.7037e-10
0.0017358 0.00076232 0.00032721 0.00013766 5.69e-05 2.3151e-05 9.2878e-06 3.679e-06 1.4406e-06 5.5824e-07 2.1426e-07 8.1515e-08 3.0763e-08 1.1523e-08 4.2867e-09
0.0070833 0.0033935 0.001578 0.00071495 0.00031662 0.00013741 5.8573e-05 2.4566e-05 1.0154e-05 4.1418e-06 1.6691e-06 6.6527e-07 2.6248e-07 1.0259e-07 3.9755e-08
0.022523 0.01187 0.0060211 0.0029554 0.0014095 0.00065541 0.00029799 0.00013279 5.8116e-05 2.5022e-05 1.0615e-05 4.4423e-06 1.8361e-06 7.5031e-07 3.0339e-07
0.056398 0.033024 0.018428 0.0098671 0.005098 0.0025529 0.0012436 0.00059114 0.00027488 0.00012531 5.6113e-05 2.4719e-05 1.0728e-05 4.5926e-06 1.9414e-06
0.11158 0.073506 0.045573 0.026843 0.01513 0.0082078 0.0043059 0.0021929 0.0010877 0.00052686 0.00024979 0.00011615 5.3064e-05 2.3852e-05 1.0563e-05
0.17385 0.13089 0.091294 0.059747 0.037043 0.021923 0.012459 0.0068335 0.0036315 0.0018763 0.00094518 0.00046536 0.00022441 0.00010618 4.9374e-05
0.21107 0.18539 0.14778 0.10881 0.074955 0.048796 0.030253 0.017976 0.010288 0.0056949 0.0030601 0.0016008 0.00081734 0.00040822 0.00019981
0.19575 0.20632 0.19188 0.16145 0.12513 0.090508 0.061727 0.04001 0.024806 0.014788 0.0085139 0.0047507 0.0025773 0.0013629 0.00070418
0.13406 0.17663 0.19712 0.1935 0.17139 0.13947 0.10569 0.075354 0.050967 0.032916 0.020408 0.012201 0.0070603 0.003967 0.0021702
0.063943 0.11233 0.1567 0.18459 0.19074 0.17739 0.15122 0.1198 0.089133 0.062798 0.042179 0.027157 0.016837 0.010091 0.0058655
0.018977 0.050003 0.093006 0.13695 0.16982 0.18425 0.17952 0.15999 0.13226 0.1025 0.075107 0.052387 0.034978 0.022461 0.013926
0.0026399 0.013912 0.038815 0.076207 0.11812 0.15379 0.17481 0.17806 0.16559 0.14259 0.11493 0.087452 0.063257 0.043745 0.029059
0 0.0018215 0.010164 0.029933 0.061862 0.10068 0.13733 0.16319 0.17345 0.16803 0.15048 0.12595 0.099387 0.074457 0.053266
0 0 0.0012569 0.0074028 0.022949 0.049799 0.084907 0.12108 0.15014 0.16622 0.16747 0.15575 0.13519 0.11049 0.085626
0 0 0 0.00086723 0.0053768 0.017502 0.039787 0.07092 0.10553 0.13631 0.15695 0.16421 0.15837 0.14237 0.12037
0 0 0 0 0.00059839 0.0038955 0.013284 0.031571 0.058722 0.091019 0.12227 0.1462 0.15862 0.15845 0.14736
0 0 0 0 0 0.00041289 0.0028159 0.010039 0.024896 0.048236 0.077756 0.10847 0.1345 0.15115 0.15618
0 0 0 0 0 0 0.00028489 0.0020313 0.0075564 0.019521 0.039334 0.065845 0.095256 0.12234 0.14222
0 0 0 0 0 0 0 0.00019658 0.0014625 0.0056673 0.015226 0.031861 0.05531 0.082873 0.1101
0 0 0 0 0 0 0 0 0.00013564 0.0010512 0.0042363 0.011819 0.025648 0.046115 0.071478
0 0 0 0 0 0 0 0 0 9.359e-05 0.00075433 0.0031569 0.0091339 0.020528 0.038183
0 0 0 0 0 0 0 0 0 0 6.4577e-05 0.00054051 0.0023458 0.0070296 0.016344
0 0 0 0 0 0 0 0 0 0 0 4.4558e-05 0.00038676 0.0017385 0.0053894
0 0 0 0 0 0 0 0 0 0 0 0 3.0745e-05 0.0002764 0.0012852
0 0 0 0 0 0 0 0 0 0 0 0 0 2.1214e-05 0.00019729
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.4638e-05
b =
3712
246.89
43.304
22.55
14.897
10.066
6.8138
4.6131
3.1232
2.1146
1.4316
0.96927
0.65623
0.44429
0.3008
0.20365
0.13788
0.093351
0.063202
0.04279
0.02897
0.019614
0.013279
0.0089906
0.006087
0.0041211
0.0027902
0.001889
0.0012789
0.00086589
A .mat file with the full-precision variables may be found here.
Here are the results I'm getting on my machine (Matlab R2013a on OS X 10.10.5):
>> x=A\b
x =
5087.6
433.99
64.166
27.995
19.494
14.546
10.934
8.2265
6.1834
4.6933
3.2779
3.8272
-3.5375
23.953
-79.278
254.22
-702.1
1713.2
-3658.2
6822.7
-11046
15412
-18349
18393
-15244
10181
-5273.4
1992.3
-489.85
59.155
Although norm(A*x-b) returns a value on the order of 1e-13, the results are not physically reasonable given the problem I am trying to solve (values in x should be monotonically decreasing, and none should be negative). As an example, here is a similar dataset that returns a correct (looking) solution with the same matrix A:
>> c
c =
5142.1
339.52
22.417
1.4802
0.097731
0.0064529
0.00042607
2.8132e-05
1.8575e-06
1.2265e-07
8.0979e-09
5.3469e-10
3.5304e-11
2.331e-12
1.5391e-13
1.0162e-14
6.7099e-16
4.4304e-17
2.9253e-18
1.9315e-19
1.2753e-20
8.4205e-22
5.5598e-23
3.671e-24
2.4239e-25
1.6004e-26
1.0567e-27
6.9771e-29
4.6068e-30
3.0418e-31
>> x = A\c
x =
7029.1
653.25
60.709
5.642
0.52434
0.04873
0.0045287
0.00042087
3.9114e-05
3.635e-06
3.3782e-07
3.1395e-08
2.9177e-09
2.7116e-10
2.52e-11
2.342e-12
2.1765e-13
2.0227e-14
1.8798e-15
1.747e-16
1.6236e-17
1.5089e-18
1.4023e-19
1.3033e-20
1.21e-21
1.1339e-22
9.9766e-24
1.1858e-24
2.3902e-26
2.078e-26

Mexed function gives very slight difference when performing math

So I tried the matrix exponential function using MATLAB's Coder toolkit, and I got it to build. I went on to test to see if the results were reliable and more efficient. While the code was faster, the answer it produced was very slight.
I ran the original function and got an answer of:
p =
1 0 0 0 0 0 0 0 0 0 0 0
-0.05 1 -1.25e-07 5e-06 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
1.25e-07 -5e-06 -0.05 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 -0.05 1 0 0 0 0 0 0
-2.0833e-05 0.00125 -5.2083e-11 4.1667e-09 0 0 1 0.05 0 1.25e-07 0 0
-0.00125 0.05 -4.1667e-09 2.5e-07 0 0 0 1 0 5e-06 0 0
5.2083e-11 -4.1667e-09 -2.0833e-05 0.00125 0 0 0 -1.25e-07 1 0.05 0 0
4.1667e-09 -2.5e-07 -0.00125 0.05 0 0 0 -5e-06 0 1 0 0
0 0 0 0 -2.0833e-05 0.00125 0 0 0 0 1 0.05
0 0 0 0 -0.00125 0.05 0 0 0 0 0 1
And I then ran the mexed version of the function with the same input:
p2 =
1 0 0 0 0 0 0 0 0 0 0 0
-0.05 1 -1.25e-07 5e-06 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
1.25e-07 -5e-06 -0.05 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 -0.05 1 0 0 0 0 0 0
-2.0833e-05 0.00125 -5.2083e-11 4.1667e-09 0 0 1 0.05 0 1.25e-07 0 0
-0.00125 0.05 -4.1667e-09 2.5e-07 0 0 0 1 0 5e-06 0 0
5.2083e-11 -4.1667e-09 -2.0833e-05 0.00125 0 0 0 -1.25e-07 1 0.05 0 0
4.1667e-09 -2.5e-07 -0.00125 0.05 0 0 0 -5e-06 0 1 0 0
0 0 0 0 -2.0833e-05 0.00125 0 0 0 0 1 0.05
0 0 0 0 -0.00125 0.05 0 0 0 0 0 1
At first glance, these two matrices are equal, but they are actually VERY SLIGHTLY off:
p-p2
ans =
0 0 0 0 0 0 0 0 0 0 0 0
-6.9389e-18 0 0 8.4703e-22 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
-3.3881e-21 -2.1684e-19 -3.2312e-26 8.2718e-25 0 0 0 0 0 5.294e-23 0 0
-2.1684e-19 0 -8.2718e-25 5.294e-23 0 0 0 0 0 8.4703e-22 0 0
6.4623e-27 0 -3.3881e-21 2.1684e-19 0 0 0 0 0 0 0 0
0 0 0 6.9389e-18 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Most of the result is equivalent to the original function, but some of it is not. Also, the difference between the two is so small that I cant possibly believe it would be a mathematical error, rather than perhaps a precision error. And the reason I am so concerned with this, is because this does cause issues with the overlaying reason I am using the function.
Is there a reason why the mex function is off by so little, and is there a way to fix this?
The differences you observe are so little that you can consider the results actually are the same.
The way they are computed are different and that is why you do not get exactly the same result. Yet the difference is roughly machine epsilon and is just due to the fact that computers do not work with an infinite precision but with some discrete representation of the numbers.

Flipping Around 5 percent of 1's and 0's

For the following letter, I wish to add noise to it by changing 5 percent of the 1's into 0's. So far, I have the following code which turns them all into 0's. Can someone please point me in the right direction? Thank you!
letterA = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0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== 1 & rand(size(letterA)) <= 0.05 ) = 0;
In fact you could also do
letterA( rand(size(letterA)) <= 0.05 ) = 0;
which sets each element with probability of 5% to zero. The already zero elements are not affected. I think what causes confusion here is that you have to recognize that each element is independently handled from each other. It makes no difference if you do the first or the second version.
You can check it:
letterA = (rand(1e5,1) < 0.2); N1 = nnz(letterA);
letterA( rand(size(letterA)) <= 0.05 ) = 0;
(N1 - nnz(letterA))/N1
which gives values around 0.05, i.e. 5%. And it is not true what EitanT says, that it will flip at maximum 5%. It can be more than 5% or less, but on average it is 5%.
EitanTs version flippes exactly 5%, so which version to select depends on the application. For EitanT version the noise is correlated to the signal (because it is exact), which may or may not be what you want.
The basic approach is to find the indices of the 1's and count them, randomly pick a desired amount of indices out of them, and then operate on them:
one_flip_ratio = 0.05;
idx_ones = find(letterA == 1); %// Indices of 1's
flips = round(one_flip_ratio * numel(idx_ones)); %// Number of flips
idx_flips = idx_ones(randperm(numel(idx_ones), flips)); %// Indices of elements
letterA(idx_flips) = 0; %// Flip elements
This will flip 5% of the 1's to 0's.
Thanks for throwing out all these ideas, but eventually I came up with this. It will allow me to easily control both letter and background noise, which is what I intend to do. I'm just a novice, so this may not be the most efficient code, but it gets the job done! (I'm not looking for exactly 5%, the naked eye display value is what I'm more worried about.) PLEASE let me know how this can be improved! Thank you.
background_noise_intensity=0.05;
letter_noise_intensity=0.05;
for i=1:numel(letterA)
if letterA(i)==0
if rand < background_noise_intensity
letterA(i)=1;
end
elseif letterA(i)==1
if rand < letter_noise_intensity
letterA(i)=0;
end
end
end
noisy_letters=letterA;
reshaped_noisy_letters=reshape(noisy_letters,37,19)';
imshow(reshaped_noisy_letters);