Er=(abs(Ds - Dm))./(0.02);
E = sum(Er);
n1 = 25 .* randn(1,1) + 1800
m1 = 1500 + n1*(2000 - 1500)
n2 = 50 .* randn(1,1) + 400
m2 = 0 + n2*(1000 - 0)
n3 = 50 .* randn(1,1) + 1850
m3 = 1200 + n3*(2200 - 1200)
n4 = 0.05 .* randn(1,1) + 0.2
m4 = 0 + n4*(1 - 0)
n5 = 0.05 .* randn(1,1) + 0.5
m5 = 0 + n5*(1 - 0)
m = [m1;m2;m3;m4;m5]'
dm = ref_coeff(m,theta)
ds = dm + (0.02*randn(size(dm)) + 0)
En1 = (abs(ds - dm))./(0.02)
En = sum(En1)
Ec = En - E
These expressions are independent with T, still why you are keeping those assignment within for loop with the variation of T?
You can use the proper indexing, likewise
T=sort(T);
Mint=M;
for i=1:length(T)
if T(i)...
.........
end
end