Commit e0884a4a authored by SINGH SOURABH BHAGWAN's avatar SINGH SOURABH BHAGWAN

Lab01 upload

parents
We have made equal contribution to this.
Sourabh,150050009 : 100%
Harshith Goka 150050069 : 100%
Uddeshya 15D110007 : 100%
We pledge on my honour that we have not given or received any unauthorised assistance on this assignment or any previous task.
Citations :
https://www.gnu.org/software/octave/doc/v4.0.0/Global-Variables.html
https://www.gnu.org/software/octave/doc/v4.0.1/Formatted-Output.html#XREFfprintf
https://www.gnu.org/software/octave/doc/v4.0.1/Defining-Functions.html
https://www.gnu.org/software/octave/doc/v4.0.0/Solvers.html
global v; % Global Variable
fileID=fopen('input_inlab_task_A1.txt','r');
v = fscanf(fileID,'%f'); % Input in "nx ny" format
fclose(fileID);
function result = solver(i) % F[x] Solution using Snell's law
global v;
a = v(1,1) * cos(i) + v(2,1) * sin(i);
result = sin(i/3) *(a) - sin(i);
return;
endfunction
x0 = [0.00001,pi/2]; % Expected Range
[ans] = fzero("solver",x0); % Root finding
fid = fopen("output_inlab_task_01.txt","w"); % Creating a output file as writable
fprintf(fid, "%f",ans); % printing into the file
fclose(fid); % closing the file
function result = final_state_3(mat,action) # It operates Matrix mat with action
for i = 1:3
for j = 1:3
if (action(i,j) == 1)
mat = press(mat,i,j); # Calls press
endif
endfor
endfor
result = mat;
return;
endfunction
# We will create 'lookup' function which will actually follow the algorithm to solve
# and a 'press' fucntion which will simulate the pressing of a particular button
function res = lookup_solve_3(mat)
result = zeros(3);
while(!(isequal(mat,zeros(3)))) # Until mat becomes null matrix
for (i=1:2)
for (j=1:3)
if(mat(i,j)==1),
result (i+1,j) = ! result(i + 1,j); #pressing the buttons which are below the on buttons
mat = press(mat,i+1,j);
endif
endfor
endfor
if(mat(3,:)==[0 0 1]), # Look up table implemented
result (1,2) = ! result(1,2);
result (1,3) = ! result(1,3);
mat = press(mat,1,2);mat = press(mat,1,3);
elseif(mat(3,:)==[0 1 0]),
result (1,1) = ! result(1,1);
result (1,2) = ! result(1,2);
result (1,3) = ! result(1,3);
mat = press(mat,1,1);mat = press(mat,1,2); mat=press(mat(1,3));
elseif(mat(3,:)==[0 1 1]),
result (1,1) = ! result(1,1);
mat=press(mat,1,1);
elseif(mat(3,:)==[1 0 0]),
result (1,1) = ! result(1,1);
result (1,2) = ! result(1,2);
mat=press(mat,1,1);mat=press(mat,1,2);
elseif(mat(3,:)==[1 0 1]),
result (1,1) = ! result(1,1);
result (1,3) = ! result(1,3);
mat=press(mat,1,1);mat=press(mat,1,3);
elseif(mat(3,:)==[1 1 0]),
result (1,3) = ! result(1,3);
mat=press(mat,1,3);
elseif(mat(3,:)==[1 1 1]),
result (1,2) = ! result(1,2);
mat=press(mat,1,2);
endif
endwhile
res=result;
return;
endfunction
function res1 = press(mat,n,m) # Simulates the pressing of button (n,m) on the mat
mat(n,m) = !(mat(n,m));
if(n+1<=3), # Checking boundary conditions and inverting the bulbs
mat(n+1,m) = !(mat(n+1,m));
endif
if(n-1>=1),
mat(n-1,m) = !(mat(n-1,m));
endif
if(m+1<=3),
mat(n,m+1) = !(mat(n,m+1));
endif
if(m-1>=1),
mat(n,m-1) = !(mat(n,m-1));
endif
res1=mat;
return;
endfunction
\ No newline at end of file
We are team 27 Audacity.
We have made equal contribution to this.
Sourabh,150050009 : 100%.
Harshith Goka 150050069 : 100%.
Uddeshya 15D110007 : 100%.
We pledge on our honour that we have not given or received any unauthorised assistance on this assignment or any previous task consciously or unconsciously.
What have we learnt:
We learnt to do some basic operation on matrices and revised our concepts of liner algebra.
We also learnt about control statements and functions in octave.
References:
https://www.gnu.org/software/octave (GNU Manual)
\ No newline at end of file
We have made equal contribution to this.
Sourabh,150050009 : 100%
Harshith Goka 150050069 : 100%
Uddeshya 15D110007 : 100%
We pledge on our honour that we have not given or received any unauthorised assistance on this assignment or any previous task.
Reference :
https://www.gnu.org/software/octave
Reflection Essay :
We learnt basic funtionality of octave in Linear Algebra like: inverses; transpose; determinant , finding roots of Equation, creating functions and accessing it from other files. Also learnt basic input output methods in octave.
\ No newline at end of file
function ret = backward_solve(mat)
n=size(mat)(1) ;
m = size(mat)(2);
b=reshape(mat',n*m,1); # makes it a column matrix
Q=generate(n,m); # generates the adjency matrix
R= - (Q\b) * det(Q); # Qx + b = 0 as written in the given webpage
x=mod(round(R),2); # rounding to mod 2
X=reshape(x,m,n);
ret = X';
return;
endfunction
clear;
function ret = backward_solve_3(mat)
n=length(mat);
b=reshape(mat,1,n*n);#reshaping matrix 3x3 to 1x9
Q=zeros(n*n);
for (i=1:n),#generating Q
for (j=1:n),
A=zeros(n);
A = press(A,i,j);#simulating pressing of button
Q(j + (i - 1)*n,:) = reshape(A,1,n*n);
endfor
endfor
R= Q\(b') * det(Q);# Solving eqn Qx+b=0
X=reshape(R,n,n)';
ret = mod(round(X),2);# Simplifying matrix by taking mod2
return;
endfunction
clear;
function result = final_state_3(mat,action) # It operates Matrix mat with action
for i = 1:3
for j = 1:3
if (action(i,j) == 1)
mat = press(mat,i,j); # Calls press
endif
endfor
endfor
result = mat;
return;
endfunction
function result = generate(n,m)#generates the adjency matrix
temp = zeros(n*m);
for i = 1:n
for j = 1:m
temp(:,j + (i - 1) * m) = reshape(((press(zeros(n,m),i,j)))',n*m,1);
endfor
endfor
result = temp;
return;
endfunction
180.0 100 0.05 5 3.5
\ No newline at end of file
76.63 27 0.027 0.0555 7 5 3.5 5
\ No newline at end of file
0.0 0.0 76.63 27 0.1 0.027 0.0555 7 10 3.5 5 3.0 0.0
\ No newline at end of file
global var = load('input_outlab_task_A1.txt')#reading values from input file
function ret = eqnset1(i)
global var;
x1 = var(1); #assigning of diiferent input variables
y1 = var(2);
d = var(3);
w = var(4);
u = var(5);
ret = ((y1-w)*tan(i))+(w*sin(i)/(sqrt(u^2-(sin(i))^2))) - x1; #equation to find root
return;
endfunction
x0=[0.000001,pi/2];#expected root to be present in this range
ret1 = fzero(@eqnset1,x0);#finding zero
ret1 = ret1*180/pi;#converting to degree
fid = fopen('output_outlab_task_A1.txt','w');
fprintf(fid,'%f',ret1);
fclose(fid);
clear;
global var = load('input_outlab_task_A2.txt');#reading from input file
global final_co=[0,0];#final co-ordinates X-wing
global final_co;
function ret = eqnset2(i)
global var;
global final_co;#intialising variables
x1 = var(1);
y1 = var(2);
v2 = var(3);
vls = var(4);
d = var(5);
w = var(6);
u = var(7);
t1 = var(8); # formula born from physics
t2 = (vls * cos(i) / ( vls * cos(i) - v2)) * (((y1 - (w )) /(vls * cos(i))) + (u * u * w /((sqrt(u^2 - sin(i)^2)) * vls )) + (v2 * t1 /(vls * cos(i))));
y1f = y1+v2*t1+v2*t2; #updated value of final y co-ordinates of X-wing
final_co=[x1,y1f];
ret = ((y1-w -d)*tan(i))+(w*sin(i)/(sqrt(u^2-(sin(i))^2))) + (y1f - y1 + d)*tan(i) - x1;#final func to solve
endfunction
x0= pi/4;
ret1 = fsolve(@eqnset2,x0);#finding root with x0 as intial value
ret1 = ret1*180/pi; #angle in radian
fid = fopen('output_outlab_task_A2.txt' ,'w');
fprintf(fid,'%f %f %f',ret1, final_co(1), final_co(2));
fclose(fid);
clear;
\ No newline at end of file
70.625633
\ No newline at end of file
41.070191 76.630000 91.838433
\ No newline at end of file
function res1 = press(mat,n,m) # Simulates the pressing of button (n,m) on the mat
mat(n,m) = !(mat(n,m));
if(n+1<=size(mat)(1)), # Checking boundary conditions and inverting the bulbs
mat(n+1,m) = !(mat(n+1,m));
endif
if(n-1>=1 ),
mat(n-1,m) = !(mat(n-1,m));
endif
if(m+1<=size(mat)(2)),
mat(n,m+1) = !(mat(n,m+1));
endif
if(m-1>=1),
mat(n,m-1) = !(mat(n,m-1));
endif
res1=mat;
return;
endfunction
\ No newline at end of file
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