MATLAB Answers

challenge implementing for loop

5 views (last 30 days)
Prince Igweze
Prince Igweze on 3 Dec 2019
Edited: KALYAN ACHARJYA on 3 Dec 2019
I have chanllenges implementing this code to run a loop from Tmax to Tmin and accepting or rejecting m based on if statement
theta = [0:90]';
M = [1800;400;1850;0.2;0.5]';
Dm = ref_coeff(M,theta);
Ds = Dm + (0.02*randn(size(Dm)) + 0);
T = [logspace(3,0)]';
Mint = M;
for T = max(T) : min(T)
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
if Ec(T) >= 0
fprintf('%d.\n',m)
elseif (exp(Ec(T)/T)) > randn(0,1)
fprintf('%d.\n',m)
end
end

  1 Comment

KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Dec 2019
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

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!