Instantly share code, notes, and snippets.

What would you like to do?
Bisection Method (More accurate).
%% 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 = 1000;
hi = c;
lo = 0;
v = -1;
% How accurate we are in our calculation.
% Not to be confused with tolerance of energy calculation (aka error).
TOL = 1e-4;
% Use bisection to determine max velocity to keep energy within
% the specified tolerance.
for i=1:maxiters
% "Guess" the velocity
v_prev = v;
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 %6.4f %10.4e %10.4e %10.8f\n', i, v, g, E, K, err)
fprintf('%4d: %14.4f %8.6fc %10.8f\n', i, v, v/c, err)
% Velocity is "close enough"
if abs(v - v_prev) < TOL
% Not close enough, adjust velocity
% Error is too big, try a lower velocity
if err > tolerance
hi = v;
% Not close enough to target value, increase velocity
lo = v;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment