Working on a function that will determine the angle needed to launch a projectile at in order to hit a target, works like a newton solver. I have a script to run the function
for theta=0:0.1:pi V_int=380; % (m/s)
target=400; % (meters)
dt=0.1; % (seconds)
THETA=missile(V_int,theta,dt); fprintf('Angle needed to travel 400 meters is %3.1f radians\n', THETA);end
Then I have a function that calculates the distance travled over a range of angles to ensure that the projectile will be within range of the target
function [x,y]=projectile(V_int,theta,dt)time = dt; %(seconds)
k = 0.32; %drag coefficient (given)
g = -9.81; %(m/s^2)
%First time step in x direction
x(1) = 0; %initial x position (meters)
v_int_x = V_int*cos(theta); %initial velocity in x direction (m/s)
a_x = -v_int_x*k; %initial acceleration in x direction (m/s^2)
x(2) = x(1)+v_int_x*time+0.5*a_x*(time^2); %second position in x direction (meters)
v_fin_x = v_int_x+a_x*time; %(m/s)
%First time step in y direction
y(1) = 0; %initial y postision (meters)
v_int_y = V_int*sin(theta); %initial velocity in y direction (m/s)
a_y = g-v_int_y*k; %initial acceleration in y direction (m/s^2)
y(2)=y(1)+v_int_y*time+0.5*a_x*(time^2); %second y position (meters)
v_fin_y=v_int_y+a_y*time; %second velocity in y direction (m/s)
time=time+dt; %time step added to time already elapsed to give total time
i=3; %counter
while y(i-1)>=0 %calculates x values
a_x=-v_fin_x*k; %acceleration in x direction for each time step (m/s^2)
x(i)=x(i-1)+v_fin_x*time+0.5*a_x*(time^2); %x position for each time step (meters)
v_fin_x=v_fin_x+a_x*time; %velocity in x direction for each time step (m/s)
%calculates y values
a_y=g-v_fin_y*k; %acceleration in y direction for each time step (m/s^2)
y(i)=y(i-1)+v_fin_y*time+0.5*a_y*(time^2); %position in y direction for each time step (meters)
v_fin_y=v_fin_y+a_y*time; %velocity in y direction for each time step (m/s)
%change counter and timestep to continue loop
i=i+1; %updates placement of next series of numbers for vectors
time=time+dt; %updates total time elapsed (seconds)
endend
And now I am trying to finish the function that will determine the angle needed for the given target
function THETA=missile(V_int,theta,dt) % Given initial velocity, theta, change in theta and a time step,
% function will determine the change in horizontal distance.
for theta=0:0.1:pi %(radians)
x=projectile(V_int,theta,dt);end%determine the angle that will give the maximum distance travled
index=(find(x==max(x))); % Finds largest distance traveled fo all values theta
max_theta=theta(index); % Max theta used to go furthest distance
x_distance_1=max(abs(x)); % Max distance traveled
theta_1=max_theta; % Max theta will become first theta used
dist_from_target=x_distance_1-target; % Determines how far from target the missile landed
theta=theta_1-0.1; %changes theta so function can be run again and we can determine change_in_theta
x=projectile(V_int,theta);change_in_x=x-x_distance_1;change_in_theta=(d_theta/change_in_x)*dist_from_target;error=change_in_x; while error>=2 || error<=-2 % Loop will run as long as the error is more that 2 meters in either direction
x=projectile(V_int,theta,dt); dist_from_target=x-target; change_in_theta=(d_theta/change_in_x)*dist_from_target; theta=theta+change_in_theta; THETA=theta;endend
I am getting an error in line 12 of this last function,
dist_from_target=x_distance_1-target; % Determines how far from target the missile landed
I'm guessing it has something to do with the naming of the variable between the two functions? But I am not getting a reason for the error when it pops up in the command window.
Best Answer