simulate an object thrown with drag force in matlab

To simulate an object thrown with drag force in MATLAB, you can use numerical integration. The force acting on the object can be decomposed into the sum of the gravitational force and the drag force. Assuming the drag force is proportional to the square of the velocity, the equation of motion can be written as:

main.m
m * d^2r/dt^2 = -m * g - k * |dr/dt| * dr/dt
45 chars
2 lines

where m is the mass of the object, g is the gravitational acceleration, k is the drag coefficient, and r is the position vector of the object.

You can use the built-in ODE solver ode45 to solve this differential equation, by defining a function that returns the derivative of the position vector. Here is an example script that simulates the motion of an object thrown at an angle:

main.m
clear all;
close all;

% parameters
m = 0.1;        % mass (kg)
g = 9.81;       % gravitational acceleration (m/s^2)
k = 0.1;        % drag coefficient
v0 = 10;        % initial velocity (m/s)
theta = 45;     % angle of projection (degrees)
tspan = [0, 10]; % time span of simulation (s)

% initial conditions
x0 = 0;
y0 = 0;
vx0 = v0*cosd(theta);
vy0 = v0*sind(theta);
z0 = [x0; y0; vx0; vy0];

% function to compute derivative
f = @(t,z) [z(3); z(4); -k/m * norm(z(3:4)) * z(3); -g - k/m * norm(z(3:4)) * z(4)];

% solve differential equation
[t,z] = ode45(f, tspan, z0);

% plot trajectory
figure;
plot(z(:,1), z(:,2));
xlabel('x (m)');
ylabel('y (m)');
title('Projectile motion with drag force');
701 chars
31 lines

This script defines the parameters and initial conditions of the simulation, and then defines the function f that computes the derivatives of the position vector. The ODE solver ode45 is used to solve this differential equation over the time span tspan, and the resulting trajectory is plotted.

gistlibby LogSnag