Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Eli Simhayev - 2017
- # Question 3
- n = 5;
- maxIter = 50;
- # ===== Task C =====
- # calculate the coefficient of xᵢ - g.
- function calc_g(i,H,g,x)
- sum = 0;
- for j=1:n
- if j != i
- sum += H[i,j]*x[j];
- end
- end
- return sum - g[i];
- end
- function projected_CD(H,g,a,b,x_CD)
- for i=1:n
- g_coe = -calc_g(i,H,g,x_CD); # g coefficient
- h_coe = H[i,i]; # h coefficient
- g_div_h = g_coe/h_coe;
- if g_div_h < a[i]
- x_CD[i] = a[i];
- elseif g_div_h > b[i]
- x_CD[i] = b[i];
- else
- x_CD[i] = g_div_h;
- end
- end
- return x_CD;
- end
- # ===== Task D =====
- H = [5 -1 -1 -1 -1;
- -1 5 -1 -1 -1;
- -1 -1 5 -1 -1;
- -1 -1 -1 5 -1;
- -1 -1 -1 -1 5];
- g = [18; 6; -12; -6; 18];
- a = zeros(n); b = 5*ones(n);
- x_CD = zeros(n); # guess
- for i=1:maxIter
- x_CD = projected_CD(H,g,a,b,x_CD);
- end
- println("x_CD = ", x_CD); # x_CD = [5.0,3.66667,0.666667,1.66667,5.0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement