Thursday, 25 October 2018

Program to solve 4 variable equation(octave code)

function [x,y,z,alpha]=eq4variable(aaaa1,bbbb1,dddd1,eeee1,cccc1,aaaa2,bbbb2,dddd2,eeee2,cccc2,aaaa3,bbbb3,dddd3,eeee3,cccc3,aaaa4,bbbb4,dddd4,eeee4,cccc4)
    var1=[aaaa1,bbbb1,dddd1,cccc1]
    var2=[aaaa2,bbbb2,dddd2,cccc2]
    var3=[aaaa3,bbbb3,dddd3,cccc3]
    var3=[aaaa4,bbbb4,dddd4,cccc4]

   
      %solving equation
  
  %reassigning values to convert equation into 3 variable 
  aaa1=aaaa1*eeee2;
  bbb1=bbbb1*eeee2;
  ccc1=cccc1*eeee2;
  ddd1=dddd1*eeee2;
  eee1=eeee1*eeee2;
 
  aaa2=aaaa2*eeee1;
  bbb2=bbbb2*eeee1;
  ccc2=cccc2*eeee1;
  ddd2=dddd2*eeee1;
  eee2=eeee2*eeee1;
   %Subtracting equation1 and equation2
  PPP1=aaa1-aaa2;
  RRR1=bbb1-bbb2;
  SSS1=ddd1-ddd2;
  TTT1=eee1-eee2;
  QQQ1=ccc1-ccc2;
 
    %reassigning values to convert equation into 3 variable 
  aaa3=aaaa3*eeee4;
  bbb3=bbbb3*eeee4;
  ccc3=cccc3*eeee4;
  ddd3=dddd3*eeee4;
  eee3=eeee3*eeee4;
 
  aaa4=aaaa4*eeee3;
  bbb4=bbbb4*eeee3;
  ccc4=cccc4*eeee3;
  ddd4=dddd4*eeee3;
  eee4=eeee4*eeee3;
   %Subtracting equation3 and equation4
  PPP2=aaa3-aaa4;
  RRR2=bbb3-bbb4;
  SSS2=ddd3-ddd4;
  TTT2=eee3-eee4;
  QQQ2=ccc3-ccc4;
 
    %reassigning values to convert equation into 3 variable 
  aaa5=aaaa1*eeee4;
  bbb5=bbbb1*eeee4;
  ccc5=cccc1*eeee4;
  ddd5=dddd1*eeee4;
  eee5=eeee1*eeee4;
 
  aaat6=aaaa4*eeee1;
  bbb6=bbbb4*eeee1;
  ccc6=cccc4*eeee1;
  ddd6=dddd4*eeee1;
  eee6=eeee4*eeee1;
   %Subtracting equation1 and equation4
  PPP3=aaa1-aaa4;
  RRR3=bbb1-bbb4;
  SSS3=ddd1-ddd4;
  TTT3=eee1-eee4;
  QQQ3=ccc1-ccc4;
 
  [x,y,z]=eq3variable(PPP1,RRR1,SSS1,QQQ1,PPP2,RRR2,SSS2,QQQ2,PPP3,RRR3,SSS3,QQQ3)
 
  alpha=(cccc1-(aaaa1*x)-(bbbb1*y)-(ccc1*z))/eeee1
 
  end

For proper reading I had done some color changes:


Comments are written in green.
function and conditions are written in aqua blue.
Input arguments are highlighted by yellow color.

This code is not tested yet.....



Monday, 22 October 2018

Program to solve 3 variable equation (Octave code)


I am extremely sorry about my previous code it was not giving correct result. But this code is working correctly.



function eq3variable(aaa1,bbb1,ddd1,ccc1,aaa2,bbb2,ddd2,ccc2,aaa3,bbb3,ddd3,ccc3)
    var1=[aaa1,bbb1,ddd1,ccc1]
    var2=[aaa2,bbb2,ddd2,ccc2]
    var3=[aaa3,bbb3,ddd3,ccc3]

 
      %solving equation

  %reassigning values to convert equation into two variable = eliminating d1 and d2
  aa1=aaa1*ddd2;
  bb1=bbb1*ddd2;
  cc1=ccc1*ddd2;
  dd1=ddd1*ddd2;

  aa2=aaa2*ddd1;
  bb2=bbb2*ddd1;
  cc2=ccc2*ddd1;
  dd2=ddd2*ddd1;

  %Subtracting equation1 and equation2
  PP1=aa1-aa2;
  RR1=bb1-bb2;
  SS1=dd1-dd2;
  QQ1=cc1-cc2;

  %----------------------------------------------------  
  aa3=aaa3*ddd2;
  bb3=bbb3*ddd2;
  cc3=ccc3*ddd2;
  dd3=ddd3*ddd2;

  aa2=aaa2*ddd3;
  bb2=bbb2*ddd3;
  cc2=ccc2*ddd3;
  dd2=dd2*ddd3;

  %Subtracting equation3 and equation2
  PP2=aa3-aa2;
  RR2=bb3-bb2;
  SS2=dd3-dd2;
  QQ2=cc3-cc2;

  eq2variable(PP1,RR1,QQ1,PP2,RR2,QQ2)

function eq2variable(aa1,bb1,cc1,aa2,bb2,cc2)
  var1=[aa1,bb1,cc1]
  var2=[aa2,bb2,cc2]

  %solving equation
  %reassigning values to solve value of y
  ay1=aa1*aa2;
  by1=bb1*aa2;
  cy1=cc1*aa2;

  ay2=aa2*aa1;
  by2=bb2*aa1;
  cy2=cc2*aa1;

  %Subtracting equation1 and equation2
  P1=ay1-ay2;
  R1=by1-by2;
  Q1=cy1-cy2;

  y=Q1/R1;

  %reassigning values to solve value of x
  ax1=aa1*bb2;
  bx1=bb1*bb2;
  cx1=cc1*bb2;

  ax2=aa2*bb1;
  bx2=bb2*bb1;
  cx2=cc2*bb1;

  %Subtracting equation1 and equation2
  P2=ax1-ax2;
  R2=bx1-bx2;
  Q2=cx1-cx2;

  x=Q2/P2;

x=x
y=y


end

x=x;
y=y;

z=(-(aaa1*x)-(bbb1*y)+ccc1)/ddd1

end


For proper reading I had done some color changes:


Comments are written in green.
function and conditions are written in aqua blue.
Input arguments are highlighted by yellow color.

This code will not work if any of the input arguments is zero


Sunday, 21 October 2018

Solve 2 variable equation (Octave code)


I am extremely sorry about my previous code it was not giving correct result. But this code is working correctly.

function eq2variable(aa1,bb1,cc1,aa2,bb2,cc2)
  var1=[aa1,bb1,cc1]
  var2=[aa2,bb2,cc2]

  %solving equation
  %reassigning values to solve value of y
  ay1=aa1*aa2;
  by1=bb1*aa2;
  cy1=cc1*aa2;

  ay2=aa2*aa1;
  by2=bb2*aa1;
  cy2=cc2*aa1;

  %Subtracting equation1 and equation2
  P1=ay1-ay2;
  R1=by1-by2;
  Q1=cy1-cy2;

  y=Q1/R1;

  %reassigning values to solve value of x
  ax1=aa1*bb2;
  bx1=bb1*bb2;
  cx1=cc1*bb2;

  ax2=aa2*bb1;
  bx2=bb2*bb1;
  cx2=cc2*bb1;

  %Subtracting equation1 and equation2
  P2=ax1-ax2;
  R2=bx1-bx2;
  Q2=cx1-cx2;

  x=Q2/P2;

x=x
y=y


end


For proper reading I had done some color changes:


Comments are written in green.
function and conditions are written in aqua blue.
Input arguments are highlighted by yellow color.

This code will not work if any of the input arguments is zero

Saturday, 20 October 2018

Perform NOR operation on two decimal numbers (Octave code)

function  NOr_conversion(dec1,dec2)
i=1;
j=1;
dec_count=1;

for dec_count=1:1:2
  if dec_count==1
    dividend=dec1;
    i=i;
  end
  if dec_count==2
    dividend=dec2;
    i=j;
  end
  if dividend==0
    binary_value=0
  else
q_temp=dividend;
    while (q_temp>1)
      r_temp=mod(q_temp,2);
      binary_matrix(i,1)=q_temp;
      binary_matrix(i,2)=r_temp;

      q_temp=floor(dividend/2);
      dividend=q_temp;
      i=i+1;
    end
   binary_matrix(i,2)=1;
   a=binary_matrix(:,2);
   b=a';
   binary_value=flip(b);
   end
 
  if dec_count==1
    A_binary_value=binary_value
  end
  if dec_count==2
    B_binary_value=binary_value
  end 
end

% zero padding
  A_count=size(A_binary_value);
  A_count=A_count(1,2);
  B_count=size(B_binary_value);
  B_count=B_count(1,2);
  count=max(A_count,B_count)
  for net_count=count: -1 : 1
    if A_count == 0
      A_binary_value(1,net_count)=0;
    else
      A_binary_value(1,net_count)=A_binary_value(1,A_count);
      A_count=A_count-1;
    end
    if B_count == 0
     B_binary_value(1,net_count)=0;
    else
     B_binary_value(1,net_count)=B_binary_value(1,B_count);
     B_count=B_count-1;
    end
 
  end
    A= A_binary_value(1,:)
    B= B_binary_value(1,:)
 
% Zero padding end

% Oring the two values
    for Or_count=count:-1:1
      if A(1,Or_count)==B(1,Or_count);
        Or_result(1,Or_count)=A(1,Or_count);
      else
        Or_result(1,Or_count)=1;
      end
 
% NOR conversion 
   
      if Or_result(1,Or_count)==1
        NOr_result(1,Or_count)=0;
      end
      if Or_result(1,Or_count)==0
        NOr_result(1,Or_count)=1;
      end
    end
    Or_result=Or_result(1,:)
    NOr_result=NOr_result(1,:)
 
end

Flaws of the above code:

1. It will only work correctly if smaller number is entered as dec1 and greater number as dec2.
2. This program contains many loops so speed of evaluation will be slow.

Perform NAND Operation on two decimal numbers (Octave code)

function  NAnd_conversion(dec1,dec2)
i=1;
j=1;
dec_count=1;

for dec_count=1:1:2
  if dec_count==1
    dividend=dec1;
    i=i;
  end
  if dec_count==2
    dividend=dec2;
    i=j;
  end
  if dividend==0
    binary_value=0
  else
q_temp=dividend;
    while (q_temp>1)
      r_temp=mod(q_temp,2);
      binary_matrix(i,1)=q_temp;
      binary_matrix(i,2)=r_temp;

      q_temp=floor(dividend/2);
      dividend=q_temp;
      i=i+1;
    end
   binary_matrix(i,2)=1;
   a=binary_matrix(:,2);
   b=a';
   binary_value=flip(b);
   end
 
  if dec_count==1
    A_binary_value=binary_value
  end
  if dec_count==2
    B_binary_value=binary_value
  end 
end

% bits padding
  A_count=size(A_binary_value);
  A_count=A_count(1,2);
  B_count=size(B_binary_value);
  B_count=B_count(1,2);
  count=max(A_count,B_count)
  for net_count=count: -1 : 1
    if  A_count == 0
      A_binary_value(1,net_count)=0;
    else
      A_binary_value(1,net_count)=A_binary_value(1,A_count);
      A_count=A_count-1;
    end
    if  B_count == 0
     B_binary_value(1,net_count)=0;
    else
     B_binary_value(1,net_count)=B_binary_value(1,B_count);
     B_count=B_count-1;
    end
 
  end
    A= A_binary_value(1,:)
    B= B_binary_value(1,:)
 
% Zero padding end

% Anding the two values
  for And_count=count:-1:1
    if A(1,And_count)==B(1,And_count);
      And_result(1,And_count)=A(1,And_count);
    else
      And_result(1,And_count)=0;
    end

% Nand conversion
    if And_result(1,And_count)==0
      NAnd_result(1,And_count)=1;
    end
    if And_result(1,And_count)==1
      NAnd_result(1,And_count)=0;
    end
   
  end
    And_result=And_result(1,:)
    NAnd_result=NAnd_result(1,:)
   
end

Flaws of the above code:

1. It will only work correctly if smaller number is entered as dec1 and greater number as dec2.
2. This program contains many loops so speed of evaluation will be slow.


Perform OR operation on two decimal numbers (Octave Code)

function  Or_conversion(dec1,dec2)
i=1;
j=1;
dec_count=1;

for dec_count=1:1:2
  if dec_count==1
    dividend=dec1;
    i=i;
  end
  if dec_count==2
    dividend=dec2;
    i=j;
  end
  if dividend==0
    binary_value=0
  else
q_temp=dividend;
    while (q_temp>1)
      r_temp=mod(q_temp,2);
      binary_matrix(i,1)=q_temp;
      binary_matrix(i,2)=r_temp;

      q_temp=floor(dividend/2);
      dividend=q_temp;
      i=i+1;
    end
   binary_matrix(i,2)=1;
   a=binary_matrix(:,2);
   b=a';
   binary_value=flip(b);
   end
 
  if dec_count==1
    A_binary_value=binary_value
  end
  if dec_count==2
    B_binary_value=binary_value
  end
end

% bits padding
  A_count=size(A_binary_value);
  A_count=A_count(1,2);
  B_count=size(B_binary_value);
  B_count=B_count(1,2);
  count=max(A_count,B_count)
  for net_count=count: -1 : 1
    if A_count == 0
      A_binary_value(1,net_count)=0;
    else
      A_binary_value(1,net_count)=A_binary_value(1,A_count);
      A_count=A_count-1;
    end
    if  B_count == 0
     B_binary_value(1,net_count)=0;
    else
     B_binary_value(1,net_count)=B_binary_value(1,B_count);
     B_count=B_count-1;
    end
 
  end
    A= A_binary_value(1,:)
    B= B_binary_value(1,:)
 
% Zero padding end

% Oring the two values
  for Or_count=count:-1:1
    if A(1,Or_count)==B(1,Or_count);
      Or_result(1,Or_count)=A(1,Or_count);
    else
      Or_result(1,Or_count)=1;
    end
  end
    Or_result=Or_result(1,:)
   
end

Flaws of the above code:

1. It will only work correctly if smaller number is entered as dec1 and greater number as dec2.
2. This program contains many loops so speed of evaluation will be slow.

Performing AND operation on two decimal numbers (Octave code)

function  And_conversion(dec1,dec2)
i=1;
j=1;
dec_count=1;

for dec_count=1:1:2
  if dec_count==1
    dividend=dec1;
    i=i;
  end
  if dec_count==2
    dividend=dec2;
    i=j;
  end
  if dividend==0
    binary_value=0
  else
q_temp=dividend;
    while (q_temp>1)
      r_temp=mod(q_temp,2);
      binary_matrix(i,1)=q_temp;
      binary_matrix(i,2)=r_temp;
   
      q_temp=floor(dividend/2);
      dividend=q_temp;
      i=i+1;
    end
   binary_matrix(i,2)=1;
   a=binary_matrix(:,2);
   b=a';
   binary_value=flip(b);
   end
 
  if dec_count==1
    A_binary_value=binary_value
  end
  if dec_count==2
    B_binary_value=binary_value
  end
end

% bits padding
  A_count=size(A_binary_value);
  A_count=A_count(1,2);
  B_count=size(B_binary_value);
  B_count=B_count(1,2);
  count=max(A_count,B_count)
  for net_count=count: -1 : 1
    if A_count == 0
      A_binary_value(1,net_count)=0;
    else
      A_binary_value(1,net_count)=A_binary_value(1,A_count);
      A_count=A_count-1;
    end
    if  B_count == 0
     B_binary_value(1,net_count)=0;
    else
     B_binary_value(1,net_count)=B_binary_value(1,B_count);
     B_count=B_count-1;
    end
 
  end
    A= A_binary_value(1,:)
    B= B_binary_value(1,:)
 
% Zero padding end

% Anding the two values
  for And_count=count:-1:1
    if A(1,And_count)==B(1,And_count);
      And_result(1,And_count)=A(1,And_count);
    else
      And_result(1,And_count)=0;
    end
  end
    And_result=And_result(1,:)
   
end

Flaws of the above code:

1. It will only work correctly if smaller number is entered as dec1 and greater number as dec2.
2. This program contains many loops so speed of evaluation will be slow.

Tuesday, 16 October 2018

Octave code for decimal to binary conversion

function result=decimal2binary(dividend)
i=1;
j=1;
 if dividend==0
    binary_value=0
  else
q_temp=dividend;
  while (q_temp>1)
  r_temp=mod(q_temp,2);
  binary_matrix(i,1)=q_temp;
  binary_matrix(i,2)=r_temp;
  display(binary_matrix);
  q_temp=floor(dividend/2);
  dividend=q_temp;
  i=i+1;
  end
  binary_matrix(i,2)=1;
  a=binary_matrix(:,2);
  b=a';
result=flip(b)
  end

end

This code is based on simple decimal to binary conversion .

Our number gets divide by 2. We get the result in two forms Quotient and Remainder. Remainder become the LSB and Quotient becomes dividend and again get divided by 2. Until Quotient become l. This condition is given in a while loop:

To store the values of Quotients and Remainders, I created a matrix binary_matrix with i rows and 2 columns. First column contains Quotient and Second column contains Remainder.

In this way we got all the bits. But I require column 2 only. So I extracted it. This will result into a matrix of (i*1) i rows and 1 column. I taken the transpose of it. Then arranged it in reverse order to get the result.  

Flaws of above program:
  1.  As this program contain loop it will be time consuming.
  2. This program will not convert float values to binary.


Program to solve 4 variable equation(octave code)

function [x,y,z,alpha]=eq4variable( aaaa1,bbbb1,dddd1,eeee1,cccc1,aaaa2,bbbb2,dddd2,eeee2,cccc2,aaaa3,bbbb3,dddd3,eeee3,cccc3,aaaa4,bbbb4,d...