Advertisement
MOOOSE21

Untitled

May 7th, 2025
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.20 KB | None | 0 0
  1. C= [11, 13, 17, 14; 16, 18, 14, 10; 21, 24, 13, 10];
  2. S= [8 5 9];
  3. D= [8 7 5 4];
  4. [m ,n]= size(C);
  5.  
  6. if(sum(S)==sum(D))
  7.     disp('Balanced');
  8. else if(sum(S)<sum(D))
  9.         C(end+1,:)=zeros(1,n);
  10.         S(end+1)= sum(D)- sum(S);
  11.         s=sum(D);
  12.  
  13. else
  14.      C(:,end+1)=zeros(m,1);
  15.      D(end+1)= sum(S)- sum(D);
  16.      s=sum(C);
  17. end
  18. end
  19. table =C;
  20. table= [table , S(:)];
  21. mD = [D, s];  
  22. table= [table ; mD];
  23. var ={'D1','D2','D3', 'D4', 'Supply' };
  24. disp(array2table(table, 'VariableNames', var));
  25. X=C;
  26. Ci=C;
  27. while any(S~=0)|| any(D~=0)
  28.     mini = min(C(:));
  29.     [r, c]= find(C==mini);
  30.     y = min(S(r), D(c));
  31.     [alloc, ind]= max(y);
  32.     rr= r(ind);
  33.     cc= c(ind);
  34.     X(rr, cc)= alloc;
  35.     S(rr)= S(rr)- alloc;
  36.     D(cc)= D(cc)-alloc;
  37.     C(rr, cc)= inf;
  38.  
  39.  
  40. end
  41. table =X;
  42. table= [table , S(:)];
  43. mD = [D, s];  
  44. table= [table ; mD];
  45. var ={'D1','D2','D3', 'D4', 'Supply' };
  46. disp(array2table(table, 'VariableNames', var));
  47.  
  48. Cost = Ci.*X;
  49. [m, n]= size(X);
  50. if nnz(X)==m+n-1
  51.     disp('Non-degenerate initial BFS');
  52. else
  53.     disp('degenerate initial BFS');
  54. end
  55. final = sum(Cost(:));
  56. disp("Total Cost is ");
  57. disp(final);
  58. //
  59. C = [-3 2 0 0];
  60. A = [1 1 1 0; 2 -1 0 1];
  61. b = [6; 9];
  62. m=size(A,1);
  63. n=size(A,2);
  64. bv_index=n-m+1:n;
  65. Y=[A,b];
  66. xb=Y(:,end);
  67. cb=C(bv_index);
  68. zicj= cb*Y(:,1:n)-C;
  69. z=cb*xb;
  70. table=[zicj,z;Y];
  71. disp(table)
  72. while any(zicj<0)
  73.     [~,ev]=min(zicj);
  74.     if all(Y(:,ev)<=0)
  75.         disp("unbounded")
  76.         return;
  77.     end
  78.     ratio=inf(1,m);
  79.     for j=1:m
  80.         if (Y(j,ev)>0)
  81.             ratio(j)=xb(j)/Y(j,ev);
  82.         end
  83.     end
  84.     [~,lv]=min(ratio);
  85.     bv_index(lv)=ev;
  86.     pivot=Y(lv,ev);
  87.     Y(lv,:)=Y(lv,:)/pivot;
  88.     for i=1:m
  89.         if i~=lv
  90.             Y(i,:)=Y(i,:)-Y(i,ev)*Y(lv,:);
  91.         end
  92.     end
  93.     xb=Y(:,end);
  94.     cb=C(bv_index);
  95.     zicj=cb*Y(:,1:n)-C;
  96.     z=cb*xb
  97.     table=[zicj,z;Y];
  98.     disp(table)
  99. end
  100. \\
  101. clc;
  102. clear;
  103.  
  104. % Objective function (Maximize Z = -3x + 2y)
  105. C = [-3 2 0 0 -1e5 -1e5];   % Add Big M penalties (negative for maximization)
  106.  
  107. % Constraints (converted for simplex):
  108. % x + y - s1 + a1 = 6
  109. % 2x - y - s2 + a2 = 9
  110.  
  111. A = [1  1 -1  0 1 0;
  112.      2 -1  0 -1 0 1];
  113. b = [6; 9];
  114.  
  115. [m, n] = size(A);
  116.  
  117. % Initial basic variables (artificial variables are basic initially)
  118. bv_index = [5 6];
  119.  
  120. % Form initial tableau
  121. Y = [A b];
  122.  
  123. % Initial values
  124. xb = Y(:, end);
  125. cb = C(bv_index);
  126.  
  127. % Compute initial Zj - Cj
  128. zicj = cb * Y(:, 1:n) - C;
  129. z = cb * xb;
  130. table = [zicj z; Y];
  131. disp('Initial Tableau:');
  132. disp(table);
  133.  
  134. % Simplex loop
  135. while any(zicj < 0)
  136.     [~, ev] = min(zicj);  % Entering variable
  137.  
  138.     if all(Y(:, ev) <= 0)
  139.         disp("Unbounded solution");
  140.         return;
  141.     end
  142.  
  143.     ratio = inf(m, 1);
  144.     for j = 1:m
  145.         if Y(j, ev) > 0
  146.             ratio(j) = xb(j) / Y(j, ev);
  147.         end
  148.     end
  149.  
  150.     [~, lv] = min(ratio); % Leaving variable
  151.     bv_index(lv) = ev;
  152.  
  153.     % Pivot operation
  154.     pivot = Y(lv, ev);
  155.     Y(lv, :) = Y(lv, :) / pivot;
  156.  
  157.     for i = 1:m
  158.         if i ~= lv
  159.             Y(i, :) = Y(i, :) - Y(i, ev) * Y(lv, :);
  160.         end
  161.     end
  162.  
  163.     % Update tableau
  164.     xb = Y(:, end);
  165.     cb = C(bv_index);
  166.     zicj = cb * Y(:, 1:n) - C;
  167.     z = cb * xb;
  168.  
  169.     table = [zicj z; Y];
  170.     disp('Next Tableau:');
  171.     disp(table);
  172. end
  173.  
  174. % Final Result (no sign change needed for maximization)
  175. fprintf('\nOptimal value (Max Z): %.2f\n', z);
  176. for i = 1:n
  177.     val = 0;
  178.     if any(bv_index == i)
  179.         row = find(bv_index == i);
  180.         val = Y(row, end);
  181.     end
  182.     fprintf('Variable x%d = %.2f\n', i, val);
  183. end
  184. //
  185. C = [-2 -1 0 0 0];
  186. A = [-3 -1 1 0 0;
  187.      -4 -3 0 1 0;
  188.      -1 -2 0 0 1];
  189. b = [-3; -6; -3];
  190.  
  191. [m, n] = size(A);
  192. bv_index = n - m + 1:n;
  193.  
  194. Y = [A b];
  195. xb = Y(:, end);
  196. cb = C(bv_index);
  197.  
  198. while true
  199.     zicj = cb * Y(:, 1:n) - C;
  200.     z = cb * xb;
  201.     table = [zicj, z; Y];
  202.     disp("initial table")
  203. disp(table);
  204.  
  205.     if all(xb >= 0)
  206.         disp('Feasible Solution')
  207.         basic_var = bv_index;
  208.         xb
  209.         disp('Optimal Solution')
  210.         z
  211.         break
  212.     else
  213.         [~, lv] = min(xb);
  214.         if all(Y(lv, 1:n) >= 0)
  215.             disp('No feasible Solution');
  216.             break;
  217.         else
  218.             ratio = inf(1, n);
  219.             for j = 1:n
  220.                 if Y(lv, j) < 0
  221.                     ratio(j) = abs(zicj(j) / Y(lv, j));
  222.                 end
  223.             end
  224.             [~, ev] = min(ratio);
  225.             bv_index(lv) = ev;
  226.             pivot = Y(lv, ev);
  227.             Y(lv, :) = Y(lv, :) / pivot;
  228.             for i = 1:m
  229.                 if i ~= lv
  230.                     Y(i, :) = Y(i, :) - Y(i, ev) * Y(lv, :);
  231.                 end
  232.             end
  233.             xb = Y(:, end);
  234.             cb = C(bv_index);
  235.        
  236.         end
  237.     end
  238. end
  239.      disp(table);
  240. //
  241. clear all;
  242. clc;
  243. f= @(x1, x2)x1^2 -x1*x2 + x2^2;
  244. grad= @(x1, x2)[2*x1- x2, 2*x2- x1];
  245. x0=[1 , 0.5];
  246. tol= 10^-5;
  247. l= 0.01;
  248. t= 100;
  249. i=0;
  250. x1=[0, 0];
  251. while norm(t)>tol
  252.     g= -1* grad(x0(1), x0(2));
  253.     x1=  x0  + l*g;
  254.     t= x1-x0;
  255.     x0=x1;
  256.     i= i+1;
  257.    
  258. end
  259. M= f(x0(1), x0(2));
  260. disp('point is');
  261. disp(x0);
  262. disp("minimum value at optimal point is :");
  263. fprintf('%0.6f',M);
  264. disp('no. of iteration are ');
  265. disp(i);
  266. //
  267.  
  268.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement