

function[]=lab4()


t = linspace (0, 1); % for time from t = 0 to t = 1 hour (default is 100 points)
[x, y] = plane1Position(t);
figure(1);
plot (x, y);
title('Plane 1 position vs time');
xlabel('Time (h)');
ylabel('Distance (km)');
grid on;

% Plot of plane 2 for first hour
t = linspace (0, 1); % for time from t = 0 to t = 1 hour (default is 100 points)
[x, y] = plane2Position(t);
figure(2);
plot (x, y);
title('Plane 2 position vs time');
xlabel('Time (h)');
ylabel('Distance (km)');
grid on;


figure(3);
fplot (@distance, [0,1]);
title('Distance between Plane 1 and Plane 2 vs time using fplot');
xlabel('Time (h)');
ylabel('Distance apart (km)');
grid on;


D = distance(t); 
figure(4);
plot (t, D);
title('Distance between Plane 1 and Plane 2 vs time using plot');
xlabel('Time (h)');
ylabel('Distance apart (km)');
grid on;

%Finding: 
%(1)Time when aircrafts are closest to each other and dist b/w them at 
%this time 

%create function f to avoid using @
f=@(t) distance(t);

fprintf('The aircrafts will come closest at %f hours.\n',fminbnd(f,0,1)); 
fprintf('At this time they will be %f km apart.\n',f(fminbnd(f,0,1)));

%(2)Time b/w t=20min and t=40min where the aircrafts a furthest apart and
%dist b/w them at this time

%inverse of distance to find the min value 
f2= @(t) 1/distance(t);

fprintf('The aircrafts will be furthest apart at %f minutes.\n',60*fminbnd(f2, 20/60, 40/60));
fprintf ('At this time they will be %f km apart.\n', f(fminbnd(f2, 20/60, 40/60)));

%(3) The four times during the 1st hour where the aircrafts are 100 km
%apart
%create function for root finding
f3=@(t) f(t) - 100;

fprintf ('The aircrafts will be 100 km apart at %f,%f,%f,and %f hours.\n', fzero(f3,[0,0.1]),fzero(f3,[0.3,0.4]),fzero(f3,[0.6,0.7]),fzero(f3,[0.9,1]));

%Table of distance b/w aircraft from t=0.4 to t=0.5 hours in steps of 0.01
%hours

fprintf('Time     Distance\n');

timeVector= 0.4:0.01:0.5;
    for x=timeVector
        fprintf ('%.3f    %.3f\n',x, f(x));
    end
end

%function plane1Position

function [x, y] = plane1Position (t)
% PLANE1POSITION Calculates position of plane #1 at a given time
% Inputs:  
%      t = time (in hours)
% Outputs:  
%      x = x position (in km)
%      y = y position (in km)

x0 = 200; 
y0 = 100; 
speed = 400;
angle = 210 * (pi / 180);  % counterclockwise from x axis, in radians

x = x0 + cos(angle) * speed * t;
y = y0 + sin(angle) * speed * t;

end

%function plane2Position 

function [x, y] = plane2Position (t)
% PLANE2POSITION Calculates position of plane #2 at a given time
% Inputs:  
%      t = time (in hours)
% Outputs:  
%      x = x position (in km)
%      y = y position (in km)

radius = 100; 
speed = 450;
angle = (speed * t) / radius;

x = cos(angle) * radius;
y = sin(angle) * radius;

end

%function distance 

function [ d ] = distance( t )
% DISTANCE  calculates the distance between the aircrafts 
% [ d ] = distance( t )
% Input: 
% t- time in seconds
% Output:
% d- distance between the aircrafts in km



[x1,y1] = plane1Position(t);
[x2,y2] = plane2Position(t);
    
d = sqrt((x1-x2).^2 + (y1-y2).^2);

end

%function time
function [ t ] = time( d )
%TIME calulates the time between 0.5 and 0.8 hours for a given distance 
%[ t ] = time( d )
%Input: d- distance between aircrafts in km.
%Output: t- time in hours 


    if d>distance(0.5) || d<distance(0.8)
      
     error 'ERROR: Invalid distance value' 
   
    end
f = @(t) distance(t) - d;
t = fzero(f,[0.5,0.8]);      
end


function [ x ] = calcX( F,mu,m,h )
%CALCX  calculates the distance along the ramp given F,mu,m and h
%[ x ] = calcX( F,mu,m,h )
%Inputs:
%F - the force in newtons
%mu - friction coeff
%m - mass of the block in kg
%h height of the pully in metres
%Outputs:
%x - distance along the ramp in metres

g=9.81;

if m<=0
    error('Error: Invalid input');
end  
%equation
f= @(x) (((mu*m*g*cosd(15))+(m*g*sind(15)))/(x+h))*(sqrt((x.^2)+(h^2)));
f2=@(x) f(x) -F;
x=fzero(f2,[0.01,10]);




end










