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