DMelt:JMathlab/Equations

From HandWiki
Member

Equations

Systems of Linear Equations

Solving systems of linear equations is accomplished by either the function linsolve(A,b) (all versions of Jasymca), or linsolve2(A,b) (LAPACK, not in applet and midlet). In both cases A is the quadratic matrix of the system of equations, and b a (row or column) vector representing the right-hand-side of the equations. The equations may be written as $A\cdot z = b$ and we solve for $z$.

>> A=[2 3 1; 4 4 5; 2 9 3];
>> b=[0;3;1];
>> linsolve(A,b)
ans = 
#0.25     
#0.13636  
  0.90909   
>> linsolve2(A,b) % not Applet or Midlet
ans = 
#0.25     
#0.13636  
  0.90909

For large numeric matrices one should use the LAPACK-version if available. The Jasymca version can also handle matrices containing exact or symbolic elements. To avoid rounding errors in these cases it is advisable to work with exact numbers if possible:

>> syms x,y
>> A=[x,1,-2,-2,0;1 2 3*y 4 5;1 2 2 0 1;9 1 6 0 -1;0 0 1 0] 
A = 
  x    1    -2   -2   0   % symbolic element
  1    2    3*y  4    5   % symbolic element 
  1    2    2    0    1    
  9    1    6    0    -1   
  0    0    1    0    0    
>> b = [1 -2  3  2  4 ];
>> trigrat( linsolve( rat(A), b) )  
                            
ans = 
  (-6*y-13/2)/(x+8)                    
  (20*y+(-9*x-151/3))/(x+8)            
  4                                    
  ((-3*x+10)*y+(-49/4*x-367/6))/(x+8)  
  (-34*y+(13*x+403/6))/(x+8)

Nonlinear Equations

"Equation" in the following means the equation expression = 0. Equations are solved for a symbolic variable x by the function solve(expression, x). If expression is a quotient, then nominator = 0 is solved. Jasymca uses the following strategy to solve equations:

  • Unordered List ItemFirst, all occurrences of the variable x in expression are counted, both as free variable and embedded inside functions. Example: In $x^3\cdot\sin(x)+2x^2-\sqrt{x-1}$ x occurs three times: as free variable, in $\sin(x)$ and in $\sqrt{x-1}$.
  • Unordered List ItemIf this count is one, then we are dealing with a polynomic equation, which is solved for the polynomial's main variable, e.g. z. This works always, if the polynomial's degree is 2 or of it is biquadratic, otherwise only, if the coefficients are constant. In the next step the solution is solved for the desired variable x. As an example: Jasymca has to solve $\sin^2(x)-2\sin(x)+1=0$ for $x$. It first solves $z^2-2z+1=0$ for $z$ and then $\sin(x)=z$ for $x$. Examples with free variables:
>> syms x,b
>> solve(x^2-1,x)     
ans = [ 1  -1 ]
>> solve(x^2-2*x*b+b^2,x)
ans = b

An example with function variable ($exp(j\cdot x)$):

>> syms x
>> float( solve(sin(x)^2+2*cos(x)-0.5,x) )  
ans = [ 1.438i  -1.438i  -1.7975  1.7975 ]
  • Unordered List ItemIf count is 2, only one case is further considered: The variable occurs free and inside squareroot. This squareroot is then isolated, the equation squared and solved. This case leads to additional false solutions, which have to be sorted out manually.
>> syms x
>> y=x^2+3*x-17*sqrt(3*x^2+12);
>> solve(y,x)
ans = [ -32.501  26.528 -1.3931E-2-2.0055i  -1.3931E-2+2.0055i ]
  • Unordered List ItemIn all other cases Jasymca gives up.

Systems of Nonlinear Equations

Coupled systems of equations can be solved by the function algsys([expressions],[symbolic variables]). First, all linear equations are solved using the Gauss-method, then each equation is fed through solve() and the solution used to eliminate one variable in all other expressions. The equations are treated in the order they are supplied. This method only works for simple systems. The solution is provided as vector of solution vectors, each individual solution in as linear factor: In the first example below there is one solution with xs=-2/3, a2=3/4, a0=2, a1=0, the second example has two solutions.

>> syms xs,a0,a1,a2
>> algsys([2-a0,a1-0,a2*xs^2+a1*xs+a0-3-xs, 2*a2*xs+a1+1],[a2,a1,a0,xs])
ans = [ [ xs+2/3  a2-3/4  a0-2  a1 ] ]
>> syms a,xs
>> algsys([a*xs+3*a-(3-xs^2),a+2*xs],[a,xs])
ans = [ [ -sqrt(6)+(xs+3)  2*sqrt(6)+(a-6) ]  [ sqrt(6)+(xs+3)  -2*sqrt(6)+(a-6) ] ]
>> float(ans)
ans = [[ xs+0.55051  a-1.101 ] [ xs+5.4495  a-10.899 ]]

Summary Table

Name( Arguments ) Function Mod
subst($var_x$,$var_y$,$var_z$) substitute $var_x$ for $var_y$ in $var_z$ M,O
trigrat($var$) trigonometric and other simplifications M,O
trigexp($var$) trigonometric expansion M,O
solve($var$, $Sym$) solves $var = 0$ for $Sym$. M,O
algsys([$var_1$, $var_2,\ldots$],[$sym_1$, $sym_2,\ldots$]) solves the system of equations $var_1 = 0, var_2 = 0,\ldots$ for $sym_1, sym_2,\ldots$. M,O
linsolve($matrix$, $vector$) solves $matrix \cdot x = vector$ for $x$ M,O
linsolve2($matrix$, $vector$) solves $matrix \cdot x = vector$ for $x$ (Lapack) M,O
linlstsq($matrix$, $vector$) solves $matrix \cdot x = vector$ for $x$, overdetermined (Lapack) M,O