9
$\begingroup$

The documentation for NonlinearStateSpaceModel says:

In NonlinearStateSpaceModel[eqns, ...] the Taylor linearization is with respect to the highest-order derivatives of $x_i$ about the point ($x_{i0}$, $u_{i0}$) of the differential equations eqns.

Why does it linearise the equations? It is, after all, supposed to be a nonlinear state space model.

$\endgroup$

2 Answers 2

12
$\begingroup$

Consider a model where the nonlinearity is in $x'(t)$ and not in the highest derivative $x''(t)$.

eq1 = {x''[t] + Sin[ x'[t]] + x[t] == u[t]};

Choose the first state as $x(t)$ (call it $\bar{x}_1(t)$) and the second state as $x'(t)$ (call it $\bar{x}_2(t)$) and we get the following equations.

$$ \bar{x}_1'(t)=\bar{x}_2(t)$$ $$ \bar{x}_2'(t)+\sin \left(\bar{x}_2(t)\right)+\bar{x}_1(t)=u(t) $$

And this is exactly what NonlinearStateSpace model does. It does not throw out anything when converting it to a state-space representation.

nssm1 = NonlinearStateSpaceModel[eq1, x[t], u[t], x[t], t]

enter image description here

Therefore the simulations match.

OutputResponse[nssm1, UnitStep[t], {t, 0, 5}];
NDSolve[Join[eq1 /. u[t] -> UnitStep[t], {x[0] == 0, x'[0] == 0}], x[t], {t, 0, 5}];
p1 = Plot[Evaluate@Join[%, %%], {t, 0, 5}]

enter image description here

Now consider another model which has the nonlinearity in the highest-order derivative $x''(t)$.

eq2 = {Sin[x''[t]] + Sin[ x'[t]] + x[t] == u[t]};

Then the second state-equation becomes

$$ \sin\left(\bar{x}_2'(t)\right)+\sin \left(\bar{x}_2(t)\right)+\bar{x}_1(t)=u(t) $$

This cannot be represented using NonlinearStateSpaceModel, which requires that the state derivatives all be linear. Thus it will return the same model as before after linearizing the nonlinearity.

nssm2 = NonlinearStateSpaceModel[eq2, x[t], u[t], x[t], t]

enter image description here

And, just to verify, the simulation matches the previous one.

OutputResponse[nssm2, UnitStep[t], {t, 0, 5}];
Show[p1, Plot[%, {t, 0, 5}]]

enter image description here

The complete nonlinear model is a different animal.

NDSolve[Join[eq2 /. u[t] -> UnitStep[t], {x[0] == 0, x'[0] == 0}], x[t], {t, 0, 5}]

enter image description here

Update:

As zxczd mentions in the comments to this answer, the complete nonlinear model has multiple solutions. It's interesting to see that model linearized by NonlinearStateSpaceModel approximates just one of them.

{x''[t] == (x''[t] /. Solve[eq2, x''[t]][[1]]) /. C[1] -> 0};
NDSolveValue[{% /. u[t] -> UnitStep[t], {x[0] == 0, x'[0] == 0}}, x, {t, 0, 5}];
Show[ListLinePlot[%, PlotStyle -> Dashed], p1, PlotRange -> {{0, 5}, All}]

enter image description here

A bigger sample of possible solutions:

possibleeq = Thread[x''[t] == Re[x''[t] /. Solve[eq2, x''[t]] /. C[1] -> #]] & /@ 
Range[-5, 5] // Flatten; 
possiblesol = NDSolveValue[{# /. u[t] ->(*Sin@t*)1, {x[0] == 0, x'[0] == 0}}, 
x, {t, 0, 5}] & /@ possibleeq; 
ListLinePlot[possiblesol]

enter image description here

$\endgroup$
9
  • $\begingroup$ The last example can be solved if you add SolvedDelayed->True to NDSolve. $\endgroup$
    – xzczd
    Nov 6, 2017 at 15:17
  • $\begingroup$ @xzczd Thanks. I believe it is not simply adding the option, right? Do the equations also have to be reformulated? $\endgroup$ Nov 6, 2017 at 15:26
  • $\begingroup$ No, you just need this option :) , then NDSolve will be able to solve the problem with a DAE solver. You may want to read this: mathematica.stackexchange.com/a/158519/1871 $\endgroup$
    – xzczd
    Nov 6, 2017 at 15:33
  • $\begingroup$ I think SolvedDelayed has been deprecated in my version. "EquationSimplification" -> "Residual" works in that it returns the solution 1. $\endgroup$ Nov 6, 2017 at 15:45
  • 2
    $\begingroup$ After a second thought I noticed your last example is not trivial, it actually has infinite many solutions, and SolveDelayed only find one by default. The following is a way to find more solutions: possibleeq = Thread[x''[t] == Re[x''[t] /. Solve[eq2, x''[t]] /. C[1] -> #]] & /@ Range[-5, 5] // Flatten; possiblesol = NDSolveValue[{# /. u[t] ->(*Sin@t*)1, {x[0] == 1, x'[0] == 0}}, x, {t, 0, 5}] & /@ possibleeq; ListLinePlot[possiblesol] $\endgroup$
    – xzczd
    Nov 6, 2017 at 16:18
10
$\begingroup$

My guess is to be able to have the same internal representations as for linear systems. It is not possible to represent a system using A,B,C,D state space standard matrices without the system being linear.

For example, say the ODE was not linear, as in $x''(t)+x^2(t)=1$. Convert to state space. Let $x_1=x,x_2=x'$. Taking derivatives gives $x_1'(t)=x_2,x_2'=x''=1-x_1^2$

Now, how will one write $x'=Ax$ for the above? Not possible

$$ \begin{pmatrix} x_{1}^{\prime}\\ x_{2}^{\prime} \end{pmatrix} = \begin{pmatrix} 0 & 1\\ ? & 0 \end{pmatrix} \begin{pmatrix} x_{1}\\ x_{2} \end{pmatrix} + \begin{pmatrix} 0\\ 1 \end{pmatrix} $$

So linearization is done around the operating point to be able to obtain linear state space internal representation, so the rest of the functions in state space could use them. So close to this point, the solution is accurate.

u=UnitStep[t];
ode1=x1'[t]==u-x1[t] x2[t];
ode2=x2'[t]==u x2[t]+1;
solNDsolve=NDSolve[{ode1,ode2,x1[0]==0,x2[0]==0},x1,{t,0,2}];

ClearAll[u,x,t]
nsys=NonlinearStateSpaceModel[{{u-x1 x2,u x2+1},{x1}},{{x1,0},{x2,0}},u];
solState=OutputResponse[nsys,UnitStep[t],{t,0,5}];

Plot[{solState,Evaluate[x1[t]/.solNDsolve]},{t,0,2},
     PlotLegends->{"Nonlinear State Space solution","NDSolve solution"}]

Mathematica graphics

To change the initial conditions, then

u=UnitStep[t];
ode1=x1'[t]==u-x1[t] x2[t];
ode2=x2'[t]==u  x2[t]+1;
solNDsolve=NDSolve[{ode1,ode2,x1[0]==1,x2[0]==1},x1,{t,0,2}];

ClearAll[u,x,t]
nsys=NonlinearStateSpaceModel[{{u-x1 x2,u x2+1},{x1}},{{x1,1},{x2,1}},u];
solState=OutputResponse[nsys,UnitStep[t],{t,0,2}];

Plot[{solState,Evaluate[x1[t]/.solNDsolve]},{t,0,2},
      PlotLegends->{"Nonlinear State Space solution","NDSolve solution"}]

Mathematica graphics

$\endgroup$
5
  • 3
    $\begingroup$ You gave NonlinearStateSpaceModel a bad rap. :) What you mark as 'state-space solution' is actually 'linear state-space solution' and you could just have used StateSpaceModel. NonlinearStateSpaceModel is a bigger fish. $\endgroup$ Nov 6, 2017 at 15:16
  • 1
    $\begingroup$ @SubaThomas I am sorry, I am using the help example as is in the above. reference.wolfram.com/language/ref/… and compared its output to NDSolve. If you mean the label on my plot should have the word Nonlinear added to it, Ok, I can do that. But I thought from the context it was clear it is using Nonlinear state space. But will change the labels now. $\endgroup$
    – Nasser
    Nov 6, 2017 at 20:04
  • $\begingroup$ Looking at your code a little more closely, I see the mistake you made. The second rhs (for ode2) you entered in NonlinearStateSpaceModel should be u- x2+1. Then the solutions agree. Initially, I looked at your original equation (with no nonlinearity in the highest derivative) and attributed the differences in the solutions to you having linearized the equations. I now realize, you are missing a negative sign. (I did not also follow how you got ode1 and ode2 from the original ode you have. But that's a different issue.) $\endgroup$ Nov 6, 2017 at 20:21
  • 1
    $\begingroup$ @SubaThomas sorry about this typo again. I will correct that now also. I got the original ode1 and ode2 from the help page itself. !Mathematica graphics when I typed it by hand for NDSolve I made the mistake. Will fix soon $\endgroup$
    – Nasser
    Nov 6, 2017 at 20:26
  • 2
    $\begingroup$ Thanks Nasser. Sorry about my lack of diligence initially. To avoid confusion to anyone else who may read these comments, let me add that this answer illustrates that NonlinearStateSpaceModel does not do any linearization when there is no nonlinearity in the highest-order derivatives. There is perfect agreement in the simulations whether one uses OutputResponse or directly calls NDSolve (which is what OutputResponse does behind the scenes.) $\endgroup$ Nov 6, 2017 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.