Commit d44a7438 authored by KARTIK SINGHAL's avatar KARTIK SINGHAL

lab1-octave

parent 1a13971d
Group 11 - !nfinity
(Kartik Singhal, 150050025) (Pranay Agrawal, 150050026) (Nilesh Gupta, 150050059)
Student 1: 100%
Student 2: 100%
Student 3: 100%
I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
Citations:
http://stackoverflow.com
www.gnu.org/software/octave/doc/v4.0.0
http://in.mathworks.com/help/matlab/
http://www.wolframalpha.com/
http://en.flossmanuals.net/
http://www.dm.unibo.it/~lenci/teaching/14/maa/octavetut.pdf
http://www.gnu.org/software/octave/octave.pdf
Reflections:
1. Learnt Ocatave- taking input from text files
loading output in text files
finding roots of a equation
2. Learnt process of Bi-Refringence
\ No newline at end of file
a=load('input_inlab_task_A1.txt');# takes input from text file and stores the numbers in an array
nx=a(1);#assign nx the first element of a
ny=a(2);#assign ny second element of a
g=@(r)(nx*cos(r)+ny*sin(r))*sin(r/3)-sin(r);# define g as function of r
x0=[0 + 1e-5 pi/2];#range in which r should lie, 0 is a trivial solution irrespective of value of nx and ny
r=fzero(g,x0);#computes zero of function g in range x0 and assign it to r
fileID = fopen('output_inlab_task_01.txt','w');#opens the file in which output is to be loaded
fprintf(fileID,'%4.5f\n',r);#writing the ouput in output file
fclose(fileID);#closing the output file
\ No newline at end of file
function y=final_state_3(i,a)
b=[ 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; # in matrix b, b(i,j)=1 iff button i is toggled on pressing button j
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];
initial=i(:); # converts initial matrix i of order 3*3 into order 9*1
action=a(:); # converts action matrix a of order 3*3 into order 9*1
x=b*action+initial;
y=[ 0 0 0;
0 0 0;
0 0 0];
for i=1:9
y(i)=rem(x(i),2); #converts back x of order 9*1 into y matrix of order 3*3 and takes modulo with 2
endfor
return;
endfunction
\ No newline at end of file
function c=lookup_solve_3(a)
c=[ 0 0 0;
0 0 0; #initialising the matrix c, which keep counts of which switch is pressed how many times
0 0 0];
for x=0:2
if(a(x*3+1)==1)
a=toggle(a,x*3+2); #turns off all the lights in the top row
c(x*3+2)++;
endif
endfor
for x=0:2
if(a(x*3+2)==1)
a=toggle(a,x*3+3); #turns off all the lights in the middle row
c(x*3+3)++;
endif
endfor
# now checks the situation of bottom row and with the help of table presses the switches in top row
if(a(3,:)==[0 0 1])
a=toggle(a,4);
c(4)++;
a=toggle(a,7);
c(7)++;
endif
if(a(3,:)==[0 1 0])
a=toggle(a,1);
c(1)++;
a=toggle(a,4);
c(4)++;
a=toggle(a,7);
c(7)++;
endif
if(a(3,:)==[0 1 1])
a=toggle(a,1);
c(1)++;
endif
if(a(3,:)==[1 0 0])
a=toggle(a,1);
c(1)++;
a=toggle(a,4);
c(4)++;
endif
if(a(3,:)==[1 0 1])
a=toggle(a,1);
c(1)++;
a=toggle(a,7);
c(7)++;
endif
if(a(3,:)==[1 1 0])
a=toggle(a,7);
c(7)++;
endif
if(a(3,:)==[1 1 1])
a=toggle(a,4);
c(4)++;
endif
for x=0:2
if(a(x*3+1)==1)
a=toggle(a,x*3+2); #repeat the same process, turns off the lights in toprow
c(x*3+2)++;
endif
endfor
for x=0:2
if(a(x*3+2)==1)
a=toggle(a,x*3+3); # repeat the same process, turns off the lights in middle row
c(x*3+3)++;
endif
endfor
for x=1:9
c(x)=rem(c(x),2); # if the button is pressed even number of times then, no need of it to be pressed and if it is pressed odd number of times then it should be pressed only once.
endfor
return;
endfunction
\ No newline at end of file
Group 11 - !nfinity
(Kartik Singhal, 150050025) (Pranay Agrawal, 150050026) (Nilesh Gupta, 150050059)
Student 1: 100%
Student 2: 100%
Student 3: 100%
I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
Citations:
http://stackoverflow.com
www.gnu.org/software/octave/doc/v4.0.0
http://in.mathworks.com/help/matlab/
http://www.wolframalpha.com/
http://en.flossmanuals.net/
http://www.dm.unibo.it/~lenci/teaching/14/maa/octavetut.pdf
http://www.gnu.org/software/octave/octave.pdf
Reflections:
1. Learnt Octave-
Writing functions in octave
writing for and if loops
2. Learnt the mathematical reasoning behind lightsout problem
3. Learnt Light Chasing algorithm for solving lightsout problem
B1-lookup_solve_3.m
The function takes a 3*3 matrix in initial state and returns a 3*3 matrix which denotes which switches are to be pressed.
It uses another function toggle inside it which is defined separately.
toggle(a,x) -> returns the matrix situation of matrix a of order 3*3 after switch x is pressed
Switches: 1 4 7
2 5 8
3 6 9
B2-final_state_3.m
The function takes two 3*3 matrix i & a. i denotes the initial state of lights and a denotes the action matrix.
It ouputs the final state of the matrix
function c=toggle(a,x)
if(rem(x,3)==0)
b=[x-3 x-1 x x+3];
elseif(rem(x,3)==1)
b=[x-3 x x+1 x+3]; # vector b stores the switch numbers which will be toggled by pressing the switch x;
else
b=[x-3 x-1 x x+1 x+3];
endif
for i=1:length(b)
if(b(i)>0 && b(i)<10)
a(b(i))=1-a(b(i)); # toggles the value of switch
endif
endfor
c=a; # returns matrix c which is the state of matrix a after pressing switch x;
endfunction
\ No newline at end of file
function y=backward_solve(i)
rows=size(i,1);
columns=size(i,2);
c=0*ones (rows*columns, rows*columns); # initialise c matrix as NULL of order (rows*columns)*(rows*columns)
for j=1:rows*columns
if(rem(j,rows)==0)
b=[j-rows j-1 j j+rows];
elseif(rem(j,rows)==1)
b=[j-rows j j+1 j+rows]; # vector b stores the switch numbers which will be toggled by pressing the switch x;
else
b=[j-rows j-1 j j+1 j+rows];
endif
for k=1:length(b)
if(b(k)>0 && b(k)<rows*columns+1)
c(j,b(k))=1; # in matrix c, c(i,j)=1 iff button i is toggled on pressing button j
endif
endfor
endfor
initial=i(:); # converts initial matrix i of order 3*3 into order 9*1
x=det(c);
action=-x*inverse(c)*initial; #computes the action matrix using matrix operations
w=int32(action); #round off the action matrix into integer
y=0*ones (rows, columns); #initialise c matrix as NULL of order rows*columns
for i=1:rows*columns
y(i)=mod(w(i),2); # takes remainder with 2
endfor
return;
endfunction
\ No newline at end of file
function y=backward_solve_3(i)
b=[ 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; # in matrix b, b(i,j)=1 iff button i is toggled on pressing button j
0 0 1 0 1 1 0 0 1;
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];
initial=i(:); # converts initial matrix i of order 3*3 into order 9*1
x=det(b);
action=-x*inverse(b)*initial; #computes the action matrix using matrix operations
z=int32(action); #round off the action matrix into integer
y=[ 0 0 0;
0 0 0;
0 0 0];
for i=1:9
y(i)=mod(z(i),2); # takes remainder with 2
endfor
return;
endfunction
\ No newline at end of file
a=load('input_outlab_task_A1.txt'); # takes input from the file and store in vector
global x1=a(1);
global y1=a(2);
global d=a(3); #assign respective values in variables from the vector
global w=a(4);
global n=a(5);
function y=f(x)
global x1;
global y1;
global d; #variable declaration inside the function
global w;
global n;
y(1)=x(1)+x(2)+x(3)-x1;
y(2)=tan(x(4))*(y1-d-w)-x(1);
y(3)=tan(x(5))*w-x(2); #equation which are to be solved
y(4)=tan(x(4))*d-x(3);
y(5)=sin(x(4))-n*sin(x(5));
endfunction
[x,info]=fsolve("f",[0;0;0;0;0]); #solves the equation using fsolve
fileID = fopen('output_outlab_task_A1.txt','w');
fprintf(fileID,'%4.1f\n',x(4)*180/pi); #reads the output in file
fclose(fileID);
\ No newline at end of file
a=load('input_outlab_task_A2.txt'); # takes input from the file and store in vector
global x0=a(1);
global y0=a(2);
global x1=a(3);
global y1=a(4);
global v2=a(5);
global vl=a(6); #assign respective values in variables from the vector
global d=a(7);
global w=a(8);
global n=a(9);
global t1=a(10);
x1=x1-x0;
y1=y1-y0; #relative coordinate of x-wing with respect to TIE
function y=f(x)
global x1;
global y1;
global v2;
global vl; #variable declaration inside the function
global d;
global w;
global n;
global t1;
y(1)=(y1+v2*(x(3)+t1))*tan(x(1))+w*tan(x(2))-w*tan(x(1))-x1;
y(2)=(y1+v2*(x(3)+t1))*sec(x(1))-w*sec(x(1))+n*w*sec(x(2))-vl*x(3); #equation which are to be solved
y(3)=n*sin(x(2))-sin(x(1));
endfunction
theta = 0;
phi = 0; #initialising the variables as 0
time = 0;
[x,fval, info]=fsolve("f",[theta;phi;time]); #solves the equation using fsolve
while (info != 1)
theta = x(1);
phi = x(2); #while loop till the value for the equations converges
time = x(3);
[x,fval, info]=fsolve("f",[theta;phi;time]);
endwhile
fileID = fopen('output_outlab_task_A2.txt','w');
fprintf(fileID,'%4.1f %4.1f %4.1f',x(1)*180/pi,x1,y1+v2*(x(3)+t1)); #reads the output in file
fclose(fileID);
\ No newline at end of file
Group 11 - !nfinity
(Kartik Singhal, 150050025) (Pranay Agrawal, 150050026) (Nilesh Gupta, 150050059)
Student 1: 100%
Student 2: 100%
Student 3: 100%
I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
Citations:
http://stackoverflow.com
www.gnu.org/software/octave/doc/v4.0.0
http://in.mathworks.com/help/matlab/
http://www.wolframalpha.com/
http://en.flossmanuals.net/
http://www.dm.unibo.it/~lenci/teaching/14/maa/octavetut.pdf
http://www.gnu.org/software/octave/octave.pdf
Reflections:
1. Learnt Octave-
how fsolve function works
writing while loops
2. Learnt a fast and efficient way of solving lightsout problem for any order m*n
3. Learnt Solving various equations simultaneously in octave
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