Spacecraft Control Framework 1.0
Spacecraft Control Library
Functions
sc_math.h File Reference

Math utility functions. More...

Go to the source code of this file.

Functions

double golden_section (double RHS(double x, void *context), double a, double b, double tol, int maxIts, void *context)
 Golden section minimization. More...
 
ml_matrix rk0 (ml_matrix RHS(ml_matrix x, double t, void *context), ml_matrix &x, double h, double t, void *context)
 0th order Runge-Kutta integration algorithm with time as a RHS input More...
 
ml_matrix rk4 (ml_matrix RHS(ml_matrix x, double t), ml_matrix &x, double h, double t)
 4th order Runge-Kutta integration algorithm with time as a RHS input More...
 
ml_matrix rk4 (ml_matrix RHS(ml_matrix x, double t, void *context), ml_matrix &x, double h, double t, void *context)
 4th order Runge-Kutta integration algorithm with time as a RHS input and a context pointer
 
ml_matrix rk4 (ml_matrix RHS(ml_matrix &x, ml_matrix &a), ml_matrix &x, double h, ml_matrix a)
 4th order Runge-Kutta integration algorithm with a matrix input to the RHS
 
double pss_rem (double x, double y)
 Remainder function. More...
 
double fix (double x)
 Round to 0. More...
 
double r2p5 (double x)
 Round to nearest 0.5. More...
 
int sct_sign (double x)
 Determine sign of number and return unitary value. More...
 
double sct_max (double a, double b)
 Find maximum of two numbers. More...
 
double unwrap (double angle)
 Unwrap radian values to (-pi,pi) interval. More...
 
ml_matrix intersect_line_ellipsoid (const ml_matrix &p, const ml_matrix &u, const ml_matrix &e)
 Find the intersection between a line and an ellipsoid. More...
 
ml_matrix intersect_line_sphere (const ml_matrix &p, const ml_matrix &u, double r)
 Find the intersection between a line and an sphere. More...
 
ml_matrix p_gauss (int nMax, int mMax, double theta, ml_matrix &dp)
 Computes the Gaussian form of the Legendre functions and the first derivatives. More...
 
void s_c_harm (const ml_matrix &a, int n, ml_matrix &s, ml_matrix &c)
 Generate a series of sine and cosine harmonics. More...
 
void c_to_d_zoh (const ml_matrix &a, const ml_matrix &b, double dT, ml_matrix &aD, ml_matrix &bD)
 Compute a discrete-time linear system from a continuous system. More...
 
ml_matrix expm (const ml_matrix &a)
 Compute the matrix exponential (of a square matrix only) using a power series expansion. More...
 
double WrapPhase (double angle)
 Wrap a phase angle to keep its value between -pi and +pi. More...
 
ml_matrix WrapPhase (ml_matrix angle)
 Wrap a vector of phase angles to keep their values between -pi and +pi. More...
 
ml_matrix UnwrapPhase (ml_matrix theta)
 Unwrap a vector of angular values so they change continuously instead of wrapping. More...
 
ml_matrix diff (ml_matrix a)
 Compute the difference between successive elements of a 1-D vector. More...
 
ml_matrix delt_circ (ml_matrix x, ml_matrix y)
 Compute a delta in circular coordinates. More...
 
ml_matrix circle_fit (ml_matrix x, ml_matrix y)
 Compute a delta in circular coordinates. More...
 
double factorial (double n)
 Compute a factorial.
 
double interp_2d (ml_matrix a, ml_matrix x, ml_matrix y, double xK, double yK)
 2D interpolation
 
double newton_raphson (double f(double x, void *context), double df(double x, void *context), double x0, double tol, int nMax, void *context)
 1 dimensional root finder
 
double secant (double RHS(double x, void *context), double x0, double x00, double tol, int nMax, void *context)
 1 dimensional root finder
 
ml_matrix average_unit_vector (ml_matrix u1, ml_matrix u2)
 Average unit vector.
 
ml_matrix pinv (ml_matrix a)
 Pseudo inverse. More...
 
ml_matrix downhill_simplex (double RHS(const ml_matrix &x, void *context), ml_matrix &x, ml_matrix &options, void *context)
 Downhill simplex. More...
 
ml_matrix orthogonalize (ml_matrix a)
 Orthogonalize. More...
 
ml_matrix logistic (ml_matrix x)
 Logistic function. More...
 
double logistic (double x)
 Logistic function. More...
 
double interp_extrap_1d (ml_matrix a, ml_matrix x, double xK)
 1D interpolation and extrapolation More...
 
double gaussian_multivariate_density (ml_matrix y, ml_matrix y_bar, ml_matrix P)
 Gaussian Mulivariate Normal Distribution function. More...
 
ml_matrix roots_second_order (double a, double b, double c)
 Roots second order. More...
 
ml_matrix roots_second_order (const ml_matrix &p)
 Roots second order. More...
 
ml_matrix skew_symmetric (const ml_matrix &p)
 Generate a skew symmetric matrix. More...
 
double trig_reduction (double a, double b, double c)
 Solve for beta: a*sin(beta)+b*cos(beta) = c. More...
 
ml_matrix m_k_3_by_3 (const ml_matrix &m, int k)
 Returns a 3x3 matrix from a 3x3*n matrix. More...
 
int intersect_line_sphere_true (const ml_matrix &p1, const ml_matrix &p2, double r)
 Find if a line intersects a sphere. More...
 
ml_matrix append_diagonal (const ml_matrix &a, const ml_matrix &b)
 Append a diagonal matrix to another matrix. More...
 
double trapezoidal_integration (double Integrand(double x, void *context), double a, double b, int n, void *context)
 Trapezoidal integration. More...
 
double two_dimensional_integration (ml_matrix Integrand(const ml_matrix &x, const ml_matrix &y, void *context), double a, double b, double c, double d, int nX, int nY, void *context)
 Two dimensional integration. More...
 

Detailed Description

Math utility functions.

Function Documentation

◆ golden_section()

double golden_section ( double   RHSdouble x, void *context,
double  a,
double  b,
double  tol,
int  maxIts,
void *  context 
)

Golden section minimization.

Parameters
RHSRight-hand side function definition
aLeft range
bRight range
tolTolerance
maxItsMaximum number of interations
*contextto data from RSH
Returns
minimum value of x

◆ rk0()

ml_matrix rk0 ( ml_matrix   RHSml_matrix x, double t, void *context,
ml_matrix &  x,
double  h,
double  t,
void *  context 
)

0th order Runge-Kutta integration algorithm with time as a RHS input

0th order Runge-Kutta integration algorithm with time as a RHS input

RHS actually does the propagation

Parameters
RHSRight-hand side function definition
xCurrent state
hIntegration time step
tCurrent time
*contextPointer to data to be passed to RHS
Returns
Derivative of x

◆ rk4()

ml_matrix rk4 ( ml_matrix   RHSml_matrix x, double t,
ml_matrix &  x,
double  h,
double  t 
)

4th order Runge-Kutta integration algorithm with time as a RHS input

4th order Runge-Kutta integration algorithm with time as a RHS input

Parameters
RHSRight-hand side function definition
xCurrent state
hIntegration time step
tCurrent time
Returns
Derivative of x

◆ pss_rem()

double pss_rem ( double  x,
double  y 
)

Remainder function.

Remainder function.

Parameters
xNumerator
yDenominator
Returns
Remainder from the division of y into x

Referenced by delt_circ(), and r2p5().

◆ fix()

double fix ( double  x)

Round to 0.

Round to 0.

Parameters
xNumber to be rounded
Returns
Number rounded to 0

Referenced by jd_to_date().

◆ r2p5()

double r2p5 ( double  x)

Round to nearest 0.5.

Round to nearest 0.5.

Parameters
xNumber to be rounded
Returns
Number rounded to the nearest "point 5"

References pss_rem().

Referenced by jd_to_date(), and mean_sidereal_day().

◆ sct_sign()

int sct_sign ( double  x)

Determine sign of number and return unitary value.

Determine sign of number and return unitary value.

Parameters
xNumber
Returns
Unitary value, i.e. 1 or -1

Referenced by ImpulsiveManeuver(), InPlane(), OutOfPlane(), and Target().

◆ sct_max()

double sct_max ( double  a,
double  b 
)

Find maximum of two numbers.

Find maximum of two numbers.

Parameters
aFirst number
bSecond number
Returns
Maximum of a and b

◆ unwrap()

double unwrap ( double  angle)

Unwrap radian values to (-pi,pi) interval.

Unwrap radian values to (-pi,pi) interval.

Parameters
angleAngle in radians
Returns
Unwrapped angle

References PI, and TWO_PI.

Referenced by PID3Axis::Update().

◆ intersect_line_ellipsoid()

ml_matrix intersect_line_ellipsoid ( const ml_matrix &  p,
const ml_matrix &  u,
const ml_matrix &  e 
)

Find the intersection between a line and an ellipsoid.

Always returns the closest point. The line is defined by a point and unit vectors. Returns empty if there is no intersection

Parameters
p(3,1) Point
u(3,1) Unit vector
e(3,1) (a,b,c) semi-axes of ellipsoid
Returns
Intersection point

Referenced by intersect_line_sphere().

◆ intersect_line_sphere()

ml_matrix intersect_line_sphere ( const ml_matrix &  p,
const ml_matrix &  u,
double  r 
)

Find the intersection between a line and an sphere.

Find the intersection between a line and an sphere.

Always returns the closest point. The line is defined by a point and unit vectors.

Parameters
p(3,1) Point
u(3,1) Unit vector
r(1,1) Radius of sphere
Returns
Intersection point

References intersect_line_ellipsoid().

◆ p_gauss()

ml_matrix p_gauss ( int  nMax,
int  mMax,
double  theta,
ml_matrix &  dp 
)

Computes the Gaussian form of the Legendre functions and the first derivatives.

Computes the Gaussian form of the Legendre functions and the first derivatives.

Because there is no zero indexing in MATLAB, the P's are offset in the p matrix as follows:

n,m p(n+1,m+1) = P

MATLAB Form: [p, dP] = PGauss( nMax, mMax, theta )


References: Wertz, J., Spacecraft Attitude Determination and Control, Kluwer, 1976, pp. 781.

Parameters
nMaxmax value of first index
mMaxmax value of second index
thetainput value (usually an angle in rads)
dpMatrix for derivative outputs dp(n,m) / d(theta)
Returns
p Gauss functions, P(n,m)

Referenced by mag_field().

◆ s_c_harm()

void s_c_harm ( const ml_matrix &  a,
int  n,
ml_matrix &  s,
ml_matrix &  c 
)

Generate a series of sine and cosine harmonics.

Generate a series of sine and cosine harmonics.

Parameters
aColumn vector (m,1) argument, in radians
nNumber of harmonics
sVector of sine harmonics output
cVector of cosine harmonics output

Referenced by mag_field().

◆ c_to_d_zoh()

void c_to_d_zoh ( const ml_matrix &  a,
const ml_matrix &  b,
double  dT,
ml_matrix &  f,
ml_matrix &  g 
)

Compute a discrete-time linear system from a continuous system.

Compute a discrete-time linear system from a continuous system.

Parameters
aContinuous matrix
bContinuous matrix
dTTime step (sec)
fDiscrete matrix
gDiscrete matrix

References expm().

Referenced by FFEccLinOrb(), LPCircular(), LPEccentricGVE(), and MonitoringAlg().

◆ expm()

ml_matrix expm ( const ml_matrix &  a)

Compute the matrix exponential (of a square matrix only) using a power series expansion.

Parameters
aSquare matrix
Returns
b Matrix exponential, b = exp(a)

Referenced by c_to_d_zoh().

◆ WrapPhase() [1/2]

double WrapPhase ( double  angle)

Wrap a phase angle to keep its value between -pi and +pi.

*sctlib

Parameters
anglePhase angle [rad]
Returns
Phase angle wrapped between -PI and PI [rad]

References PI, and TWO_PI.

Referenced by add_elements(), CirclePhase(), El2Alfriend(), EllipsePhase(), FFEccGoals(), FFEccHills2Goals(), FFEccXExt(), FFEccYExt(), FFEccZExt(), GenerateTeamGoals(), InPlane(), MeanAnom2TrueLat(), MonitoringAlg(), OrbElemDiff(), orbit_acquisition_dv(), sub_elements(), and TargetTrueAnom().

◆ WrapPhase() [2/2]

ml_matrix WrapPhase ( ml_matrix  angle)

Wrap a vector of phase angles to keep their values between -pi and +pi.

*sctlib

Parameters
angleVector of phase angles with no limits. [rad]
Returns
wrapped Vector of phase angles wrapped between -PI and +PI. [rad]

References PI, and TWO_PI.

◆ UnwrapPhase()

ml_matrix UnwrapPhase ( ml_matrix  angle)

Unwrap a vector of angular values so they change continuously instead of wrapping.

*sctlib

Parameters
angleVector of angular values. [rad]
Returns
unwrapped New vector with jumps larger than 2*PI in magnitude removed so that it changes "continuously". [rad]

References diff(), PI, and TWO_PI.

Referenced by DeltaEl2AlfriendMat(), FFEccDiscreteHills(), GenerateTimeVector(), ImpulsiveLPManeuver(), LPEccentric(), LPEccentricGVE(), and MonitoringAlg().

◆ diff()

ml_matrix diff ( ml_matrix  a)

Compute the difference between successive elements of a 1-D vector.

Compute the difference between successive elements of a 1-D vector.

Parameters
a1-dimensional matrix, length N
Returns
b 1-dimensional matrix, ith element is a(i+1)-a(i), length N-1

Referenced by AccelVector2ManeuverStruct(), DeltaEl2AlfriendMat(), FFEccDiscreteHills(), FFEccEstimateCost(), FFEccXExt(), FFEccYExt(), FFEccZExt(), ImpulsiveLPManeuver(), LPEccentricGVE(), LPEccentricTimeWeight(), MonitoringAlg(), NearestOffset(), OptimalAssignment(), and UnwrapPhase().

◆ delt_circ()

ml_matrix delt_circ ( ml_matrix  x,
ml_matrix  y 
)

Compute a delta in circular coordinates.

Compute a delta in circular coordinates.

It does not check dimensions.

Parameters
x2-dimensional matrix
y2-dimensional matrix
Returns
z 2-dimensional matrix

References PI, and pss_rem().

◆ circle_fit()

ml_matrix circle_fit ( ml_matrix  x,
ml_matrix  y 
)

Compute a delta in circular coordinates.

Compute a delta in circular coordinates.

Parameters
xcolumn matrix
ycolumn matrix
Returns
s column matrix [a;b;r]

◆ pinv()

ml_matrix pinv ( ml_matrix  a)

Pseudo inverse.

Pseudo inverse.

Parameters
amatrix
Returns
orthogonal matrix

Referenced by az_el_range_range_rate_to_rv(), camera_calibration(), momentum_unloading(), and q_from_u().

◆ downhill_simplex()

ml_matrix downhill_simplex ( double   RHSconst ml_matrix &x, void *context,
ml_matrix &  x,
ml_matrix &  options,
void *  context 
)

Downhill simplex.

Parameters
RHSFunction to minimize
xInitial guess
optionsOptions
contextContext pointer to pass to RHS

Referenced by fit_PSF_to_data().

◆ orthogonalize()

ml_matrix orthogonalize ( ml_matrix  a)

Orthogonalize.

Orthogonalize.

Orthogonalize a 3x3 matrix

Parameters
amatrix
Returns
orthogonal matrix

Referenced by camera_calibration(), and q_from_u().

◆ logistic() [1/2]

ml_matrix logistic ( ml_matrix  x)

Logistic function.

Parameters
xinput
Returns
1/(1+exp(-x))

◆ logistic() [2/2]

double logistic ( double  x)

Logistic function.

Parameters
xinput
Returns
1/(1+exp(-x))

◆ interp_extrap_1d()

double interp_extrap_1d ( ml_matrix  a,
ml_matrix  x,
double  xK 
)

1D interpolation and extrapolation

1D interpolation and extrapolation

Parameters
aarray of values
xcorresponding x value
xKvalue of interest
Returns
value

◆ gaussian_multivariate_density()

double gaussian_multivariate_density ( ml_matrix  y,
ml_matrix  y_bar,
ml_matrix  P 
)

Gaussian Mulivariate Normal Distribution function.

Gaussian Mulivariate Normal Distribution function.

Parameters
yarray of parameters
y_barcorresponding means
Pcovariance matrix
Returns
likelihood

◆ roots_second_order() [1/2]

ml_matrix roots_second_order ( double  a,
double  b,
double  c 
)

Roots second order.

Roots second order.

Parameters
aCoefficient
bCoefficient
cCoefficient
Returns
roots

Referenced by roots_second_order().

◆ roots_second_order() [2/2]

ml_matrix roots_second_order ( const ml_matrix &  p)

Roots second order.

Roots second order.

Parameters
parray of coefficients
Returns
roots

References roots_second_order().

◆ skew_symmetric()

ml_matrix skew_symmetric ( const ml_matrix &  v)

Generate a skew symmetric matrix.

Generate a skew symmetric matrix.

Parameters
vVector (3x1)
Returns
skew symmetric matrix

Referenced by attitude_propagation_discrete(), discrete_state_transition_matrix(), multi_body_central_hub_with_rwa::state_derivative(), and multi_body_central_hub::state_derivative().

◆ trig_reduction()

double trig_reduction ( double  a,
double  b,
double  c 
)

Solve for beta: a*sin(beta)+b*cos(beta) = c.

Parameters
aCoefficient
bCoefficient
cRHS
Returns
roots

◆ m_k_3_by_3()

ml_matrix m_k_3_by_3 ( const ml_matrix &  m,
int  k 
)

Returns a 3x3 matrix from a 3x3*n matrix.

Parameters
m3x3*n matrix
kindex
Returns
3x3 matrix

Referenced by multi_body_central_hub_with_rwa::state_derivative(), and multi_body_central_hub::state_derivative().

◆ intersect_line_sphere_true()

int intersect_line_sphere_true ( const ml_matrix &  p1,
const ml_matrix &  p2,
double  r 
)

Find if a line intersects a sphere.

Find if a line intersects a sphere.

Parameters
p13x1 origin point on the sphere
p23x1 final point off the sphere
rRadius of sphere
Returns
3x3 matrix

◆ append_diagonal()

ml_matrix append_diagonal ( const ml_matrix &  a,
const ml_matrix &  b 
)

Append a diagonal matrix to another matrix.

Append a diagonal matrix to another matrix.

Parameters
asquare matrix
bdiagonal of a matrix
Returns
Combined matrix

◆ trapezoidal_integration()

double trapezoidal_integration ( double   Integranddouble x, void *context,
double  a,
double  b,
int  n,
void *  context 
)

Trapezoidal integration.

Trapezoidal integration.

Referenced by rhs_airy_defocused_psf().

◆ two_dimensional_integration()

double two_dimensional_integration ( ml_matrix   Integrandconst ml_matrix &x, const ml_matrix &y, void *context,
double  a,
double  b,
double  c,
double  d,
int  nX,
int  nY,
void *  context 
)

Two dimensional integration.

Two dimensional integration.

Parameters
Integrandpointer to the integrand function
aMinimum x
bMaximum x
cMinimum y
dMaximum y
nXNumber of divisions in x
nYNumber of divisions in y
*contextPointer to data to pass to Integrand
Returns
Integral value

Referenced by imager_psf(), and imager_psf_uint16().