Commit fcaaf8af authored by HARSH  DEPAL's avatar HARSH DEPAL

Outlab 1

parent db23a515
Group number 23
Group members:
(Harsh Depal, 150050010),(Tarun Verma, 150050021),(Bhavya Choudhary, 150050028)
Honor code:-
I pledge on my honour that I have not given or received
any unauthorized assistance on this assignment or any previous task.
Individual Percentage :
Harsh Depal:100%, Tarun Verma:100%, Bhavya Choudhary:100%
Citations:
www.math.utah.edu
stackoverflow.com
gnu.org
www.dm.unibo.it
tutorialspoint.com
http://www.ueda.info.waseda.ac.jp/~n-kato/lightsout/
wikipedia.org
Brief Reflection :
We had study little bit of modular arithmatics for matrices to solve the Lights Out puzzle.
As we couldn't find a function can do modular arithmatic in Octave we had to write our own functions using for loops.
We learned how octave with its powerful built-in functions can be used to solve complicated mathematical equations.
\ No newline at end of file
function action = backward_solve_3(cstate)
[row,col]=size(cstate);
M=[]; # M will become the matrix where Mij = 1 ⇐⇒ button i is toggled on pressing button j
for x=1:row*col
for y=1:row*col
if(abs(y-x) == col | (abs(y-x)<=1 & floor((y-1)/col)==floor((x-1)/col)))
v(y)=1;
else
v(y)=0;
endif
endfor
M=[M;v]; # adding ith column as a row. we will transpose it later
endfor
for i=1:row
for j=1:col
w((i-1)*col+j)=cstate(i,j); # converting cstate into vector
endfor
endfor
M=[M;w]; # making augmented matrix
M = M';
guassM=guass_elim(M); # last column is the answer, doing guass elimination
action=[];
for l=1:row # converting last column as a matrix
for k=1:col
Arow(k)=guassM((l-1)*col+k,col*row+1);
endfor
action=[action;Arow];
endfor
endfunction
function action = backward_solve_3(cstate)
[row,col]=size(cstate);
M=[]; # M will become the matrix where Mij = 1 ⇐⇒ button i is toggled on pressing button j
for x=1:row*col
for y=1:row*col
if(abs(y-x) == col | (abs(y-x)<=1 & floor((y-1)/col)==floor((x-1)/col)) )
v(y)=1;
else
v(y)=0;
endif
endfor
M=[M;v]; # adding ith column as a row. we will transpose it later
endfor
for i=1:row
for j=1:col
w((i-1)*col+j)=cstate(i,j); # converting cstate into vector
endfor
endfor
M=[M;w]; # making augmented matrix
M = M';
guassM=guass_elim(M); # last column is the answer
action=[];
for l=1:row # converting last column as a matrix
for k=1:col
Arow(k)=guassM((l-1)*col+k,col*row+1);
endfor
action=[action;Arow];
endfor
endfunction
# function to solve the matrix "augmented" by guassian elimination with modular arithematic and return the guass jordan form as the final "matrix"
function final = guass_elim(augmented)
final = augmented;
n = size(final,1); # n stores the number of rows in final
for i = 1 : n
if (final(i,i)!=1)
# for loop to make i,i position 1 by row operations
for a = i : n
if (final(a,i) == 1)
final(i,:) = mod((final(i,:) + final(a,:)),2);
break;
endif
endfor
endif
# for loop to make all elements of column i 0 except i,i
for a = 1 : n
if (final(a,i) == 1 && a!=i)
final(a,:) = mod((final(i,:) + final(a,:)),2);
endif
endfor
endfor
endfunction
\ No newline at end of file
filename = "input_outlab_task_A1.txt";
[x1,y1,d,w,n] = textread(filename,"%f %f %f %f %f"); #reading data from input file
f = @(x) [(y1-w)*tand(x(1)) + w*tand(x(2)) - x1;n*sind(x(2)) - sind(x(1))];
x = fsolve(f,[0;0]); # using fsolve to find the required values
csvwrite("output_outlab_task_A1.txt",x(1)); #writing the calculated answer in the output file
\ No newline at end of file
inputfile = "input_outlab_task_A2.txt";
outputfile = "output_outlab_task_A2.txt";
[xt,yt,xx,yx,v2,vl,d,w,n,t1] = textread(inputfile,"%f %f %f %f %f %f %f %f %f %f"); # reading data from input file
x1 = xx - xt;
y1 = yx - yt + t1*v2;
f = @(x) [sind(x(1))-n*sind(x(2));(y1 + ((y1-w+w*n*cosd(x(1))/cosd(x(2)))/(vl*cosd(x(1))-v2))*v2)*tand(x(1)) + w*(tand(x(2))-tand(x(1))) - x1];
x = fsolve(f,[0;0]); # using fsolve to find required vector x
xf = xx;
yf = yx + t1*v2 + ((y1-w+n*w*cosd(x(1))/cosd(x(2)))/(vl*cosd(x(1))-v2))*v2; # calculating final coordinates xf and yf
ofile = fopen(outputfile,"w");
fprintf(ofile,"%f %f %f",x(1),xx,yf); # writing the calculated answer in output file
fclose(ofile);
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