% 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);

