Skip to content

Instantly share code, notes, and snippets.

@josh-kaplan
Last active June 11, 2017 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josh-kaplan/37668544f9263351322bf759adfe6936 to your computer and use it in GitHub Desktop.
Save josh-kaplan/37668544f9263351322bf759adfe6936 to your computer and use it in GitHub Desktop.
Bisection Method. This will work with either MATLAB or Octave. Used to determine the maximum speed at which kinetic energy can be described by the Newtonian equation with an error with a specified tolerance of relativistic energy.
%% Newtonian vs. Relativistic Energy Accuracy
% Determines the maximum speed a particle can have such that its
% kinetic energy described as 0.5*m*v^2 with an error of no greater
% than <tolerance>.
close all; clear all; clc;
% Input - The tolerance of the accuracy of the Newtonian kinetic energy
% compared to the relativistic energy.
tolerance = .005;
% Speed of light [m/s]
c = 299792458;
% Bisection parameters
maxiters = 100;
hi = c;
lo = 0;
% How accurate we are in our calculation.
% Not to be confused with tolerance of energy calculation (aka error).
TOL = .0001;
% Use bisection to determine max velocity to keep energy within
% the specified tolerance.
for i=1:maxiters
% "Guess" the velocity
v = (hi - lo) / 2 + lo;
% Calculate our energies
g = (1 - v^2/c^2)^(-1/2); % Gamma
K = 0.5*v^2; % Newtonian kinetic energy per unit mass
E = g*c^2 - c^2; % Relativistic kinetic energy per unit mass
% Calculate energy error
err = abs(K - E) / E;
fprintf('%4d: %14.4f %8.6fc %10.8f\n', i, v, v/c, err)
% Error is too big, try a lower velocity
if err > tolerance
hi = v;
% Error is within tolerance
else
% Not close enough to target value, increase velocity
if abs(err - tolerance) > TOL
lo = v;
% Error is within accepted tolerance, we're done.
else
break
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment