Commit 8e1a6ac2 authored by ABHISHEK KUMAR's avatar ABHISHEK KUMAR

Lab1

parent 5fef7573
%Outlab 1 : Task B2 - solving the lights out! puzzle for any matrix
function x = backward_solve(b)
k = size(b); %size(b) returns the number of rows and columns
if(k(1) < k(2))
n = k(2);
else
n = k(1);
endif
%creating toggle matrix for the nxn matrix (row number or column number - whichever is bigger)
A = zeros(n*n,n*n);
%part of the toggle matrix for cells not in the corners or the sides
for i=2:n-1
for j=2:n-1
A(fij(i,j,n),fij(i,j,n)) = 1; %f(i,j,n) returns the order assigned to a cell (i,j) in the board
A(fij(i,j,n),fij(i-1,j,n)) = 1;
A(fij(i,j,n),fij(i+1,j,n)) = 1;
A(fij(i,j,n),fij(i,j+1,n)) = 1;
A(fij(i,j,n),fij(i,j-1,n)) = 1;
endfor
endfor
%part of the toggle matrix for cells on the sides
for j=2:n-1
A(fij(1,j,n),fij(1,j-1,n)) = 1;
A(fij(1,j,n),fij(1,j+1,n)) = 1;
A(fij(1,j,n),fij(1,j,n)) = 1;
A(fij(1,j,n),fij(2,j,n)) = 1;
endfor
for j=2:n-1
A(fij(n,j,n),fij(n,j-1,n)) = 1;
A(fij(n,j,n),fij(n,j+1,n)) = 1;
A(fij(n,j,n),fij(n,j,n)) = 1;
A(fij(n,j,n),fij(n-1,j,n)) = 1;
endfor
for i=2:n-1
A(fij(i,1,n),fij(i+1,1,n)) = 1;
A(fij(i,1,n),fij(i-1,1,n)) = 1;
A(fij(i,1,n),fij(i,1,n)) = 1;
A(fij(i,1,n),fij(i,2,n)) = 1;
endfor
for i=2:n-1
A(fij(i,n,n),fij(i+1,n,n)) = 1;
A(fij(i,n,n),fij(i-1,n,n)) = 1;
A(fij(i,n,n),fij(i,n,n)) = 1;
A(fij(i,n,n),fij(i,n-1,n)) = 1;
endfor
%part of the toggle matrix for cells in the corners
A(fij(1,1,n),fij(1,1,n)) = 1;
A(fij(1,1,n),fij(2,1,n)) = 1;
A(fij(1,1,n),fij(1,2,n)) = 1;
A(fij(1,n,n),fij(1,n,n)) = 1;
A(fij(1,n,n),fij(1,n-1,n)) = 1;
A(fij(1,n,n),fij(2,n,n)) = 1;
A(fij(n,1,n),fij(n,1,n)) = 1;
A(fij(n,1,n),fij(n-1,1,n)) = 1;
A(fij(n,1,n),fij(n,2,n)) = 1;
A(fij(n,n,n),fij(n,n,n)) = 1;
A(fij(n,n,n),fij(n-1,n,n)) = 1;
A(fij(n,n,n),fij(n,n-1,n)) = 1;
%extracting that part of the toggle matrix relevant to the mxn matrix, for nxn matrix no change
A = A(1:k(1)*k(2),1:k(1)*k(2));
%reshaping b to a column matrix
b = b';
b = reshape(b,k(1)*k(2),1);
%solving Ax + b = 0 mod 2
x = inv(A)*b*det(A); %determinant is multiplied as matrix inverse in mod 2 or GF(2) (Galois field)
%is normal inverse*determinant
x = reshape(x,k(2),k(1));
x = int64(x); %to solve precision problems - changing the data type
x = mod(x',2);
end
\ No newline at end of file
%Outlab 1 : Task B1 - solving lights out! puzzle mathematically for 3x3 matrix
function x = backward_solve_3(b)
%toggle matrix for 3x3 board
A = [1 1 0 1 0 0 0 0 0; 1 1 1 0 1 0 0 0 0; 0 1 1 0 0 1 0 0 0; 1 0 0 1 1 0 1 0 0; 0 1 0 1 1 1 0 1 0; 0 0 1 0 1 1 0 0 1; 0 0 0 1 0 0 1 1 0; 0 0 0 0 1 0 1 1 1; 0 0 0 0 0 1 0 1 1];
%reshaping b to a column matrix
b = b';
b = reshape(b,9,1);
%solving Ax + b = 0 in mod 2
x = inv(A)*b*det(A); %determinant is multiplied as matrix inverse in mod 2 or GF(2) (Galois field)
%is normal inverse*determinant
x = reshape(x,3,3);
x = int64(x); %so as to solve precision problems - changing the data type
x = mod(x',2);
end
\ No newline at end of file
%function to solve the physical equations involves in the situation
function y = f(theta)
a = load("input_outlab_task_A1.txt"); %Taking input from file
x1 = a(1);
y1 = a(2);
d = a(3); %Initialising each physical variable
w = a(4);
n = a(5);
y=zeros(2,1);
y(2,1)=(y1-cot(theta(1))*x1)*cos(theta(2))-(w*sin(theta(1)-theta(2)))/abs(sin(theta(1))); %Pair of non-linear equations,distance of a point from a line
y(2,2)=sin(theta(1))-n*sin(theta(2)); %Snell's Law
endfunction
\ No newline at end of file
function y = f2(theta)
a = load("input_outlab_task_A2.txt"); %Taking inpuut from the file
x0 = a(1);
y0 = a(2);
x1 = a(3);
y1 = a(4); %Initialising each physical variable
v2 = a(5);
vl = a(6);
d = a(7);
w = a(8);
n = a(9);
t1 = a(10);
%theta(1) = angle to be found,theta(2) = refraction angle,theta(3) = time to reach destination after t1,theta(4) = xf,theta(5) = yf
y = zeros(5,1); %System of non-linear physical equations
y(1)=sin(theta(1))-n*sin(theta(2)); %Snell's Law
y(2)=x1-tan(theta(1))*(y1-w-d)-tan(theta(2))*w - tan(theta(1))*(d+v2*t1+v2*theta(3)); %Laser travels x1 in time t
y(3)=(y1-w-d)/cos(theta(1)) + w*n/cos(theta(2)) + (d+v2*t1+v2*theta(3))/cos(theta(1)) - vl*theta(3); %Distance travelled in time t
y(4)=x1-theta(4); %To output xf
y(5)=y1+v2*t1+v2*theta(3)-theta(5); %To output yf
end
%function to returns the order assigned to a cell (i,j) in the board
function index=fij(i,j,n)
index=i+(j-1)*n;
end
\ No newline at end of file
%Outlab 1: Task A1 - solving a given physical situation
[theta, fval, info]= fsolve(@f, [0.001;0.001]);
%write to output file
fid = fopen("output_outlab_task_A1.txt",'w');
fdisp(fid,theta(1)*180/pi);
fclose(fid);
%Outlab 1: Task A2 solving a physical situation
[theta, fval, info] = fsolve(@f2,[0.5;0.1;1000;50.0;27.0]);
c = [theta(1)*180/pi,theta(4),theta(5)];
%write output to file
fid = fopen("output_outlab_task_A2.txt",'w');
fdisp(fid,c);
fclose(fid);
Group 25 Marauders
(Shaan Vaidya 150050004) (Abhishek Kumar 150050020) (Vishwajeet Singh Bagdawat 150050046)
We pledge on our honour that we have not received or given any unauthorised assistance on this assignment or any previous task.
Contribution: Shaan 100% Abhishek 100% Vishwajeet 100%
Citations:
1. http://www.dm.unibo.it/~lenci/teaching/14/maa/octavetut.pdf - Learnt basic syntax and their implementation
2. https://www.gnu.org/software/octave/doc/v4.0.1/Binary-I_002fO.html - Learnt how to write output into a text file
3. https://www.gnu.org/software/octave/doc/v4.0.0/Rearranging-Matrices.html - Learnt how to reshape matrix
4. https://www.gnu.org/software/octave/doc/v4.0.1/The-if-Statement.html - Learnt to use if statement
5. http://stackoverflow.com/questions/19387181/octave-mod-function-and-integer - Faced a problem in mod operator, so this answer on stack exchange on the default behavior of mod operator greatly helped in solving the problem.
6. https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Vectors_and_matrices - Extracting a sub-matrix of a matrix
7. http://pari.math.u-bordeaux.fr/archives/pari-users-0301/msg00002.html - Figured out how to find matrix inverse in mod 2
Reflection Essay:
A1, A2 involved solving non-linear equations. For solving the equations, we used fsolve function. We did face problems with that as fsolve spat out different solutions for slightly different starting estimates which is a normal thing, given that it employs numerical methods to solve the equations so different starting estimates may converge to a bit different answers. This is always going to be a drawback so it is good to have as good an estimate as possible
The Lights Out! puzzle is an extremely interesting problem and can involve further analysis regarding existence of solutions which is assumed in the problem given, and the number of solutiobs. B1, B2 involved solving a system of linear equations or in other words a matrix equation. Building the toggle matrix was pretty trivial. What took the most time was solving the equation in a finite field or mod 2 or Galois field(2). Some internet "research" showed that matrix inverse mod 2 can simply be obtained by multiplying the normal inverse with the determinant. This observation had been made by us already just by looking at the values the inv() function turned up with.
On a lighter note, this assignment though quite interesting took a toll on us in not being able to celebrate a friend's birthday :P It was Vishwajeet's birthday and we couldn't even get a birthday cake for him because we were all busy. He's gonna hold that against us for long...
\ No newline at end of file
function f(current,A,action)
B= [0 0 1;0 1 0;0 1 1;1 0 0;1 0 1;1 1 0;1 1 1]; % Matrix to check what the bottom row is
T= [0 1 1;1 1 1;1 0 0;1 1 0;1 0 1;0 0 1;0 1 0]; % The corresponding operations in the top row to be performed on the basis of the entries of the bottom row
toggle=[1 1 0 1 0 0 0 0 0;1 1 1 0 1 0 0 0 0;0 1 1 0 0 1 0 0 0;1 0 0 1 1 0 1 0 0;0 1 0 1 1 1 0 1 0;0 0 1 0 1 1 0 0 1;0 0 0 1 0 0 1 1 0;0 0 0 0 1 0 1 1 1;0 0 0 0 0 1 0 1 1]; % Toggle matrix
if(current==zeros(3,3))
action=mod(action,2)
else
for n=1:3
if(current(1,n)==1)
A(2,n)=1; % updating the entries of A if there are any ones in the first row of current matrix
endif
end
current=func(current, A); % apply the action matrix A on the current state of the board
action=action+A;
A=zeros(3,3);
for n=1:3
if(current(2,n)==1)
A(3,n)=1; % updating the entries of A if there are any ones in the second row of current matrix
endif
end
current=func(current, A);
action=action+A;
A=zeros(3,3);
for n=1:7
if(current(3,1:3)==B(n,1:3))
A(1,1:3)=T(n,1:3);
endif
end
current=func(current, A);
action=action+A;
A=zeros(3,3);
f(current,A,action);
endif
endfunction
%Task B2: Mathematical solution to Lights Out!
function Answer=final_state_3(b,x)
A = [1 1 0 1 0 0 0 0 0; 1 1 1 0 1 0 0 0 0; 0 1 1 0 0 1 0 0 0; 1 0 0 1 1 0 1 0 0; 0 1 0 1 1 1 0 1 0; 0 0 1 0 1 1 0 0 1; 0 0 0 1 0 0 1 1 0; 0 0 0 0 1 0 1 1 1; 0 0 0 0 0 1 0 1 1];
x = reshape(x',9,1); %Converting x to a column matrix from a 3x3 matrix
D = A*x;
D = reshape(D,3,3); %Converting back to 3x3 matrix for computing (Ax + initial)
D = D';
Answer = mod(b + D,2); % Displaying the answer by taking mod 2
endfunction
\ No newline at end of file
function current = func(current, A)
toggle = [1 1 0 1 0 0 0 0 0;1 1 1 0 1 0 0 0 0;0 1 1 0 0 1 0 0 0;1 0 0 1 1 0 1 0 0;0 1 0 1 1 1 0 1 0;0 0 1 0 1 1 0 0 1;0 0 0 1 0 0 1 1 0;0 0 0 0 1 0 1 1 1;0 0 0 0 0 1 0 1 1];
x = reshape(A',9,1);
x = toggle*x;
x = reshape(x,3,3);
x = x';
current = mod(current+x,2);
endfunction
%Task B1
function lookup_solve_3(initial)
current=initial; % current is basically used to store the currently processed matrix
A=zeros(3,3); % A will be the action matrix corresponding to a particular row
action=zeros(3,3); % output will be the final action matrix ie the desired result
f(current,A,action);
endfunction
Group no. 25 Marauders
(Shaan Vaidya 150050004) (Abhishek Kumar 150050020) (Vishwajeet Singh Bagdawat 150050046)
We pledge on our honor that we have not received or given any unauthorised assistance in this assignment or any previous task.
Note:
Task B1: function lookup_solve_3 takes a single argument (a 3 × 3 matrix representing the initial state of the board) and outputs the action matrix.
Task B2: function final_state_3 takes as input two 3×3 matrix describing the initial state and the switches pressed, and outputs a 3×3 matrix representing the final state of the board.
Citations :
1. https://www.gnu.org/software/octave/doc/v4.0.0/Terminal-Input.html - Learnt how to take input from user
2. https://www.gnu.org/software/octave/doc/v4.0.0/Rearranging-Matrices.html - Learnt how to reshape matrix
3. http://www.dm.unibo.it/~lenci/teaching/14/maa/octavetut.pdf - Learnt basic syntax and their implementation
Reflection Essay:
Created different functions for improved readability
Octave does not support functions with arguments passed by reference so if we want to change the value of an argument passed, pass it also as output
Implemented the light chasing algorithm for solving 'Lights Out!' game
function y = f(theta)
n = load ("input_inlab_task_A1.txt"); %loading input from the file as a form of matrix
nx = n(1);
ny = n(2);
y= sin(theta) - nx*sin(theta)*sin(theta/3) - ny*cos(theta)*sin(theta/3); %physics equation
endfunction
Group 25 Marauders
(Shaan Vaidya 150050004) (Abhishek Kumar 150050020) (Vishwajeet Singh Bagdawat 150050046)
I pledge on my honour that I have not given or received unauthorised assistance on this assignment or any previous task.
Citations:
https://en.wikibooks.org/wiki/Octave : fwrite, fopen
https://www.gnu.org/software/octave/octave.pdf : fzero, user-defined functions
Contribution:
Shaan : 100%
Abhishek: 100%
Vishwajeet: 100%
Reflections:
It was the first lab so it took some getting used to to this novel method of "googling" to learn everything in the code. We had to tweak the code quite a lot but finally it did work. We got it wrong with the physical equation as we assumed a different physical situation; that took a lot of time. We tried 'fsolve' in the beginning and then moved to 'fzero'. We also had to create a different file for the funtion definition.
[theta, fval, info]=fzero(@f, [0.0001, 1.57]) % zeros of function f
%write the output to file
fid = fopen('output_inlab_task_01.txt', 'w');
fdisp(fid, theta);
fclose(fid);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment