2
$\begingroup$

    n = 13;
    Show[Plot[

      Evaluate[Chop @ 
        OutputResponse[
         TransferFunctionModel[Unevaluated[{{1/(s (s + 2) (s + 3))}}], s, 
          SamplingPeriod ->None, SystemsModelLabels -> None], Ramp[t], 
         t]], {t, 0, n}, PlotRange -> All], 
     Plot[Ramp[t], {t, 0, n}, PlotRange -> All, PlotStyle -> Red]]

enter image description here

for $n=10$ this works fine

enter image description here

I am not exactly sure if I am plotting transfer function the right way. Any help is appriciated.

$\endgroup$
0

2 Answers 2

2
$\begingroup$

Use Simplify rather than Chop which only works on numerical results

n = 13;
Show[Plot[
  Evaluate[Simplify@
    OutputResponse[
     TransferFunctionModel[Unevaluated[{{1/(s (s + 2) (s + 3))}}], s, 
      SamplingPeriod -> None, SystemsModelLabels -> None], Ramp[t], 
     t]], {t, 0, n}, PlotRange -> All], 
 Plot[Ramp[t], {t, 0, n}, PlotRange -> All, PlotStyle -> Red]]

enter image description here

Just to show why this happens; take a look at this example:

Plot[Exp[5 t] (Exp[-5 t] - t/t + 1), {t, 0, 20}]

enter image description here

The plot above should have been a horizontal line across 1, but it's not. What happens here is that the HoldAll attribute of Plot prevents any symbolic transformation to be done, or the expression sometimes is too complicated and time consuming for Mathematica to automatically simplify (like your case), so small numerical errors that are multiplied by terms like Exp[t] become significant in the plot. Thus, Simplify would be helpful in this case.

Plot[Evaluate[Exp[5 t] (Exp[-5 t] - t/t + 1) // Simplify], {t, 0, 20}]

enter image description here

$\endgroup$
1
  • $\begingroup$ That was a lifesaver $\endgroup$
    – Dip773
    Commented Apr 5, 2017 at 5:33
3
$\begingroup$

On a side note, Your system is marginally unstable (sometimes called marginally stable)

n = 13;
sys = TransferFunctionModel[{{1/(s (s + 2) (s + 3))}}, s, 
  SamplingPeriod -> None, SystemsModelLabels -> None]

Mathematica graphics

poles = TransferFunctionPoles[sys]

Mathematica graphics

Now to the main question. I have always used the numerical version of output response. I find it much faster as well. The method you used produces analytical solution. And I am not sure why the analytical solution becomes unstable for large t. However the numerical solution works:

out = First@OutputResponse[sys, Ramp[t], {t, 0, 20}];
Plot[out, {t, 0, 20}]

Mathematica graphics

Verified with Matlab, it gives same result as above:

>> clear
>> s=tf('s');
>> sys = 1/(s*(2+s)*(3+s));
>> t=0:0.1:20;[y,t]=lsim(sys,t,t);
>> plot(t,y)

Mathematica graphics

When in doubt with OutputResponse use the numerical version, which happens automatically when you change t to {t,0,20} and it is much faster also.

It is possible the analytical solution have some issues.

 out = First@OutputResponse[sys, Ramp[t], t]

Mathematica graphics

 Plot[out, {t, 0, 20}]

Mathematica graphics

On other hand, it is possible the analytical solution is actually correct and the numerical solution is not! Since this is marginally stable system. Hard for me to say now. May be someone who knows more can look into this.

update

I just verified this using Maple 2016.2, and it gives same plot as Matlab and Mathematica's numerical version. This leads me to think Mathematica analytical solution is the one that could have some issue in it:

 sys := DynamicSystems:-TransferFunction(1/(s*(2+s)*(3+s)));
 p1:=DynamicSystems:-ResponsePlot(sys, t,duration=20);

Mathematica graphics

One way to really make sure, is the following: Convert the transfer function to ODE, and solve the ODE with ramp input and see if you get same solution as one given by OutputResponse or not. No time to do this now, have to go. Will try to do this later.

$\endgroup$
2
  • $\begingroup$ Actually it's caused by numerical error. You can get the same plot from analytical results by simplifying out. I believe, due to the complex form of the solution and the HoldAll property of Plot, large numerical errors are not uncommon in such cases. $\endgroup$
    – W.Mason
    Commented Apr 4, 2017 at 10:44
  • $\begingroup$ Thank you, I have upvoted you. I wish there is an "accept both answer" $\endgroup$
    – Dip773
    Commented Apr 5, 2017 at 5:33

Your Answer

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

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