Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Programs-for-Nonlinear-Dynamics/raycaustic.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
151 lines (113 sloc)
2.66 KB
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Feb 16 16:44:42 2021 | |
raycaustic.py | |
@author: nolte | |
D. D. Nolte, Optical Interferometry for Biology and Medicine (Springer,2011) | |
""" | |
import numpy as np | |
from matplotlib import pyplot as plt | |
plt.close('all') | |
# model_case 1 = cosine | |
# model_case 2 = circle | |
# model_case 3 = square root | |
# model_case 4 = inverse power law | |
# model_case 5 = ellipse | |
# model_case 6 = secant | |
# model_case 7 = parabola | |
# model_case 8 = Cauchy | |
model_case = int(input('Input Model Case (1-7)')) | |
if model_case == 1: | |
model_title = 'cosine' | |
xleft = -np.pi | |
xright = np.pi | |
ybottom = -1 | |
ytop = 1.2 | |
elif model_case == 2: | |
model_title = 'circle' | |
xleft = -1 | |
xright = 1 | |
ybottom = -1 | |
ytop = .2 | |
elif model_case == 3: | |
model_title = 'square-root' | |
xleft = 0 | |
xright = 4 | |
ybottom = -2 | |
ytop = 2 | |
elif model_case == 4: | |
model_title = 'Inverse Power Law' | |
xleft = 1e-6 | |
xright = 4 | |
ybottom = 0 | |
ytop = 4 | |
elif model_case == 5: | |
model_title = 'ellipse' | |
a = 0.5 | |
b = 2 | |
xleft = -b | |
xright = b | |
ybottom = -a | |
ytop = 0.5*b**2/a | |
elif model_case == 6: | |
model_title = 'secant' | |
xleft = -np.pi/2 | |
xright = np.pi/2 | |
ybottom = 0.5 | |
ytop = 4 | |
elif model_case == 7: | |
model_title = 'Parabola' | |
xleft = -2 | |
xright = 2 | |
ybottom = 0 | |
ytop = 4 | |
elif model_case == 8: | |
model_title = 'Cauchy' | |
xleft = 0 | |
xright = 4 | |
ybottom = 0 | |
ytop = 4 | |
def feval(x): | |
if model_case == 1: | |
y = -np.cos(x) | |
elif model_case == 2: | |
y = -np.sqrt(1-x**2) | |
elif model_case == 3: | |
y = -np.sqrt(x) | |
elif model_case == 4: | |
y = x**(-0.75) | |
elif model_case == 5: | |
y = -a*np.sqrt(1-x**2/b**2) | |
elif model_case == 6: | |
y = 1.0/np.cos(x) | |
elif model_case == 7: | |
y = 0.5*x**2 | |
elif model_case == 8: | |
y = 1/(1 + x**2) | |
return y | |
xx = np.arange(xleft,xright,0.01) | |
yy = feval(xx) | |
lines = plt.plot(xx,yy) | |
plt.xlim(xleft, xright) | |
plt.ylim(ybottom, ytop) | |
delx = 0.001 | |
N = 75 | |
for i in range(N+1): | |
x = xleft + (xright-xleft)*(i-1)/N | |
val = feval(x) | |
valp = feval(x+delx/2) | |
valm = feval(x-delx/2) | |
deriv = (valp-valm)/delx | |
phi = np.arctan(deriv) | |
slope = np.tan(np.pi/2 + 2*phi) | |
if np.abs(deriv) < 1: | |
xf = (ytop-val+slope*x)/slope; | |
yf = ytop; | |
else: | |
xf = (ybottom-val+slope*x)/slope; | |
yf = ybottom; | |
plt.plot([x, x],[ytop, val],linewidth = 0.5) | |
plt.plot([x, xf],[val, yf],linewidth = 0.5) | |
plt.gca().set_aspect('equal', adjustable='box') | |
plt.show() | |