Created
June 12, 2017 00:02
-
-
Save josh-kaplan/10b9d6021646aad2a668f996e0a56e9a to your computer and use it in GitHub Desktop.
Bisection Method (More accurate).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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 | |
break | |
% Not close enough, adjust velocity | |
else | |
% Error is too big, try a lower velocity | |
if err > tolerance | |
hi = v; | |
% Not close enough to target value, increase velocity | |
else | |
lo = v; | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment