Commit 0aa5d714 authored by VINAYAK K's avatar VINAYAK K

Update lab01

parent cd7300da
Group 33 : Lab01_inlab
Group Members:
(Vinayak K, 150050098), (Shriram S B, 150050099), (Anubhaw Kuntal Xess, 150050100)
Vinayak K - 100%
Shriram S B - 100%
Anubhaw Kuntal Xess - 100%
I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
Reflection essay:
- octave function declaration, global variables
- tried fsolve()
- found that fzero() better for single variable
- File I/O in octave
Citations: Gnu octave Documentation
\ No newline at end of file
% nx, ny global variable to hold tensor values
global nx;
global ny;
% reading nx, ny from input file
input_ptr = fopen("input_inlab_task_A1.txt", "r");
[nx, ny, count, errmsg] = fscanf(input_ptr, "%lf %lf", "C");
fclose(input_ptr);
% function which returns value of the expression for some incident angle x(rad)
function ret_val = f (x)
global nx;
global ny;
ret_val = ( sin(x) / sin(x/3) - nx * cos(x) - ny * sin(x) );
endfunction
% finding interval in which function changes sign
a = 1e-10;
dx = 0.1 + 1e-10;
b = dx;
while (f(a)*f(b) >= 0)
a += dx;
b += dx;
endwhile
% the interval in which expression has a root
bracket = [a; b];
% fzero to find value at which expression is zero
[x, fval, info, output] = fzero("f", bracket);
output_ptr = fopen("output_inlab_task_01.txt", "w");
fprintf(output_ptr, "%f", x);
fclose(output_ptr);
% 'final' stores final state of the board
function final = final_state_3(b,x)
% numbering the buttons
a = [1, 2, 3; 4, 5, 6; 7, 8, 9];
% A(i, j) = 1 if button i is toggled on pressing button j
A = zeros(9,9);
% initializing A
for i = 1 : 3
for j = 1 : 3
A(a(i,j), a(i,j)) = 1;
if (i + 1 <= 3)
A(a(i,j), a(i + 1, j)) = 1;
end
if (i - 1 >= 1)
A(a(i,j), a(i - 1, j)) = 1;
end
if (j + 1 <= 3)
A(a(i,j), a(i, j + 1)) = 1;
end
if (j - 1 >= 1)
A(a(i,j), a(i, j - 1)) = 1;
end
endfor
endfor
% b - initial state of board
% x - action vector
% reshaping x and b from 3x3 to 9x1
x = reshape(x, 9, 1);
b = reshape(b, 9, 1);
final = reshape(mod((A*x + b), 2), 3, 3);
endfunction
% output stores final result
% input - matrix given by user
function output = lookup_solve_3(input)
output = [0, 0, 0; 0, 0, 0; 0, 0, 0];
% input used to store state of game after every press of button
% presses (r,c) button and toggles the neighbouring buttons too
function toggle(r, c)
input(r, c) = ~input(r, c);
if r - 1 >= 1
input(r - 1, c) = ~input(r - 1, c);
end
if r + 1 <= 3
input(r + 1, c) = ~input(r + 1, c);
end
if c - 1 >= 1
input(r, c - 1) = ~input(r, c - 1);
end
if c + 1 <= 3
input(r, c + 1) = ~input(r, c + 1);
end
endfunction
% takes a row and the button to press in it as input and presses corresponding buttons
function toggle_row(row, but_press)
for k = 1 : 3
if (but_press(1, k) == 1)
toggle(row, k);
output(row, k) = ~output(row, k);
end
endfor
endfunction
% the table given in problem statement
bot_top_toggle = [0, 1, 1;
1, 1, 1;
1, 0, 0;
1, 1, 0;
1, 0, 1;
0, 0, 1;
0, 1, 0];
while (true)
% 1st and 2nd row made zero
for i = 1 : 2
toggle_row(i + 1, input(i, :) );
endfor
% binary_equivalent of last row for easy access
bottom_binary = 4 * input(3, 1) + 2 * input(3, 2) + input(3, 3);
if (bottom_binary ~= 0)
% toggling top row according to state of bottom row
toggle_row(1, bot_top_toggle(bottom_binary, :));
else
break;
end
endwhile
endfunction
Group 33 : Lab01_inlab
Tasks : B1, B2
Group Members:
(Vinayak K, 150050098), (Shriram S B, 150050099), (Anubhaw Kuntal Xess, 150050100)
Vinayak K - 100%
Shriram S B - 100%
Anubhaw Kuntal Xess - 100%
I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
Reflection essay:
- matrix multiplication
- reshaping of matrices
- nested functions
Citations: Gnu octave Documentation
\ No newline at end of file
function solution = backward_solve(input)
% number of rows, columns in input
[rows, cols] = size(input);
% no_buttons = number of buttons
no_buttons = rows*cols;
A = zeros(no_buttons, no_buttons);
% numbering the buttons
a = zeros(rows, cols);
for i = 1 : rows
for j = 1 : cols
a(i, j) = cols * (i - 1) + j;
endfor
endfor
% A(i,j) = 1 if ith bulb is toggled with jth switch is pressed
% initializing A
for i = 1 : rows
for j = 1 : cols
A(a(i,j), a(i,j)) = 1;
if (i + 1 <= rows)
A(a(i,j), a(i + 1, j)) = 1;
end
if (i - 1 >= 1)
A(a(i,j), a(i - 1, j)) = 1;
end
if (j + 1 <= cols)
A(a(i,j), a(i, j + 1)) = 1;
end
if (j - 1 >= 1)
A(a(i,j), a(i, j - 1)) = 1;
end
endfor
endfor
% vectorization of input as a column vector
input_col_vec = reshape(input', no_buttons, 1);
% ith index of row_of_x contains row_number of A which has (i - 1) zeros before the first one
row_for_x = zeros(1,no_buttons);
% Gaussian elimination of A
for i = 1 : no_buttons
target_rows = find_rows(A, i - 1);
no_of_rows_to_be_added = size(target_rows)(2);
if (no_of_rows_to_be_added >= 2)
for j = 2 : no_of_rows_to_be_added
A(target_rows(j), :) = mod((A(target_rows(j), :) + A(target_rows(1), :)), 2);
input_col_vec(target_rows(j)) = input_col_vec(target_rows(j)) + input_col_vec(target_rows(1));
endfor
end
if (no_of_rows_to_be_added >= 1)
row_for_x(i) = target_rows(1);
end
endfor
% x - vector form of solution
x = zeros(no_buttons, 1);
input_col_vec = mod(input_col_vec, 2);
for i = no_buttons : -1 : 1
if (row_for_x(i) != 0)
x(i) = mod((input_col_vec(row_for_x(i)) + A(row_for_x(i),:)*x), 2);
end
endfor
% solution
x = reshape(x, cols, rows)';
solution = x;
endfunction
function solution = backward_solve_3(input)
% number of rows, columns in input
[rows, cols] = size(input);
% no_buttons = number of buttons
no_buttons = rows*cols;
A = zeros(no_buttons, no_buttons);
% numbering the buttons
a = zeros(rows, cols);
for i = 1 : rows
for j = 1 : cols
a(i, j) = cols * (i - 1) + j;
endfor
endfor
% A(i,j) = 1 if ith bulb is toggled with jth switch is pressed
% initializing A
for i = 1 : rows
for j = 1 : cols
A(a(i,j), a(i,j)) = 1;
if (i + 1 <= rows)
A(a(i,j), a(i + 1, j)) = 1;
end
if (i - 1 >= 1)
A(a(i,j), a(i - 1, j)) = 1;
end
if (j + 1 <= cols)
A(a(i,j), a(i, j + 1)) = 1;
end
if (j - 1 >= 1)
A(a(i,j), a(i, j - 1)) = 1;
end
endfor
endfor
% vectorization of input as a column vector
input_col_vec = reshape(input', no_buttons, 1);
% ith index of row_of_x contains row_number of A which has (i - 1) zeros before the first one
row_for_x = zeros(1,no_buttons);
% Gaussian elimination of A
for i = 1 : no_buttons
target_rows = find_rows(A, i - 1);
no_of_rows_to_be_added = size(target_rows)(2);
if (no_of_rows_to_be_added >= 2)
for j = 2 : no_of_rows_to_be_added
A(target_rows(j), :) = mod((A(target_rows(j), :) + A(target_rows(1), :)), 2);
input_col_vec(target_rows(j)) = input_col_vec(target_rows(j)) + input_col_vec(target_rows(1));
endfor
end
if (no_of_rows_to_be_added >= 1)
row_for_x(i) = target_rows(1);
end
endfor
% x - vector form of solution
x = zeros(no_buttons, 1);
input_col_vec = mod(input_col_vec, 2);
for i = no_buttons : -1 : 1
if (row_for_x(i) != 0)
x(i) = mod((input_col_vec(row_for_x(i)) + A(row_for_x(i),:)*x), 2);
end
endfor
% solution
x = reshape(x, cols, rows)';
solution = x;
endfunction
% finds rows which has 'no_of_zeros' zeros before the first one
function found_rows = find_rows(A, no_of_zeros)
found_rows = zeros(1,0);
for i = 1 : size(A)(1)
if (isequal(A(i,1:no_of_zeros),zeros(1, no_of_zeros)) && isequal(A(i, no_of_zeros + 1), 1))
found_rows(end + 1) = i;
end
endfor
endfunction
\ No newline at end of file
% global variables
global x1;
global y1;
global d;
global w;
global mu;
% reading values from file
input_ptr = fopen("input_outlab_task_A1.txt", "r");
[x1, y1, d, w, mu, count, errmsg] = fscanf(input_ptr, "%lf %lf %lf %lf %lf", "C");
fclose(input_ptr);
% function which gives value of expression at x(1) - incident angle on smoke screen
% x(2) - refracted angle on smoke screen whose(expression's) value has to be made zero
function y = f(x)
global x1;
global y1;
global d;
global w;
global mu;
y = zeros(2,1);
y(1) = (y1 - w) * tand(x(1)) + w * tand(x(2)) - x1;
y(2) = sind(x(1)) - mu * sind(x(2));
endfunction
% solving equations using fsolve
%initial values for fsolve
i = atand(y1/x1);
r = asind(sind(i)/mu);
[x, fval, info] = fsolve("f", [i, r]);
%writing angle at which the laser has to be shot to file
output_ptr = fopen("output_outlab_task_A1.txt", "w");
fprintf(output_ptr, "%.1f\n", x(1));
fclose(output_ptr);
\ No newline at end of file
% global variables
global xTIE;
global yTIE;
global xX_WING;
global yX_WING;
global v2;
global vLaser;
global d;
global w;
global mu;
global t1;
% reading values from file
fid = fopen("input_outlab_task_A2.txt", "r");
[xTIE, yTIE, xX_WING, yX_WING, v2, vLaser, d, w, mu, t1, count, errmsg] = fscanf(fid, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", "C");
fclose(fid);
%(x1, y1) - position of x-Xwing relative to xTIE
global x1 = xX_WING - xTIE;
global y1 = yX_WING - yTIE;
% function which gives value of expression at x(1) - incident angle on smoke screen x(2) - refracted angle on smoke screen
% x(3) - y coordinate of x-Xwing relative to xTIE at the time of impact whose(expression's) value has to be made zero
function y = f(x)
global x1;
global y1;
global v2;
global vLaser;
global d;
global w;
global mu;
global t1;
y= zeros(3,1);
y(1) = (x(3) - w) * tand(x(1)) + w * tand(x(2)) - x1;
y(2) = (v2/vLaser)*((x(3) - w)/(cosd(x(1))) + w * mu/(cosd(x(2)))) - x(3) + y1 + v2 * t1;
y(3) = sind(x(1)) - mu * sind(x(2));
endfunction
% solving the equations using fsolve
%initial values for fsolve
yInitial = y1 + v2*t1;
i = atand(yInitial/x1);
r = asind(sind(i)/mu);
[x, fval, info] = fsolve("f", [i, r, yInitial]);
% writing (angle at which laser has to shot and the nearest position at which x-Xwing will get hit to the file )
output_ptr = fopen("output_outab_task_A2.txt", "w");
fprintf(output_ptr, "%.1f %.1f %.1f", x(1), x1, x(3));
fclose(output_ptr);
Group 33 : Lab01_outlab
Tasks : A1, A2, B1, B2
Group Members:
(Vinayak K, 150050098), (Shriram S B, 150050099), (Anubhaw Kuntal Xess, 150050100)
Vinayak K - 100%
Shriram S B - 100%
Anubhaw Kuntal Xess - 100%
I pledge on my honour that I have not given or received any unauthorized assistance on this assignment or any previous task.
Reflection essay (A1, A2) :
- Coverting a problem related to physics to mathematical equations and solving them using octave
- Learnt to solve simultaneous equations using fsolve
Reflection essay (B1, B2) :
- appending to the end of vector
- reshape works in column major order but not row major order
- Gaussian elimination
find_rows.m contains helper function required by backward_solve.m and backward_solve_3.m
backward_solve.m works even with 3x3 matrix.
So, for backward_solve_3.m is just the copy of backward_solve.m
Citations:
- Gnu octave Documentation
- http://www.ueda.info.waseda.ac.jp/~n-kato/lightsout/ for how Lights out work and how to solve it (Found Gaussian Elimination from here)
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