Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C= [11, 13, 17, 14; 16, 18, 14, 10; 21, 24, 13, 10];
- S= [8 5 9];
- D= [8 7 5 4];
- [m ,n]= size(C);
- if(sum(S)==sum(D))
- disp('Balanced');
- else if(sum(S)<sum(D))
- C(end+1,:)=zeros(1,n);
- S(end+1)= sum(D)- sum(S);
- s=sum(D);
- else
- C(:,end+1)=zeros(m,1);
- D(end+1)= sum(S)- sum(D);
- s=sum(C);
- end
- end
- table =C;
- table= [table , S(:)];
- mD = [D, s];
- table= [table ; mD];
- var ={'D1','D2','D3', 'D4', 'Supply' };
- disp(array2table(table, 'VariableNames', var));
- X=C;
- Ci=C;
- while any(S~=0)|| any(D~=0)
- mini = min(C(:));
- [r, c]= find(C==mini);
- y = min(S(r), D(c));
- [alloc, ind]= max(y);
- rr= r(ind);
- cc= c(ind);
- X(rr, cc)= alloc;
- S(rr)= S(rr)- alloc;
- D(cc)= D(cc)-alloc;
- C(rr, cc)= inf;
- end
- table =X;
- table= [table , S(:)];
- mD = [D, s];
- table= [table ; mD];
- var ={'D1','D2','D3', 'D4', 'Supply' };
- disp(array2table(table, 'VariableNames', var));
- Cost = Ci.*X;
- [m, n]= size(X);
- if nnz(X)==m+n-1
- disp('Non-degenerate initial BFS');
- else
- disp('degenerate initial BFS');
- end
- final = sum(Cost(:));
- disp("Total Cost is ");
- disp(final);
- //
- C = [-3 2 0 0];
- A = [1 1 1 0; 2 -1 0 1];
- b = [6; 9];
- m=size(A,1);
- n=size(A,2);
- bv_index=n-m+1:n;
- Y=[A,b];
- xb=Y(:,end);
- cb=C(bv_index);
- zicj= cb*Y(:,1:n)-C;
- z=cb*xb;
- table=[zicj,z;Y];
- disp(table)
- while any(zicj<0)
- [~,ev]=min(zicj);
- if all(Y(:,ev)<=0)
- disp("unbounded")
- return;
- end
- ratio=inf(1,m);
- for j=1:m
- if (Y(j,ev)>0)
- ratio(j)=xb(j)/Y(j,ev);
- end
- end
- [~,lv]=min(ratio);
- bv_index(lv)=ev;
- pivot=Y(lv,ev);
- Y(lv,:)=Y(lv,:)/pivot;
- for i=1:m
- if i~=lv
- Y(i,:)=Y(i,:)-Y(i,ev)*Y(lv,:);
- end
- end
- xb=Y(:,end);
- cb=C(bv_index);
- zicj=cb*Y(:,1:n)-C;
- z=cb*xb
- table=[zicj,z;Y];
- disp(table)
- end
- \\
- clc;
- clear;
- % Objective function (Maximize Z = -3x + 2y)
- C = [-3 2 0 0 -1e5 -1e5]; % Add Big M penalties (negative for maximization)
- % Constraints (converted for simplex):
- % x + y - s1 + a1 = 6
- % 2x - y - s2 + a2 = 9
- A = [1 1 -1 0 1 0;
- 2 -1 0 -1 0 1];
- b = [6; 9];
- [m, n] = size(A);
- % Initial basic variables (artificial variables are basic initially)
- bv_index = [5 6];
- % Form initial tableau
- Y = [A b];
- % Initial values
- xb = Y(:, end);
- cb = C(bv_index);
- % Compute initial Zj - Cj
- zicj = cb * Y(:, 1:n) - C;
- z = cb * xb;
- table = [zicj z; Y];
- disp('Initial Tableau:');
- disp(table);
- % Simplex loop
- while any(zicj < 0)
- [~, ev] = min(zicj); % Entering variable
- if all(Y(:, ev) <= 0)
- disp("Unbounded solution");
- return;
- end
- ratio = inf(m, 1);
- for j = 1:m
- if Y(j, ev) > 0
- ratio(j) = xb(j) / Y(j, ev);
- end
- end
- [~, lv] = min(ratio); % Leaving variable
- bv_index(lv) = ev;
- % Pivot operation
- pivot = Y(lv, ev);
- Y(lv, :) = Y(lv, :) / pivot;
- for i = 1:m
- if i ~= lv
- Y(i, :) = Y(i, :) - Y(i, ev) * Y(lv, :);
- end
- end
- % Update tableau
- xb = Y(:, end);
- cb = C(bv_index);
- zicj = cb * Y(:, 1:n) - C;
- z = cb * xb;
- table = [zicj z; Y];
- disp('Next Tableau:');
- disp(table);
- end
- % Final Result (no sign change needed for maximization)
- fprintf('\nOptimal value (Max Z): %.2f\n', z);
- for i = 1:n
- val = 0;
- if any(bv_index == i)
- row = find(bv_index == i);
- val = Y(row, end);
- end
- fprintf('Variable x%d = %.2f\n', i, val);
- end
- //
- C = [-2 -1 0 0 0];
- A = [-3 -1 1 0 0;
- -4 -3 0 1 0;
- -1 -2 0 0 1];
- b = [-3; -6; -3];
- [m, n] = size(A);
- bv_index = n - m + 1:n;
- Y = [A b];
- xb = Y(:, end);
- cb = C(bv_index);
- while true
- zicj = cb * Y(:, 1:n) - C;
- z = cb * xb;
- table = [zicj, z; Y];
- disp("initial table")
- disp(table);
- if all(xb >= 0)
- disp('Feasible Solution')
- basic_var = bv_index;
- xb
- disp('Optimal Solution')
- z
- break
- else
- [~, lv] = min(xb);
- if all(Y(lv, 1:n) >= 0)
- disp('No feasible Solution');
- break;
- else
- ratio = inf(1, n);
- for j = 1:n
- if Y(lv, j) < 0
- ratio(j) = abs(zicj(j) / Y(lv, j));
- end
- end
- [~, ev] = min(ratio);
- bv_index(lv) = ev;
- pivot = Y(lv, ev);
- Y(lv, :) = Y(lv, :) / pivot;
- for i = 1:m
- if i ~= lv
- Y(i, :) = Y(i, :) - Y(i, ev) * Y(lv, :);
- end
- end
- xb = Y(:, end);
- cb = C(bv_index);
- end
- end
- end
- disp(table);
- //
- clear all;
- clc;
- f= @(x1, x2)x1^2 -x1*x2 + x2^2;
- grad= @(x1, x2)[2*x1- x2, 2*x2- x1];
- x0=[1 , 0.5];
- tol= 10^-5;
- l= 0.01;
- t= 100;
- i=0;
- x1=[0, 0];
- while norm(t)>tol
- g= -1* grad(x0(1), x0(2));
- x1= x0 + l*g;
- t= x1-x0;
- x0=x1;
- i= i+1;
- end
- M= f(x0(1), x0(2));
- disp('point is');
- disp(x0);
- disp("minimum value at optimal point is :");
- fprintf('%0.6f',M);
- disp('no. of iteration are ');
- disp(i);
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement