Skip to content

Instantly share code, notes, and snippets.

@josh-kaplan
Created June 11, 2017 23:48
Show Gist options
  • Save josh-kaplan/adc0155f3e86762815a2b6d86ce311a5 to your computer and use it in GitHub Desktop.
Save josh-kaplan/adc0155f3e86762815a2b6d86ce311a5 to your computer and use it in GitHub Desktop.
Newton's 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 - Using Newton's Method
% 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;
% Newton's method
maxiters = 1e5;
TOL = 1e-4;
guess = .5*c;
% Use Newton's method to solve
for i=1:maxiters
% "Guess" the velocity
v = guess;
% 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
% Newton's method
f = abs( (E - K) / E ) - tolerance;
df = v / E;
guess = v - f/df;
fprintf('%4d: %14.4f %14.4f %8.6fc %10.8f\n', i, guess, v, v/c, f + .005)
% If close enough, break
if abs(guess - v) < TOL
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment