lower commit

parent 8d89a000
# Created by Octave 4.0.3, Sat Jul 30 10:04:35 2016 IST <akash@akashs-MBP>
# name: A
# type: matrix
# rows: 9
# columns: 9
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
% function res = backward_solve(x)
% b = x(:);
% m = size(x,1);
% n = size(x,2);
% A = zeros(m*n);
% % generating the matrix A, when boxes are numbered vertically
% %%% NOTE: we have a faster version to calculate A, using no for loop. Written explanation is difficult.
% %%% But we can explain verbally. It is attached as a comment below.
% for ai = 1:m*n
% if(ai>1 && mod(ai,m)!=1) %checks if it is not boundary cases
% A(ai,ai-1) = 1; %i toggles i-1
% endif
% if(ai<m*n && mod(ai,m)!=0) %checks if it is not boundary cases
% A(ai,ai+1) = 1; %i toggles i+1
% endif
% if(ai>m) %checks if it is not boundary cases
% A(ai,ai-m) = 1; %i toggles i-m
% endif
% if(ai<=m*n-m) %checks if it is not boundary cases
% A(ai,ai+m) = 1; %i toggles i+m
% endif
% endfor
% A = A + eye(m*n); %i toggles i
% adjA = round(det(A)*inv(A)); % compute adjoint A
% res = mod(adjA*b,2); % solve the equation
% res = reshape(res,m,n); % reshape to appropriate size
% endfunction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% FASTER VERSION:-
function res = backward_solve(x)
b = x(:);
m = size(x,1);
n = size(x,2);
A = zeros(m*n);
% generating the matrix A
A = findFactor(size(x'));
% A(i,i) = 1 for all i
adjA = round(det(A)*inv(A)); % compute adjoint A
res = mod(adjA*b,2); % solve the equation
res = reshape(res,m,n); % reshape to appropriate size
endfunction
%% file findFactor.m :-
% function res = findFactor(size)
% m = size(1);
% n = size(2);
% ID = eye(m*n);
% res = ID;
% ID(n:n:m*n,n:n:m*n) = 0;
% res(m*n+1 : (m*n)^2) += ID(1 : m*n*(m*n-1));
% ID(n:n:m*n,n:n:m*n) = eye(m);
% ID(n+1:n:m*n,n+1:n:m*n)=0;
% res(1 : m*n*(m*n-1)) += ID(m*n+1 : (m*n)^2);
% ID(n+1:n:m*n,n+1:n:m*n)= eye(m-1);
% res(m*n^2+1 : (m*n)^2) += ID(1 : m*n^2*(m-1));
% res(1 : m*n^2*(m-1)) += ID(m*n^2+1 : (m*n)^2);
% endfunction;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function for backward solving 3*3 matrix
function y = backward_solve_3(x)
b = x'(:); % Convert input matrix to a vector
load Apush; % Load the matrix which shows the lights toggled on pressing a specific button.
AdjA = round(det(A)*inv(A)); % Calculating the adjoint of the above matrix
z = mod(-AdjA*b,2); % Solving the equation (b + Ax) % 2 = 0 for x using modular arithmetics
% x = (-AdjA*b) % 2
y = reshape(z,3,3)'; % Coverting vector to a 3*3 matrix showing the buttons to be pressed for lights out
endfunction
function res = findFactor(size)
m = size(1);
n = size(2);
ID = eye(m*n);
res = ID;
ID(n:n:m*n,n:n:m*n) = 0;
res(m*n+1 : (m*n)^2) += ID(1 : m*n*(m*n-1));
ID(n:n:m*n,n:n:m*n) = eye(m);
ID(n+1:n:m*n,n+1:n:m*n)=0;
res(1 : m*n*(m*n-1)) += ID(m*n+1 : (m*n)^2);
ID(n+1:n:m*n,n+1:n:m*n)= eye(m-1);
res(m*n^2+1 : (m*n)^2) += ID(1 : m*n^2*(m-1));
res(1 : m*n^2*(m-1)) += ID(m*n^2+1 : (m*n)^2);
endfunction;
\ No newline at end of file
file = fopen('output_outlab_task_A1.txt', 'w');
[x y d w n] = num2cell(load('input_outlab_task_A1.txt')){:}; %Load input into variables
fprintf(file, '%f\n', fsolve(@(theta) tand(theta)-(x + w*sind(theta)*(1 - cosd(theta)/sqrt(n^2 - sind(theta)^2))*secd(theta))/y, 45));
% lateral_displacement = w*sind(theta)*(1 - cosd(theta)/sqrt(n^2 - sind(theta)^2)) % Caused due to smoke screen
% x_shoot = x + lateral_displacement*secd(theta) % x-coordinate of the point to shoot at.
% y_shoot = y_shoot % y-coordinate of the point to shoot at.
% Equation used -> tand(theta) = x_shoot/y_shoot
fclose(file);
for i = [1:11:90]
[shootangle,residue,info] = fsolve('ytheta',40); %solves for shootangle with different initial guesses
if(info==1) %implies correct solution found
break;
endif;
endfor;
[xtie ytie xwing ywing v2 vlaser d w n t1] = num2cell(load('input_outlab_task_A2.txt')){:}; %reads from file and assigns the variables respectively
x = xwing-xtie;
y = ywing-ytie; %relative position required
r = asind(sind(shootangle)/n); %snells law
t = (w*tand(r)*(n*n-1)+x)/(vlaser*sind(shootangle)); %time between shoot and hit = sum of (delta x)/(x comp of velocity)
final_xpos = xwing; %final position of xwing
final_ypos = ywing+v2*(t1+t);
fid=fopen('output_outlab_task_A2.txt','w+');
fprintf(fid,"%.2f %f %f",shootangle,xwing,ywing+v2*(t1+t));
fclose(fid);
\ No newline at end of file
Group Number - 37
Honor Code -
I pledge by my honor that I have not received or given any unauthorized help for this assignment.
Contributions -
(Meet Taraviya, mtaraviya, 150050002) - 100%
(Akash Trehan, atrehan, 150050031) - 100%
(Rohit Kumar Jena, rohitrango, 150050061) - 100%
Citations -
-> num2cell(Used this for multiple assignments in one line) - https://www.gnu.org/software/octave/doc/v4.0.0/Creating-Cell-Arrays.html
->sin() for radian, sind() for degrees - https://www.gnu.org/software/octave/doc/v4.0.1/Trigonometry.html
->save and load variables - https://www.gnu.org/software/octave/doc/v4.0.0/Simple-File-I_002fO.html
Reflection Essay -
A1 was pretty easy just using the simple refraction formulae and snell’s law. A2 had some complicated equations and we made some pretty silly mistakes. Also we ran into errors where we got division by zero on putting a particular starting point while not on others. Also we were getting zero in some cases. ’fsolve’ can get really tricky sometimes. We also tried using ‘fzero’ but it requires a range which we were not sure what to give so we went with ‘fsolve’. We saw a post on Piazza saying that we should use ‘fsolve’ with starting point 40 so we did that. For B1 it was easily done using Modular Mathematics. We struggled on creating matrix for B2 but finally came up with 2 algorithms one of which was very fast for large size matrices. We read that loops are slower in octave so we should somehow “vectorise” the code. So the faster algorithm uses matrix manipulation to arrive at a final matrix. We have put both in the file but the slower one is commented out. We got a bit late in submitting since we were just trying to improve and improve the code and documentation. We’ll take care of documentation and readme from next time. Overall it was a nice assignment and we enjoyed :D
\ No newline at end of file
function res = ytheta(theta)
[x0 y0 xw yw v2 vl d w n t1] = num2cell(load('input_outlab_task_A2.txt')){:}; %loads file into all variables, assigning
%individual variables to vector elements
x = xw-x0; %relative position
y = yw-y0; %theta = incidence angle
r = asind(sind(theta)/n); %snells law
t = (w*tand(r)*(n*n-1)+x)/(vl*sind(theta)); %time between shoot and hit = sum of (delta x)/(x comp of velocity)
res = tand(theta)-(x+w*(tand(theta)-tand(r)))/(y+v2*(t1+t)); %res = 0 to be solved by fsolve
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