%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Projectile Motion Lab  MUZ 8/23/2017 (Plotting & Optimization)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
clear all; close all
%% Modeling & Plotting Projectile Motion
%Setting initial variables
v_0 = 25; % meters/second
theta = 60; % degrees

t=0:0.1:20; % time range for calculation (seconds)

% X-component
v_0x = v_0*cosd(theta); % calculating x component of initial velocity
a_x = 0;                % setting x component of acceleration

d_x = v_0x.*t + 0.5*a_x.*t.^2;    % calculating x positions

% Y-component
v_0y = v_0*sind(theta); % calculating y component of initial velocity
a_y = -9.8;             % setting y component of acceleration in m/s^2

d_y = v_0y.*t + 0.5*a_y.*t.^2;    % calculating y positions

% Zeroing points with negative y positions
 d_x(d_y<0)=0;
 d_y(d_y<0)=0;

% Plotting final results
figure(1)
plot(d_x,d_y)
    xlabel('X position (m)')
    ylabel('Y position (m)')
        ax = gca; 
        ax.FontSize = 18;

% All of this can be saved as a function like ProjectileMotion.m

%% Changing One Initial Parameter at a time (intro to for-loops)
v0_vec = 1:5:25; % setting a range of initial velocities
theta = 45;

%looping through the range of initial velocities
for i = 1:length(v0_vec)
        figure(2)
        hold on
        v_0 = v0_vec(i); % setting v_0 each time
        
        [d_x,d_y]=ProjectileMotion_fn(v_0,theta);
        %axis([0 1800 0 500])
end
title('Varying v_0')
    ax = gca; 
    ax.FontSize = 18;


% Now we'll vary the initial angle
v0 = 25;
theta_vec = 0:5:90;

for i = 1:length(theta_vec)
        figure(3)
        hold on
        theta = theta_vec(i);
        
        [d_x,d_y]=ProjectileMotion_fn(v_0,theta);
end
title('Varying Theta')
    ax = gca; 
    ax.FontSize = 18;

%% Changing Two Parameters & Storing Data in a Matrix
% setting a range for both initial parameters
v0_vec = 1:5:25;
theta_vec = 0:5:90;

% Nested for-loop
for i = 1:length(v0_vec)
    for j = 1:length(theta_vec)
        figure(4)
        hold on
        v_0 = v0_vec(i);
        theta = theta_vec(j);
        
        [d_x,d_y]=ProjectileMotion_fn(v_0,theta);
        
        %Calculating and storing maximum height and range value matrices
        Height(i,j)=max(d_y);
        Range(i,j)=max(d_x);
    end
end
title('Varying v_0 & Theta')
    ax = gca; 
    ax.FontSize = 18;


%% Optimization (data/matrix visualization via imagesc)

figure(5)
subplot(2,1,1)
Range(Height>10)=0; %zeroing heights greater than 10m
Height(Height>10)=0; %zeroing heights greater than 10m

imagesc(Height); colorbar; title('Height (m)'); axis xy;
    xlabel('Theta (degrees)'); ylabel('Velocity (m/s)')
    set(gca,'xTickLabel',[20 45 70],'yTickLabel',[20 40 60 80 100])
    ax = gca; 
    ax.FontSize = 18;
    
subplot(2,1,2)
imagesc(Range); colorbar; title('Range (m)'); axis xy;
    xlabel('Theta (degrees)'); ylabel('Velocity (m/s)')
    set(gca,'xTickLabel',[20 45 70],'yTickLabel',[20 40 60 80 100])
    ax = gca; 
    ax.FontSize = 18;