Commit 8a241481 authored by DIVYANSH's avatar DIVYANSH

150020086

parents
function [output_matrix] = backward_solve(input_matrix) %takes input matrice and gives solution matrice
action_matrix=Get_Matrix(input_matrix); %where action_matrix(i,j) = 1 ⇐⇒ button i is toggled on pressing button j
csize=columns(input_matrix); %columns in initial given matrix
rsize=rows(input_matrix); %rows in initial given matrix
aug_matrix=transpose(input_matrix); %aug_matrix has column vector of input_matrix
aug_matrix=vec(aug_matrix);
[action_matrix aug_matrix]=rref(action_matrix,aug_matrix); %rref in mod2
output_matrix=reshape(aug_matrix,csize,rsize); %output_matrix contains solution to input_matrix
output_matrix=transpose(output_matrix);
endfunction
function isvalid = check(x,y,row,column) %return the bool isvalid to Get_Matrix
isvalid = 0;
x1 = fix((x-1)/column)+1;
y1 = mod(x-1,column)+1; %obtaining the coordinates of element corresponding to 'x'
x2 = fix((y-1)/column)+1;
y2 = mod(y-1,column)+1; %obtaining the coordinates of element corresponding to 'y'
if(x1+1==x2&&y1==y2)
isvalid=1;
endif
if(x1-1==x2&&y1==y2)
isvalid=1;
endif
if(y1+1==y2&&x1==x2)
isvalid=1;
endif
if(y1-1==y2&&x1==x2)
isvalid=1;
endif
if(x==y)
isvalid=1;
endif
endfunction
function output = Get_Matrix(matrix) %function to make the main Matrix A
row = rows(matrix);
column = columns(matrix);
output = zeros( row*column ); %create a matrix with all the entries '0'
for x = 1:row*column
for y = 1:row*column
if(check(x,y,row,column))
output(x,y)=1; %changing the values in output to make A matrix
endif
endfor
endfor
endfunction
function [action_matrix aug_matrix] = rowadd(row1,row2,action_matrix,aug_matrix) %adding row1 to row2
colsize=columns(action_matrix);
for k=1:colsize
action_matrix(row2,k)=eleoperation(action_matrix(row1,k),action_matrix(row2,k));
endfor
aug_matrix(row2,1)=eleoperation(aug_matrix(row1,1),aug_matrix(row2,1));
endfunction
function f_value = eleoperation(i,j) %returns mod2 operation on variables i and j
if(i==0 && j==0)
f_value=0;
else
f_value=!(i && j);
endif
endfunction
function [action_matrix aug_matrix] =rowinterchange(row1,row2,action_matrix,aug_matrix) %interchanges row1 with row2
x=action_matrix(row1,:);
action_matrix(row1,:)=action_matrix(row2,:);
action_matrix(row2,:)=x;
y=aug_matrix(row1,1);
aug_matrix(row1,1)=aug_matrix(row2,1);
aug_matrix(row2,1)=y;
endfunction
function [action_matrix aug_matrix] =ref(action_matrix,aug_matrix) %calculating reduced echlon form in mod2
colsize=columns(action_matrix);
rowsize=rows(action_matrix);
pivot=0;
temp=-1;
found=0;
for i=1:colsize
for j=(pivot+1):rowsize
if(action_matrix(j,i)==1)
if(temp==-1)
pivot=j;
temp=pivot-1;
else
temp=pivot;
pivot=j;
endif
[action_matrix aug_matrix]=rowinterchange((temp+1),pivot,action_matrix,aug_matrix);
pivot=temp+1;
found=1;
break;
endif
endfor
if(found==0)
continue;
else
for k=(pivot+1):rowsize
if(action_matrix(k,i)==1)
[action_matrix aug_matrix]=rowadd(pivot,k,action_matrix,aug_matrix);
endif
endfor
found=0;
endif
endfor
endfunction
function [action_matrix aug_matrix] =rref(action_matrix,aug_matrix) %calculating row reduced echlon form
colsize=columns(action_matrix);
rowsize=rows(action_matrix);
[action_matrix aug_matrix]=ref(action_matrix,aug_matrix);
pivot=0;
temp=-1;
found=0;
for i=colsize:-1:1
for j=rowsize:-1:1
if(action_matrix(j,i)==1)
pivot=j;
found=1;
break;
endif
endfor
if(found==0)
continue;
else
for k=(pivot-1):-1:1
if(action_matrix(k,i)==1)
[action_matrix aug_matrix]=rowadd(pivot,k,action_matrix,aug_matrix);
endif
endfor
found=0;
endif
endfor
endfunction
function [output_matrix] = backward_solve_3(input_matrix) %takes input matrice and gives solution matrice
action_matrix= [ 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;];
%where action_matrix(i,j) = 1 ⇐⇒ button i is toggled on pressing button j
csize=3; %columns in initial given matrix
rsize=3; %rows in initial given matrix
aug_matrix=transpose(input_matrix); %aug_matrix has column vector of input_matrix
aug_matrix=vec(aug_matrix);
[action_matrix aug_matrix]=rref(action_matrix,aug_matrix); %rref in mod2
output_matrix=reshape(aug_matrix,csize,rsize); %output_matrix contains solution to input_matrix
output_matrix=transpose(output_matrix);
endfunction
function [action_matrix aug_matrix] = rowadd(row1,row2,action_matrix,aug_matrix) %adding row1 to row2
colsize=columns(action_matrix);
for k=1:colsize
action_matrix(row2,k)=eleoperation(action_matrix(row1,k),action_matrix(row2,k));
endfor
aug_matrix(row2,1)=eleoperation(aug_matrix(row1,1),aug_matrix(row2,1));
endfunction
function f_value = eleoperation(i,j) %returns mod2 operation on variables i and j
if(i==0 && j==0)
f_value=0;
else
f_value=!(i && j);
endif
endfunction
function [action_matrix aug_matrix] =rowinterchange(row1,row2,action_matrix,aug_matrix) %interchanges row1 with row2
x=action_matrix(row1,:);
action_matrix(row1,:)=action_matrix(row2,:);
action_matrix(row2,:)=x;
y=aug_matrix(row1,1);
aug_matrix(row1,1)=aug_matrix(row2,1);
aug_matrix(row2,1)=y;
endfunction
function [action_matrix aug_matrix] =ref(action_matrix,aug_matrix) %calculating reduced echlon form in mod2
colsize=columns(action_matrix);
rowsize=rows(action_matrix);
pivot=0;
temp=-1;
found=0;
for i=1:colsize
for j=(pivot+1):rowsize
if(action_matrix(j,i)==1)
if(temp==-1)
pivot=j;
temp=pivot-1;
else
temp=pivot;
pivot=j;
endif
[action_matrix aug_matrix]=rowinterchange((temp+1),pivot,action_matrix,aug_matrix);
pivot=temp+1;
found=1;
break;
endif
endfor
if(found==0)
continue;
else
for k=(pivot+1):rowsize
if(action_matrix(k,i)==1)
[action_matrix aug_matrix]=rowadd(pivot,k,action_matrix,aug_matrix);
endif
endfor
found=0;
endif
endfor
endfunction
function [action_matrix aug_matrix] =rref(action_matrix,aug_matrix) %calculating row reduced echlon form
colsize=columns(action_matrix);
rowsize=rows(action_matrix);
[action_matrix aug_matrix]=ref(action_matrix,aug_matrix);
pivot=0;
temp=-1;
found=0;
for i=colsize:-1:1
for j=rowsize:-1:1
if(action_matrix(j,i)==1)
pivot=j;
found=1;
break;
endif
endfor
if(found==0)
continue;
else
for k=(pivot-1):-1:1
if(action_matrix(k,i)==1)
[action_matrix aug_matrix]=rowadd(pivot,k,action_matrix,aug_matrix);
endif
endfor
found=0;
endif
endfor
endfunction
val = load("input_outlab_task_A1.txt"); #loading input file as a vector
x1=val(1,1); #loading values in variables
y1=val(1,2);
d=val(1,3);
w=val(1,4);
n=val(1,5);
y=@(x)((y1-w)*tand(x)+w*sind(x)/sqrt(n^2-sind(x)^2)-x1); #applying equation
x0=[0 89];
sol=fzero(y,x0); #solving equation
fout = fopen("output_outlab_task_A1.txt","w"); #opening the output file
fprintf(fout,"%5.1f\n",sol); #printing the answer in the file
fclose(fout); #closing the file
\ No newline at end of file
#in the question we have to give two guess values in fsolve
global x1; #defining global variables
global v;
global y1;
function y=f(x)
val = load("input_outlab_task_A2.txt"); #loading input file as a vector
global x1;
global v;
global y1;
x1=val(1,1); #loading values in variables
y1=val(1,2);
v=val(1,3);
vl=val(1,4);
d=val(1,5);
w=val(1,6);
n=val(1,7);
t1=val(1,8);
y = zeros (2,1);
y(1)=w*n^2/sqrt(n^2-sind(x(1))^2)+(y1+v*x(2)-w)/cosd(x(1))-vl*(x(2)-t1); #applying equations
y(2)=w*sind(x(1))/sqrt(n^2-sind(x(1))^2)+(y1-w+v*x(2))*tand(x(1))-x1;
endfunction
[x, fval, info] = fsolve (@f,[45;2000]); #solving simultaneous equations
fout = fopen("output_outlab_task_A2.txt","w");
fprintf(fout,"%5.1f\n",x(1)); #printing required answers
fprintf(fout,"%5.1f\n",x1);
fprintf(fout,"%5.1f\n",y1+v*x(2));
fclose(fout); #closing the file
\ No newline at end of file
GROUP NUMBER : 35 (Binary)
Divyansh (150020086) : 100%
Abhay Singh Chauhan (150050034) : 100%
Piyush Bhatore(150050055) : 100%
Honor code : I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
We learnt further coding in octave. We learnt new type of functions some basic,some complicated . We used %f and \n in printf function which was new and printing a float to 1 decimal places. Also we used fsolve to solve two non-linear simultaneous equations in two variables by defining global variables.
In lights out program we understood how to find rref in mod2 and its implementation in octave.We also learned how to resize your matrix into vector and reshape it back to desired size.Further this code helped in exploring a lot more new functions.
Citations:
http://mathworld.wolfram.com/LightsOutPuzzle.html
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