MATLAB: How to implement friction into the script

frictiong++gammalaunchrocketvelocity

clear;
close all;
clc;
tic
aT = 0;
ad = 0;
for i = [37:2:53]
r = 6371 * 10^3;
G = 6.674 * 10^-11;
M = 5.972 * 10^24;
g = (G * M)/(r^2);
theta0 = i;
ax = 0;
ay = r;
v0 = 3000;
vx0 = v0*cosd(theta0);
vy0 = v0*sind(theta0);
x = 0;
y = r;
vx = vx0;
vy = vy0;
T = 0;
dt = 0.01;
at = 0;
landed = 0;
z = 1;
while landed == 0
z = z + 1;
T = T + dt;
xo = x;
yo = y;
x = x + vx * dt;
y = y + vy * dt;
d = sqrt(x^2 + y^2);
alpha = atand(x/y);
g = (G*M)/(d^2);
gy = cosd(alpha) * g;
gx = sind(alpha) * g;
vy = vy - (gy * dt);
vx = vx - (gx * dt);
v = vx/sin(alpha);
ax = [ax, x];
ay = [ay, y];
if d < r
landed = 1;
end
end
aT = [aT, T];
distance = (alpha/360) * 2 * pi * r;
ad = [ad, distance];
fprintf('Checked for degree: %.0f\n', i)
end
toc
This is a model that I've created for calculating the distance a missile travels under the following conditions:
velocity = 2000 m / s
gravity = dependent on height
distance = influenced by curvature of the earth
friction = not present
Now, I am trying to implement friction into my script and I know that the friction force is related to the speed by a coefficient:
Ff = gamma * | v | * v
With Ff and v being vectors in the direction of the missile.
Could anyone please give me a hint to what I could do to implement this relation into the script.
I will then alternate gamma and compare the results of the script to get the best value for gamma.

Best Answer

Friction is a force that produces acceleration which changes the velocity, so if you add an additional term to the velocity update you may get the effect that you want to. Theoretically you would need to define the mass of the missile, but this can be controlled by the parameter gamma (both are constant, costant*constant = constant), in the end you will have something like:
gamma = 1e-5;
vy = vy - (gy * dt) - gamma*abs(vy)*vy*dt;
vx = vx - (gx * dt) - gamma*abs(vx)*vx*dt;