0

I have a problem with my MATLAB code that solves linear equations with two phase simplex method. In some examples it's not working and I can't find what the problem is.

Working Example and not Working Examples are in the picture below :

enter image description here


Code is below

 clear
    clc
    close all

        %%
        %these are working examples
        %%
        %Number1
        %a=[-1 3 1 0 0 0 0;1 1 0 1 0 0 0;1 -1 0 0 1 0 0;1 3 0 0 0 -1 1]; 
        %f=[-1 -2 0 0 0 0 0];
        %f1=[-1 -3 0 0 0 1 0];
        %b=[10;6;2;6;0;-6];
        %Number 2
        %a=[5 8 1 0 0;5 2 0 -1 1]; 
        %f=[-1 2 0 0 0];
        %f1=[-5 -2 0 1 0];
        %b=[50;20;-20;0];
        %%
        %This is the one that don't give me any answer
        %%
        %a=[-1 1 1 -1 1 0 0;3 1 -1 0 0 -1 1]; 
        %f=[10 6 2 0 0 0 0];
        %f1=[-3 -1 1 0 0 1 0];
        %b=[1;2;0;-2];
        %%
        %Code

        c=[[a;f;f1],b];
        [e,d]=size(c);
          while f1(end)==0
                g=find(min(f)==f);
                g=g(1,1);
                h=c(:,g);
                for i=1:length(h)
                    if h(i)>0
                        l(i)=b(i)./h(i);
                    else
                        l(i)=inf;
                    end
                end
                    l(end)=inf;
                    l(end-1)=inf;
                    m=min(l);
                    n=find(l==m);
                    n=n(1,1);
                    c(n,:)=c(n,:)./c(n,g);
                     for j=1:e
                if j~=n
                    c(j,:)=c(j,:)-c(n,:).*c(j,g);
                end
                     end
                a=c(1:end-2,1:end-1);b=c(:,end);f=c(end-1,1:end-1);f1=c(end,1:end-1);

          end

          c(end,:)=[];

        while min(f)<0
            [p,q]=size(c);
            g1=find(min(f)==f);
            g1=g1(1,1);
            h1=c(:,g1);
            for i=1:length(h1)
                if h1(i)>0
                    l1(i)=b(i)./h1(i);
                else
                    l1(i)=inf;
                end

            end
            l1(end)=inf;
            m1=min(l1);
            n1=find(l1==m1);
            n1=n1(1,1);
            c(n1,:)=c(n1,:)./c(n1,g1);
            for j=1:p
                if j~=n1
                    c(j,:)=c(j,:)-c(n1,:).*c(j,g1);
                end
            end
            a=c(1:end-1,1:end-1);b=c(:,end);f=c(end,1:end-1);
        end
        Minf=-c(end,end)

If you try to solve these you will see that code doesn't have an answer and I want to know what exactly must change in order to work.

Adam
  • 2,726
  • 1
  • 9
  • 22
Ali Eslami
  • 25
  • 9
  • It's not really surprising that your code failed on the second equation since `F(x1,x2)` is minimum when `x1 = infinity` and `x2 = infinity`... – obchardon Jun 05 '19 at 16:33
  • If you have the optimization toolbox you can also use the `fmincon` function. I've tested, it work well for the first and the third function. – obchardon Jun 05 '19 at 16:36
  • Thank you but right now i have problems with the whole method and really confused can you tell me what part of the code should i change ?it took me a really really long time to write this and i'm a newbie in MATLAB so i don't have a clue about what exactly should i change mostly because i didn't learn the algorithm properly – Ali Eslami Jun 05 '19 at 18:33
  • There is no comment in your code so I don't know what you're trying to do. I would recommend to understand an algorithm before the implementation. – obchardon Jun 06 '19 at 07:36

0 Answers0