Calculate radius r = x^2 + y^2 using array values - arrays

I would like to use my calculated y[0] (x-values) and y[1] (y-values) from sol_3 using the solve_ivp to find the radius(r) and plot r(t).
How can I calculate r using x and y values from sol_r? I keep getting TypeError: only size-1 arrays can be converted to Python scalars.
It seems like I am having issues with data type.
Please find my code below:
timing = np.array([0,2*math.pi]) #t0 = 0 tf=2pi
sol_3 = solve_ivp(fun = motion, t_span=[-math.pi, 2*math.pi], y0 = initial, method='RK23')
x = (sol_3.y[0, :])
y = (sol_3.y[1, :])
r = math.sqrt(sol_3.y[1,:]**2+sol_3.y[0,:]**2)
The array of the sol_3 looks like this:
message: 'The solver successfully reached the end of the integration interval.'
nfev: 599
njev: 0
nlu: 0
sol: None
status: 0
success: True
t: array([-3.14159265, -3.14155739, -3.14120473, -3.13767817, -3.12223578,
-3.09595606, -3.0602884 , -3.01493539, -2.96220999, -2.90868487,
-2.86527118, -2.82204945, -2.76814683, -2.69499434, -2.61108567,
-2.51943819, -2.44000487, -2.36057156, -2.28852194, -2.20054302,
-2.10648128, -2.03001656, -1.97695204, -1.92388751, -1.87465566,
-1.82156056, -1.76673945, -1.73011416, -1.70585555, -1.68159695,
-1.64979671, -1.60847127, -1.55783888, -1.50388809, -1.45063319,
-1.40126952, -1.3488944 , -1.27797028, -1.19501887, -1.1039372 ,
-1.02086808, -0.95113058, -0.88636741, -0.80259334, -0.70932861,
-0.61584263, -0.55777683, -0.50936554, -0.46344206, -0.4113168 ,
-0.35663683, -0.31427459, -0.28317047, -0.25206634, -0.22135512,
-0.18102468, -0.13108896, -0.07689042, -0.02318517, 0.02738243,
0.07818198, 0.14701599, 0.22918967, 0.31978884, 0.41431515,
0.48493772, 0.55000025, 0.63412425, 0.72775963, 0.82162403,
0.87984573, 0.92849841, 0.97470164, 1.02711062, 1.0820909 ,
1.12462382, 1.15583674, 1.18704967, 1.21796912, 1.25856295,
1.30880593, 1.36330716, 1.41729713, 1.46809564, 1.51919004,
1.58841181, 1.67102044, 1.76206092, 1.85696013, 1.92801041,

Related

Argument types problem ('float', 'const int') in array

I've been trying my first codes in pine script. The question is this. I have created few array.new_float to use as buffers in the 'for' statement. The thing is that I need to do some math over the data. Now, once the 'for' is done, an error pops: 'Cannot call 'operator -' with argument 'expr0' = 'High'.An argument of 'float[]' type was used but a 'const int' is expected'.
Please, if anyone knows what am I doing wrong, I will thank you.
Edit: I will leave the script of what I'm trying to do here
//#version=5
// Indicator name
indicator("DAF_Swing_Index", shorttitle= 'DAF_SwInd', overlay=false)
// Input
T = input.int(30000, title = 'Ratio de escala', minval = 1000, maxval = 150000)
Shift = input.int(0, title = 'Desplazamiento horizontal', minval = 0, maxval = 100)
// Array
SWINGINDEX = array.new_float(200)
Open = array.new_float(200)
Open1 = array.new_float(200)
Close = array.new_float(200)
Close1 = array.new_float(200)
High = array.new_float(200)
Low = array.new_float(200)
// Other variable
var float SwingIndex = 0
var int StartBars = 1
Prev_calculated = bar_index
Rates_total = bar_index + 1
var float SH1 = 0
var float SI = 0
var float R = 0
// Initial bar verification
if Rates_total < StartBars
SwingIndex := 0
Primero = 1
if Prev_calculated > Rates_total or Prev_calculated <= 0
Primero := 1
else
Primero := Prev_calculated-1
// Main process
for bar = Primero to Rates_total
array.push(Open, high[bar])
array.push(Open1, open[bar-1])
array.push(Close, close[bar])
array.push(Close1, close[bar-1])
array.push(High, high[bar])
array.push(Low, low[bar])
K = math.max(math.abs(High - Close1), math.abs(Low - Close1))
TR = math.max(math.max(math.abs(High-Close1), math.abs(Low-Close1)), math.abs(High-Low))
ER = 0.0
if Close1 > High
ER := math.abs(High - Close1)
if Close1 < Low
ER := math.abs(Low - Close1)
SH1 := math.abs(Close1 - Open1)
R := TR - 0.5 * ER + 0.25 * SH1
SI := 0.0
if R != 0
SI := 50 * ((Close - Close1) + 0.5 * (Close - Open1)) * (K / T) / R
SwingIndex := SI
// ploting result
plot(SwingIndex, title = 'Swing Index', style = plot.style_line, color = color.rgb(193, 255, 51, 10))
So, what the error message tells you is, your are passing an array, where it expects a const value.
Like here:
K = math.max(math.abs(High - Close1), math.abs(Low - Close1))
All those variables (High, Close1, Low) are arrays. It simply can not subtract one array from another. You can however, subtract one element from another element.
So for that line, I believe you want something like this:
K = math.max(math.abs(array.get(High, bar) - array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar)))
With array.get(), you can get value the of the element at the specified index.
You should fix this in all other occurences.

baseline fitting using Numpy poly1d

i have the following baseline:
and as it can be seen, it has an almost sinusoidal shape. i am trying to use polyfit on it. Actually what I have are two arrays of data,one called x and the other y. So what i am using is:
porder = 2
coefs = np.polyfit(x, y, porder)
baseline = np.poly1d(coefs)
cleanspec = y - baseline(x)
My goal is to obtain a clean spectrum in the end, who has a straight baseline with no ondulation.
However, the fitting is not working. Any suggestions on using another more efficient method?
I have tried changing porder to 3, but i have this warning, and it doesn't change anything:
Polyfit may be poorly conditioned
My data for x:
[1.10192816e+11 1.10192893e+11 1.10192969e+11 1.10193045e+11
1.10193122e+11 1.10193198e+11 1.10193274e+11 1.10193350e+11
1.10193427e+11 1.10193503e+11 1.10193579e+11 1.10193656e+11
1.10193732e+11 1.10193808e+11 1.10193885e+11 1.10193961e+11
1.10194037e+11 1.10194113e+11 1.10194190e+11 1.10194266e+11
1.10194342e+11 1.10194419e+11 1.10194495e+11 1.10194571e+11
1.10194647e+11 1.10194724e+11 1.10194800e+11 1.10194876e+11
1.10194953e+11 1.10195029e+11 1.10195105e+11 1.10195182e+11
1.10195258e+11 1.10195334e+11 1.10195410e+11 1.10195487e+11
1.10195563e+11 1.10195639e+11 1.10195716e+11 1.10195792e+11
1.10195868e+11 1.10195944e+11 1.10196021e+11 1.10196097e+11
1.10196173e+11 1.10196250e+11 1.10196326e+11 1.10196402e+11
1.10196479e+11 1.10196555e+11 1.10196631e+11 1.10196707e+11
1.10196784e+11 1.10196860e+11 1.10196936e+11 1.10197013e+11
1.10197089e+11 1.10197165e+11 1.10197241e+11 1.10197318e+11
1.10197394e+11 1.10197470e+11 1.10197547e+11 1.10197623e+11
1.10197699e+11 1.10197776e+11 1.10197852e+11 1.10197928e+11
1.10198004e+11 1.10198081e+11 1.10198157e+11 1.10198233e+11
1.10198310e+11 1.10198386e+11 1.10198462e+11 1.10198538e+11
1.10198615e+11 1.10198691e+11 1.10198767e+11 1.10198844e+11
1.10198920e+11 1.10198996e+11 1.10199073e+11 1.10199149e+11
1.10199225e+11 1.10199301e+11 1.10199378e+11 1.10199454e+11
1.10199530e+11 1.10199607e+11 1.10199683e+11 1.10199759e+11
1.10199835e+11 1.10199912e+11 1.10199988e+11 1.10200064e+11
1.10200141e+11 1.10202582e+11 1.10202658e+11 1.10202735e+11
1.10202811e+11 1.10202887e+11 1.10202963e+11 1.10203040e+11
1.10203116e+11 1.10203192e+11 1.10203269e+11 1.10203345e+11
1.10203421e+11 1.10203498e+11 1.10203574e+11 1.10203650e+11
1.10203726e+11 1.10203803e+11 1.10203879e+11 1.10203955e+11
1.10204032e+11 1.10204108e+11 1.10204184e+11 1.10204260e+11
1.10204337e+11 1.10204413e+11 1.10204489e+11 1.10204566e+11
1.10204642e+11 1.10204718e+11 1.10204795e+11 1.10204871e+11
1.10204947e+11 1.10205023e+11 1.10205100e+11 1.10205176e+11
1.10205252e+11 1.10205329e+11 1.10205405e+11 1.10205481e+11
1.10205557e+11 1.10205634e+11 1.10205710e+11 1.10205786e+11
1.10205863e+11 1.10205939e+11 1.10206015e+11 1.10206092e+11
1.10206168e+11 1.10206244e+11 1.10206320e+11 1.10206397e+11
1.10206473e+11 1.10206549e+11 1.10206626e+11 1.10206702e+11
1.10206778e+11 1.10206854e+11 1.10206931e+11 1.10207007e+11
1.10207083e+11 1.10207160e+11 1.10207236e+11 1.10207312e+11
1.10207389e+11 1.10207465e+11 1.10207541e+11 1.10207617e+11
1.10207694e+11 1.10207770e+11 1.10207846e+11 1.10207923e+11
1.10207999e+11 1.10208075e+11 1.10208151e+11 1.10208228e+11
1.10208304e+11 1.10208380e+11 1.10208457e+11 1.10208533e+11
1.10208609e+11 1.10208686e+11 1.10208762e+11 1.10208838e+11
1.10208914e+11 1.10208991e+11 1.10209067e+11 1.10209143e+11
1.10209220e+11 1.10209296e+11 1.10209372e+11 1.10209448e+11
1.10209525e+11 1.10209601e+11 1.10209677e+11 1.10209754e+11
1.10209830e+11]
and for y:
[ 0.00143858 0.05495827 0.07481739 0.03287334 -0.06275658 0.03744501
-0.04392341 0.02849104 0.03173781 0.09748282 0.02854265 0.06573162
0.08215295 0.0240697 0.00931477 0.17572605 0.06783381 0.04853354
-0.00226023 0.03722596 0.09687121 0.10767829 0.04922701 0.08036865
0.02371989 0.13885361 0.13903188 0.09910567 0.08793601 0.06048823
0.03932097 0.04061129 0.03706228 0.13764936 0.14150589 0.12226208
0.09041878 0.13638676 0.11107155 0.12261369 0.11765545 0.07425344
0.06643712 0.1449991 0.14256909 0.0924173 0.09291525 0.12216271
0.11272059 0.07618891 0.16787807 0.07832849 0.10786856 0.12381844
0.14182937 0.08078092 0.11932429 0.06383649 0.02923562 0.0864741
0.07806758 0.04514088 0.12929371 0.11769577 0.03619867 0.02811366
0.06401639 0.06883735 0.01162673 0.0956252 0.11206549 0.0485106
0.07269545 0.01662149 0.01287365 0.13401546 0.06300487 0.01994627
0.00721926 0.04863274 -0.01578364 0.0235379 0.03102316 0.00392559
0.05662182 0.04643381 -0.00665026 0.05532307 -0.01533339 0.04838893
0.02097954 0.02551123 0.03727188 -0.04001189 -0.04294883 0.02837669
-0.06062512 -0.0743994 -0.04665618 -0.03553261 -0.07057554 -0.07028277
-0.07502298 -0.07247965 -0.03540266 -0.03226398 -0.08014487 -0.11907543
-0.18521053 -0.1117617 -0.14377897 -0.07113503 -0.02480966 -0.07459746
-0.07994097 -0.02648713 -0.10288478 -0.13328137 -0.08121377 -0.13742166
-0.024583 -0.11391389 -0.02717251 -0.08876166 -0.04369363 -0.0790144
-0.09589054 -0.12058701 0.00041344 -0.06646403 -0.06368366 -0.10335613
-0.04508286 -0.18360729 -0.0551775 -0.06476622 -0.0834523 -0.01276785
-0.04145486 -0.14549992 -0.11186823 -0.07663398 -0.11920359 -0.0539315
-0.10507118 -0.09112374 -0.09751319 -0.06848278 -0.09031172 -0.07218853
-0.03129234 -0.04543539 -0.00942861 -0.06711099 -0.00712202 -0.11696418
-0.06344093 0.03624227 -0.04798777 0.01174394 -0.08326314 -0.06761215
-0.12063419 -0.05236908 -0.03914692 -0.05370061 -0.01620056 0.06731788
-0.06600111 -0.04601257 -0.02144361 0.00256863 -0.00093034 0.00629604
-0.0252835 -0.00907992 0.03583489 -0.03761906 0.10325763 0.08016437
-0.04900467 0.0110328 0.05019604 -0.04428984 -0.03208058 0.05095359
-0.01807463 0.0691733 0.07472691 0.00659871 0.00947692 0.0014422
0.05227057]
Having this huge offset in x is probably not helping. It definitively works when removing it for the fitting process. Looks like this:
import matplotlib.pyplot as plt
import numpy as np
scaledx = xdata * 1e-8 - 1100
coefs = np.polyfit( scaledx, ydata, 7)
base = np.poly1d( coefs )
xt = np.linspace( 1.9,2.1,150)
yt = base( xt )
fig = plt.figure()
ax = fig.add_subplot( 2, 1, 1 )
bx = fig.add_subplot( 2, 1, 2 )
ax.scatter( scaledx , ydata )
ax.plot( xt , yt )
bx.plot( scaledx , ydata - base( scaledx ) )
plt.show()
with xdata and ydata being numpy arrays of the OP data lists.
Provides:
Addon
Concerning the poorly conditioned one should remember how simple linear optimization works. In case of a polynomial one builds the matrix:
A = [
[1, x1, x1**2, ...],
[1, x2, x2**2, ...],
...
[1, xn, xn**2, ...]
]
and one needs B^(-1) the inverse of B with B = AT.A and AT being the transposed of A. Now looking at the x values in the order of 1e11, B will have order 1 on one side of the diagonal and for a second order polynomial order 1e44 on the other. In case of a third order polynomial this is getting worse, accordingly. Making the inverse, hence, is becoming unstable, numerically. Luckily, and as used above, this can be solved easily by simple re-scaling of the problem at hand.

Data arrays must have the same length, and match time discretization in dynamic problems error in GEKKO

I want to find the value of the parameter m that minimizes my variable x subject to a system of differential equations. I have the following code
from gekko import GEKKO
def run_model_m(days, population, case, k_val, b_val, u0_val, sigma_val, Kmax0, a_val, c_val):
list_x =[]
list_u =[]
list_Kmax =[]
for i in range(len(days)):
list_xi=[]
list_ui=[]
list_Ki=[]
for j in range(len(days[i])):
#try:
m = GEKKO(remote=False)
#m.time= days[i][j]
eval = np.linspace(days[i][j][0], days[i][j][-1], 100, endpoint=True)
m.time = eval
x_data= population[i][j]
variable= np.linspace(population[i][j][0], population[i][j][-1], 100, endpoint=True)
x = m.Var(value=population[i][j][0], lb=0)
sigma= m.Param(sigma_val)
d = m.Param(c_val)
k = m.Param(k_val)
b = m.Param(b_val)
r = m.Param(a_val)
step = np.ones(len(eval))
step= 0.2*step
step[0]=1
m_param = m.CV(value=1, lb=0, ub=1, integer=True); m_param.STATUS=1
u = m.Var(value=u0_val, lb=0, ub=1)
#m.free(u)
a = m.Param(a_val)
c= m.Param(c_val)
Kmax= m.Param(Kmax0)
if case == 'case0':
m.Equations([x.dt()== x*(r*(1-x/(Kmax))-m_param/(k+b*u)-d), u.dt()== sigma*(m_param*b/((k+b*u)**2))])
elif case == 'case4':
m.Equations([x.dt()== x*(r*(1-u**2)*(1-x/(Kmax))-m_param/(k+b*u)-d), u.dt() == sigma*(-2*u*r*(1-x/(Kmax))+(b*m_param)/(b*u+k)**2)])
p = np.zeros(len(eval))
p[-1] = 1.0
final = m.Param(value=p)
m.Obj(x)
m.options.IMODE = 6
m.options.MAX_ITER=15000
m.options.SOLVER=1
# optimize
m.solve(disp=False, GUI=False)
#m.open_folder(dataset_path+'inf')
list_xi.append(x.value)
list_ui.append(u.value)
list_Ki.append(m_param.value)
list_x.append(list_xi)
list_Kmax.append(list_Ki)
list_u.append(list_ui)
return list_x, list_u, list_Kmax, m.options.OBJFCNVAL
scaled_days[i][j] =[-7.0, 42.0, 83.0, 125.0, 167.0, 217.0, 258.0, 300.0, 342.0]
scaled_pop[i][j] = [0.01762491277346285, 0.020592540360308997, 0.017870838266697213, 0.01690069378982034,0.015512320147187675,0.01506701796298272,0.014096420738841563,0.013991224004743027,0.010543380664478205]
k0,b0,group, case0, u0, sigma0, K0, a0, c0 = (100, 20, 'Size3, Inc', 'case0', 0.1, 0.05, 2, 0, 0.01)
list_x2, list_u2, list_Kmax2,final =run_model_m(days=[[scaled_days[i][j]]], population=
[[scaled_pop[i][j]]],case=case1, k_val=list_b1[i0][0], b_val=b1, u0_val=list_u1[i0][j0],
sigma_val=sigma1, Kmax0=K1, a_val=list_Kmax1[0][0], c_val=c1)
I get the error Data arrays must have the same length, and match time discretization in dynamic problems error but I don't understand why. I have tried making x and m_param arrays, with x=m.Var, m_param =m.MV... But still get the same error, even if they are all arrays of the same length. Is this the right way to find the solution of the minimization problem?
I think the error was just that in run_model_m I was passing a list as u0_val and it didn't have the same dimensions as m.time. So it should be u0_val=list_u1[0][0][0]

Trying to summarize list in arcpy

I have a list of xy points that I'm trying to sum together and identify the centroid, but it only uses the last value in the row. I'm trying to create a centroid for each state, Here's the code:
Total_X1 = 0
Total_Y1 = 0
TotalPop1 = 0
#Cat = "cali"
cntyName1 = "cnty"
stateName1 = "statename"
for row in cursor:
#if row[0] >= : ### for condition that is met
#if row[0]== []:
TheStateName1 = row[0]
thecntyName1 = row[4]
idpoly1 = row[5]
idobject1 = row[6]
stateFIPS1 = row[7]
countyFIPS1 = row[8]
fips1 = row[9]
fipSnum1 = row[10]
fipsNumer1 = row[11]
#totarea = row[12]
XPoint = row [13]
YPoint = row[14]
#print Cat
print TheStateName1
print thecntyName1
print row ### do something with that value!
Total_X1 += row[2] *row[3]
print Total_X1
Total_Y1 += row[1] *row[3]
print Total_Y1
TotalPop1 += row[3]
print TotalPop1
print ""
print "X is: " , Total_X1
print "POP is: " , TotalPop1
centroid_X1 = Total_X1/TotalPop1
print "your x centroid is: ",centroid_X1
print ""
#print Cat
print thecntyName1
print TheStateName1
Any Suggestions, Thanks!
The cursor can only 'see' one row at a time, you have to pull info from that row and store it elsewhere.
loc_list = [(row[0], row[1]) for row in arcpy.da.SearchCursor(dataset, ['X_coord', 'Y_coord'])
Will give you a list of X,Y tuples from your attribute table.
After that you've got multiple options for turning that list of tuples into a spatial dataset before calculating the mean - start by reading the ESRI documentation for arcpy.Point and all the related topics linked, and go from there. If you have 10.3 or above you can use Mean Center once you have a point layer.
You'll probably get a wrong answer if you just take the mean of the X and Y without projecting first, so don't.

MATLAB data indexing issue. What is going on here?

Let I be the identity, D an orthonormal projection, and p a vector.
I realized that several of my lines of code combined to be (I-(I-D))(p) and I could just simplify it to D(p). In replacing it, I computed the new method along-side the old to double check I was computing the same thing (Earlier in my code I had a line that was D = I - D. The D you see here is that D.) I wasn't getting the same answer, and traced it to an error in indexing D.
Here you can see I'm using the debugger and checking portions of D and getting the wrong data returned.
The values in the data explorer on the right are what I'd expect them to be. Sometimes I get what I'd expect from D(:,:,k,1), and sometimes I don't, even when I make the queries right after each other.
The vectors those red arrows are pointing to should be the same. Nothing else changed or was computed between those lines, and k = 2 when the first line was run. I've closed MATLAB and restarted it and get the same issue every time. (D depends on random input, but I'm not altering the seed, so I get the same thing every first run after newly opening MATLAB. The way D is computed, I do expect D(:,:,1,1) to be the identity matrix.)
What in the world is going on? Any help is appreciated.
I have wondered if MATLAB is messing with me on purpose. Sometimes when I open it, a pop-up dialog box says I need to update my student license. I click the update button, but nothing ever happens and the dialog box never closes, so I click cancel.
Edit:
K>> whos D P
Name Size Bytes Class Attributes
D 4-D 4608 double
P 4x1x6 192 double
K>> size(D)
ans =
4 4 6 6
I've been playing around with A and B a bit, and I get the same thing. Sometimes it computes correctly and sometimes it doesn't.
K>> B=permute(P,[1,3,2])
B =
0.4155 0.27554 0.52338 0.6991 -0.11346 0.20999
0.53573 -0.83781 0.53182 -0.022364 0.60291 -0.62601
-0.49246 -0.46111 -0.39168 0.45919 0.42377 0.47074
0.54574 0.097595 0.53835 -0.54763 0.66637 0.58516
K>> A=D
A(:,:,1,1) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,2,1) =
0.99071 -0.091198 0.0020814 -0.029755
-0.091198 0.10503 0.020426 -0.292
0.0020814 0.020426 0.99953 0.0066643
-0.029755 -0.292 0.0066643 0.90473
A(:,:,3,1) =
0.46769 0.019281 -0.49725 0.036486
0.019281 0.9993 0.018011 -0.0013215
-0.49725 0.018011 0.53551 0.034083
0.036486 -0.0013215 0.034083 0.9975
A(:,:,4,1) =
0.96774 0.063488 -0.10826 0.12438
0.063488 0.87506 0.21304 -0.24477
-0.10826 0.21304 0.63673 0.41737
0.12438 -0.24477 0.41737 0.52047
A(:,:,5,1) =
0.7542 0.031217 0.42575 0.056052
0.031217 0.99604 -0.054071 -0.0071187
0.42575 -0.054071 0.26255 -0.097088
0.056052 -0.0071187 -0.097088 0.98722
A(:,:,6,1) =
0.9818 -0.10286 0.085279 0.0034902
-0.10286 0.41855 0.48208 0.01973
0.085279 0.48208 0.60031 -0.016358
0.0034902 0.01973 -0.016358 0.99933
A(:,:,1,2) =
0.99071 -0.091198 0.0020814 -0.029755
-0.091198 0.10503 0.020426 -0.292
0.0020814 0.020426 0.99953 0.0066643
-0.029755 -0.292 0.0066643 0.90473
A(:,:,2,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3,2) =
0.97125 -0.15889 -0.0080537 -0.051131
-0.15889 0.12194 -0.044507 -0.28256
-0.0080537 -0.044507 0.99774 -0.014323
-0.051131 -0.28256 -0.014323 0.90907
A(:,:,4,2) =
0.91488 -0.16388 -0.18495 0.12967
-0.16388 0.6845 -0.35607 0.24964
-0.18495 -0.35607 0.59815 0.28174
0.12967 0.24964 0.28174 0.80247
A(:,:,5,2) =
0.95461 0.16812 0.10326 0.066372
0.16812 0.37733 -0.38244 -0.24582
0.10326 -0.38244 0.76511 -0.15098
0.066372 -0.24582 -0.15098 0.90295
A(:,:,6,2) =
0.99628 0.012018 0.052874 0.027665
0.012018 0.96117 -0.17085 -0.089393
0.052874 -0.17085 0.24833 -0.39329
0.027665 -0.089393 -0.39329 0.79422
A(:,:,1,3) =
0.46769 0.019281 -0.49725 0.036486
0.019281 0.9993 0.018011 -0.0013215
-0.49725 0.018011 0.53551 0.034083
0.036486 -0.0013215 0.034083 0.9975
A(:,:,2,3) =
0.97125 -0.15889 -0.0080537 -0.051131
-0.15889 0.12194 -0.044507 -0.28256
-0.0080537 -0.044507 0.99774 -0.014323
-0.051131 -0.28256 -0.014323 0.90907
A(:,:,3,3) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,4,3) =
0.98622 0.043449 -0.066709 0.085142
0.043449 0.86297 0.21038 -0.26852
-0.066709 0.21038 0.67698 0.41227
0.085142 -0.26852 0.41227 0.47382
A(:,:,5,3) =
0.62859 0.041458 0.47558 0.074661
0.041458 0.99537 -0.053085 -0.0083339
0.47558 -0.053085 0.39105 -0.0956
0.074661 -0.0083339 -0.0956 0.98499
A(:,:,6,3) =
0.95505 -0.16608 0.12371 0.0067153
-0.16608 0.38639 0.45705 0.02481
0.12371 0.45705 0.65956 -0.01848
0.0067153 0.02481 -0.01848 0.999
A(:,:,1,4) =
0.96774 0.063488 -0.10826 0.12438
0.063488 0.87506 0.21304 -0.24477
-0.10826 0.21304 0.63673 0.41737
0.12438 -0.24477 0.41737 0.52047
A(:,:,2,4) =
0.91488 -0.16388 -0.18495 0.12967
-0.16388 0.6845 -0.35607 0.24964
-0.18495 -0.35607 0.59815 0.28174
0.12967 0.24964 0.28174 0.80247
A(:,:,3,4) =
0.98622 0.043449 -0.066709 0.085142
0.043449 0.86297 0.21038 -0.26852
-0.066709 0.21038 0.67698 0.41227
0.085142 -0.26852 0.41227 0.47382
A(:,:,4,4) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,5,4) =
0.73864 0.20112 -0.011394 0.39048
0.20112 0.84524 0.0087678 -0.30047
-0.011394 0.0087678 0.9995 0.017023
0.39048 -0.30047 0.017023 0.41662
A(:,:,6,4) =
0.87322 -0.15647 0.0029936 0.29363
-0.15647 0.80689 0.0036946 0.36238
0.0029936 0.0036946 0.99993 -0.0069332
0.29363 0.36238 -0.0069332 0.31996
A(:,:,1,5) =
0.7542 0.031217 0.42575 0.056052
0.031217 0.99604 -0.054071 -0.0071187
0.42575 -0.054071 0.26255 -0.097088
0.056052 -0.0071187 -0.097088 0.98722
A(:,:,2,5) =
0.95461 0.16812 0.10326 0.066372
0.16812 0.37733 -0.38244 -0.24582
0.10326 -0.38244 0.76511 -0.15098
0.066372 -0.24582 -0.15098 0.90295
A(:,:,3,5) =
0.62859 0.041458 0.47558 0.074661
0.041458 0.99537 -0.053085 -0.0083339
0.47558 -0.053085 0.39105 -0.0956
0.074661 -0.0083339 -0.0956 0.98499
A(:,:,4,5) =
0.73864 0.20112 -0.011394 0.39048
0.20112 0.84524 0.0087678 -0.30047
-0.011394 0.0087678 0.9995 0.017023
0.39048 -0.30047 0.017023 0.41662
A(:,:,5,5) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,6,5) =
0.93556 0.24481 -0.0093576 0.016177
0.24481 0.069855 0.035553 -0.061461
-0.0093576 0.035553 0.99864 0.0023492
0.016177 -0.061461 0.0023492 0.99594
A(:,:,1,6) =
0.9818 -0.10286 0.085279 0.0034902
-0.10286 0.41855 0.48208 0.01973
0.085279 0.48208 0.60031 -0.016358
0.0034902 0.01973 -0.016358 0.99933
A(:,:,2,6) =
0.99628 0.012018 0.052874 0.027665
0.012018 0.96117 -0.17085 -0.089393
0.052874 -0.17085 0.24833 -0.39329
0.027665 -0.089393 -0.39329 0.79422
A(:,:,3,6) =
0.95505 -0.16608 0.12371 0.0067153
-0.16608 0.38639 0.45705 0.02481
0.12371 0.45705 0.65956 -0.01848
0.0067153 0.02481 -0.01848 0.999
A(:,:,4,6) =
0.87322 -0.15647 0.0029936 0.29363
-0.15647 0.80689 0.0036946 0.36238
0.0029936 0.0036946 0.99993 -0.0069332
0.29363 0.36238 -0.0069332 0.31996
A(:,:,5,6) =
0.93556 0.24481 -0.0093576 0.016177
0.24481 0.069855 0.035553 -0.061461
-0.0093576 0.035553 0.99864 0.0023492
0.016177 -0.061461 0.0023492 0.99594
A(:,:,6,6) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Edit 2:
Added relevant code. I've been pausing the code and getting the errors inside the for loops at the end. (I believe it's also giving errors in S, but I've been focusing on D trying to figure it out.)
mtimesx is from here.
n = 4;
M = 6;
P = Normalize(2*rand(n,1,M)-1);
%differences between p_i and p_j
%sum of p_i and p_j
d = Normalize(repmat(permute(P,[1,3,2]),[1,1,M]) - repmat(P,[1,M,1]));
s = Normalize(repmat(permute(P,[1,3,2]),[1,1,M]) + repmat(P,[1,M,1]));
d(isnan(d)) = 0;
%orthogonal projection onto d(:,i,j), i.e. outer product of differences
%orthogonal projection onto s(:,i,j), i.e. outer product of sums
D = mtimesx(permute(d,[1,4,2,3]), permute(d,[4,1,2,3]));
S = mtimesx(permute(s,[1,4,2,3]), permute(s,[4,1,2,3]));
D2 = D;
S2 = S;
%projection onto the complement of d(:,i,j)
%projection onto the complement of s(:,i,j)
D = repmat(eye(n),[1,1,M,M]) - D;
S = repmat(eye(n),[1,1,M,M]) - S;
%total distance to the nearest subspace
PDist = zeros([1,M]);
PDist2 = PDist;
for j = 1:M
for k = 1:M-1
for l = k:M
if j~=k && j~=l
PDist(j) = PDist(j) + min(norm(P(:,1,j) - mtimes(D(:,:,k,l),P(:,1,j))), norm(P(:,1,j) - mtimes(S(:,:,k,l),P(:,1,j))));
PDist2(j) = PDist2(j) + min(norm(D2(:,:,k,1)*P(:,1,j)),norm(S2(:,:,k,1)*P(:,1,j)));
end
end
end
end
PDist-PDist2
Normalize.m
%Normalize
%Accepts an array (of column vectors) and normalizes the columns
function B = Normalize(A)
B = A./repmat(sqrt(sum(A.*A)),size(A,1),1);
end
The problem is that you indexed the matrices using the constant 1 instead of the variable l (lowercase L), both in the first example and in the code for computing PDist2.
In general it is good to avoid using variable names that look similar to each other and/or similar to numbers.
This can be avoided by using an editor that highlights uses different colors for variables and constants (I don't know if this is possible in MATLAB). In fact, this is how I found the error in your code. As you can see, when indexing D2 for the computation of PDist2 the number 1 is colored red.

Resources