Solve nonlinear equations
Solve one nonlinear algebraic equation.
To numerically solve the equation  x^2 - cos(x) = 0,  type
Variable x Solve( x^2 - cos(x) = 0, x = 0.2 ) % x = 0.2 is a guess.
As shown in the plot to the right, this nonlinear equation has two solutions. In general, nonlinear equations have an unknown number of solutions (0, 1, 2, 9, ...). The Solve command frequently converges to a solution close to the starting guess.
If you guess   x = 0.2,     Solves to:     x =  0.82413
If you guess   x = -9,     Solves to:     x = -0.82413
To save commands (for subsequent re-use) and commands/responses, type
Save SolveNonlinearEqn.txt % Save commands. Save SolveNonlinearEqn.html % Save commands and responses.

Solve sets of nonlinear algebraic equations.
Equation for circle:  x2  +  y2  =  R2
Equation for sine curve:  y  =  A * sin(x)
To numerically solve the previous set of nonlinear equations for x and y, type
Constant R = 1 meter, A = 1 meter Variable x, y eqns[1] = x^2 + y^2 - R^2 % Circle eqns[2] = y - A*sin(x) % Sine wave Solve( eqns = 0, x = 3, y = 5 )
Note: The arguments  x = 3  and  y = 5  are a guess for the solution. The two solutions to these nonlinear equations depend on your guess, e.g.,
Guess   x = 3,   y = 5,     ->       x = 0.7391,   y = 0.6736
Guess   x = -2,   y = -2,     ->       x = -0.7391,   y = -0.6736
Program commands: SolveSampleNonlinearEqns.txt
Program responses: SolveSampleNonlinearEqns.html

Continuous solution of one nonlinear algebraic equation.
y4 - 8*y - 0.3*t2 - 9*sin(t) = 0     0 ≤ t ≤ 20

Continuous solution of two coupled nonlinear algebraic equations for qA(t) and qB(t) for 0 ≤ t ≤ 2.2
cos(qA) + 2*cos(qB)  = 2 + 0.2*cos(t)
2*sin(qB) - sin(qA)  = 0.3*sin(3*t)

Continuous solution of two coupled nonlinear algebraic equations for r(t) and θ(t) for 0 ≤ t ≤ 7.
x = r*cos(θ)   where   x = cos(1.2*t)
y = r*sin(θ)   where   y = sin(t)

Advanced: Coding sets of nonlinear algebraic equations.
Equation for circle:  x2  +  y2  =  R2
Equation for sine curve:  y  =  A * sin(x)
To write a MATLAB® code to numerically solve for x and y for  1 <= R <= 3,  type
Constant R, A = 1 meter Variable x, y eqns[1] = x^2 + y^2 - R^2 % Circle eqns[2] = y - A*sin(x) % Sine wave Input x = 3 meters, y = 3 meters Output t seconds, x meters, y meters Nonlinear(eqns=0, x, y) [R = 1, 3, 0.5] CodeNonlinearEqns.m
MotionGenesis™ produces highly efficient and symbolically optimized codes.
C code
CodeNonlinearEqns.c
Compile and link source code.
Modify input values in CodeNonlinearEqns.in
Fortran code
CodeNonlinearEqns.f
Compile and link source code.
Modify input values in CodeNonlinearEqns.in
MATLAB®
CodeNonlinearEqns.m
Invoke MATLAB® and type CodeNonlinearEqns
Modify input values in CodeNonlinearEqns.m
Running the MotionGenesis™-generated .m file produces, e.g.,
% FILE: CodeNonlinear.1
% Residual     R         x          y
% (UNITS)   (meters)  (meters)   (meters)
 1.221E-15  1.00E+00  7.391E-01  6.736E-01 
 1.177E-14  1.50E+00  1.181E+00  9.249E-01 
 3.483E-13  2.00E+00  1.740E+00  9.857E-01 
 2.209E-13  2.50E+00  2.409E+00  6.689E-01 
 4.656E-15  3.00E+00  2.997E+00  1.446E-01