a=load('input_outlab_task_A2.txt'); # takes input from the file and store in vector

global x0=a(1);
global y0=a(2);
global x1=a(3);
global y1=a(4);
global v2=a(5);
global vl=a(6);						#assign respective values in variables from the vector
global d=a(7);
global w=a(8);
global n=a(9);
global t1=a(10);

x1=x1-x0;
y1=y1-y0;			#relative coordinate of x-wing with respect to TIE

function y=f(x)
	global x1;
	global y1;
	global v2;
	global vl;						#variable declaration inside the function
	global d;
	global w;
	global n;
	global t1;
	y(1)=(y1+v2*(x(3)+t1))*tan(x(1))+w*tan(x(2))-w*tan(x(1))-x1;
	y(2)=(y1+v2*(x(3)+t1))*sec(x(1))-w*sec(x(1))+n*w*sec(x(2))-vl*x(3);			#equation which are to be solved
	y(3)=n*sin(x(2))-sin(x(1));
endfunction

theta = 0;
phi = 0;		#initialising the  variables as 0
time = 0;
[x,fval, info]=fsolve("f",[theta;phi;time]);	#solves the equation using fsolve
while (info != 1)
	theta = x(1);
	phi = x(2);									#while loop till the value for the equations converges
	time = x(3);	
	[x,fval, info]=fsolve("f",[theta;phi;time]);
endwhile

fileID = fopen('output_outlab_task_A2.txt','w');
fprintf(fileID,'%4.1f %4.1f %4.1f',x(1)*180/pi,x1,y1+v2*(x(3)+t1)); #reads the output in file 
fclose(fileID);