cl1p.net - The internet clipboard
Login/Sign Up
cl1p.net/cpm
cl1p.net/cpm
Login/Sign Up
This cl1p will be deleted in in 2 days.
Copy
clc clear all format short %% phase 1: input parameter C=[20 10]; A=[1 2 ; 3 1 ; 4 3]; b=[40;30;60]; %% phase 2: plotting the graph x=0:.01:max(b); x12=(b(1)-A(1,1).*x)./A(1,2); x22=(b(2)-A(2,1).*x)./A(2,2); x32=(b(3)-A(3,1).*x)./A(3,2); x12=max(0,x12); x22=max(0,x22); x32=max(0,x32); plot(x, x12, 'r', x, x22, 'k', x, x32, 'b') xlabel('values of x1') ylabel('values of x2') title('x1 vs x2') legend('x1+2x2=2000','x1+x2=1500','x2=600') grid on %% phase 3: find corner points with axes c=find(x==0) %%points with x1 axis this line is fix c1=find(x12==0) %%points with x2 axis for first line Line1=[x([c1 c]); x12([c1 c])]'; c2=find(x22==0); %%points with x2 axis for second line Line2=[x([c2 c]); x22([c2 c])]'; c3=find(x32==0); %%points with x2 axis for third line Line3=[x([c3 c]); x32([c3 c])]'; corpt=unique([Line1; Line2; Line3], 'rows'); % unique for no repetation of points %% phase 4: points of intersection pt=[0; 0]; for i=1:size(A,1) % all constraint=3 so we can write size(A,1) for j=i+1:size(A,1) A1=A([i j],:); B1=b([i j]); x=inv(A1)*B1; pt=[pt x]; end end ptt=pt'; %% phase 5: all corner points allpt=[ptt; corpt]; points=unique(allpt, 'rows'); %% phase 6: Feasible region for i=1:size(points,1) const1 (i)=A(1,1)*points(i,1)+A(1,2)*points(i,2)-b(1); const2 (i)=A(2,1)*points(i,1)+A(2,2)*points(i,2)-b(2); const3 (i)=A(3,1)*points(i,1)+A(3,2)*points(i,2)-b(3); s1=find(const1>0); s2=find(const2<0); s3=find(const3<0); end S=unique([s1 s2 s3]); points(S, :)=[]; %% phase 7: objective value and points value=points*C'; table=[points value]; [obj, index]=min(value); X1=points(index,1); X2=points(index,2); fprintf('objective value is %f at (%f,%f)',obj,X1,X2); clc clear all A=[12 3 -11 4; 1 2 6 -7]; % write in standard form C=[2 3 5 7]; b=[7; 3]; n=size(A,2); m=size(A,1); if n>m nCm=nchoosek(n,m); % Total no. of Basic solution p=nchoosek(1:n,m);% Pairs of basic solution sol=[]; % Default solution is zero for i=1:nCm y=zeros(n,1); A1=A(:,p(i,:)); X=inv(A1)*b; % Feasibility condition if all(X>=0 & X~=inf & X~=-inf) y(p(i,:))=X; sol=[sol y] end end else error('No of variables are less than constraints'); end % Objective function Z=C*sol; % Find the optimal value [obj,index]=max(Z); bfs=sol(:,index); % Print the solution optval=[bfs' obj]; optval %array2table(optval,'VariableNames',{'x1','x2','x3','x4','z'})