4
$\begingroup$

What is the difference between how Matlab and Mathematica solve State-Space and Transfer Function models?

I have a $16 \times 16$ state space system for which I am calculating transfer function. Mathematica and Matlab give me completely different answers. I can imagine truncation may lead to slightly different answers but what I get is a huge difference. I have checked the difference in numerical values to check the conditioning. There is no significant difference. I give the matrix as seen in Mathematica to you to check.

Please find the code attached!

a={{-(350103/48500), -14.9811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0}, {1.75898, -6.08528, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {12.4114, -32.0709, 310.284, 
0, -19.63, -102.059, -310.284, 77.9757, 0, 0, 0, 0, 0, 0, 0, 
0}, {2.21387, -5.72064, 55.3468, 0, -0.454893, -21.6178, -55.3468, 
13.9089, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0}, {1, -2.584, 25, 0, -1, -7.016, -25, 0, 0, 0, 0, 0, 
0, 0, 0, 0}, {0, 0, 0, 0, 12.4114, -32.0709, 310.284, 
0, -19.63, -102.059, -310.284, 77.9757, 0, 0, 0, 0}, {0, 0, 0, 0, 
2.21387, -5.72064, 55.3468, 0, -0.454893, -21.6178, -55.3468, 
13.9089, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0}, {0, 0, 0, 0, 1, -2.584, 25, 0, -1, -7.016, -25, 0, 0, 0, 0, 
0}, {0, 0, 0, 0, 0, 0, 0, 0, 12.4114, -32.0709, 310.284, 
0, -19.63, -102.059, -310.284, 77.9757}, {0, 0, 0, 0, 0, 0, 0, 0, 
2.21387, -5.72064, 55.3468, 0, -0.454893, -21.6178, -55.3468, 
13.9089}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 
0, 0, 0, 0, 0, 0, 1, -2.584, 25, 0, -1, -7.016, -25, 0}};

b={{116319/1940}, {10.695}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, 
{0}, {0}, {0}, {0}, {0}, {0}};

c={{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 1}};

sys = StateSpaceModel[{a,b,c}]

BodePlot[sys, {2 \[Pi] 0.01, 2 \[Pi] 100}, 
ScalingFunctions -> {{Automatic, Automatic}, {Automatic, "Degree"}}, 
PhaseRange -> {-\[Pi], \[Pi]}, GridLines -> Automatic, ImageSize ->Large]

Corresponding Matlab code with the same set of matrices:

[num,den] = ss2tf(a,b,c,[0;0;0]);

P1 = tf(num(1,:),den);
P2 = tf(num(2,:),den);
P3 = tf(num(3,:),den);

figure; bode(P1,P2,P3);

The transfer function that I can from Matlab and Mathematica are completely different in nature. I am pretty sure that according to theory Matlab gives me the right answer. But why is Mathematica so off the mark?

Could it be any of the following?

  1. Precision/truncation
  2. Size of the system
  3. Conditioning

Edit: I have included the plots for both. Left is Mathematica and right is Matlab. The colors correspond to each other. Please take note of the Magnitude scale for both! enter image description here

Any insight would be appreciated!

$\endgroup$
  • 2
    $\begingroup$ For reference, can you include both the Mathematica and MATLAB results? $\endgroup$ – J. M.'s ennui Jul 22 '16 at 14:06
  • 1
    $\begingroup$ I'd add that you really need to provide the Mathematica code, too. $\endgroup$ – JimB Jul 22 '16 at 14:28
  • 1
    $\begingroup$ Put another way: If you include the code, then you are much more likely to get assistance. Some of the best advice many times comes from folks who have no idea of your subject matter but do know how to program Mathematica. (I see you've added code in the comments. But you've only given freq_range which is undefined. Please include the full code in your question rather than the comments.) $\endgroup$ – JimB Jul 22 '16 at 14:33
  • 3
    $\begingroup$ @Zero, to reiterate what JimBaldwin said, please include the complete code. It is nontrivial and error-prone to be entering a system with 16 states and 3 outputs, and it is wasted time to repeat it if you have already done so. $\endgroup$ – Suba Thomas Jul 22 '16 at 14:43
  • 2
    $\begingroup$ Thanks. It does help. Note that C and D are reserved words in Mathematica and that the code given does not run because of that. One should avoid variable names with uppercase letters in Mathematica. After changing to a, b, c, and d the code runs as advertised. $\endgroup$ – JimB Jul 22 '16 at 15:01
9
$\begingroup$

Using Mathematica 10.4.1 (Windows 7) the following code finds a pretty good match with the Matlab results. (And I'd argue that any differences between the two are due to differences in how the precision of the input numbers are handled.)

a = Rationalize[a, 0.00001];
b = Rationalize[b];

sys = StateSpaceModel[{a, b, c, d}];

BodePlot[sys, {2 π 0.01, 2 π 100},
 ScalingFunctions -> {{Automatic, Automatic}, {Automatic, "Degree"}},
 PhaseRange -> {-π, π}, GridLines -> Automatic, 
 ImageSize -> Large,
 PlotRange -> {{Full, {0, -400}}, {Full, {-180, 180}}},
 PlotStyle -> {Blue, Red, Green}]

First BodePlot

Second BodePlot

$\endgroup$
2
$\begingroup$

In M11.3 this is much much easier to solve. Simply: use instead of d Automatic:

a = {{-(350103/48500), -14.9811, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0}, {1.75898, -6.08528, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {12.4114, -32.0709, 310.284,
     0, -19.63, -102.059, -310.284, 77.9757, 0, 0, 0, 0, 0, 0, 0, 
    0}, {2.21387, -5.72064, 55.3468, 0, -0.454893, -21.6178, -55.3468,
     13.9089, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0}, {1, -2.584, 25, 0, -1, -7.016, -25, 0, 0, 0, 0,
     0, 0, 0, 0, 0}, {0, 0, 0, 0, 12.4114, -32.0709, 310.284, 
    0, -19.63, -102.059, -310.284, 77.9757, 0, 0, 0, 0}, {0, 0, 0, 0, 
    2.21387, -5.72064, 55.3468, 0, -0.454893, -21.6178, -55.3468, 
    13.9089, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    0, 0}, {0, 0, 0, 0, 1, -2.584, 25, 0, -1, -7.016, -25, 0, 0, 0, 0,
     0}, {0, 0, 0, 0, 0, 0, 0, 0, 12.4114, -32.0709, 310.284, 
    0, -19.63, -102.059, -310.284, 77.9757}, {0, 0, 0, 0, 0, 0, 0, 0, 
    2.21387, -5.72064, 55.3468, 0, -0.454893, -21.6178, -55.3468, 
    13.9089}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0,
     0, 0, 0, 0, 0, 0, 1, -2.584, 25, 0, -1, -7.016, -25, 0}};

b = {{116319/
     1940}, {10.695}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, \
{0}, {0}, {0}, {0}, {0}};

c = {{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 1}};

a = Rationalize[a, 0.00001];
b = Rationalize[b];

sys = StateSpaceModel[{a, b, c, Automatic}]
BodePlot[sys, {2 \[Pi] 0.01, 2 \[Pi] 100}, 
 ScalingFunctions -> {{Automatic, Automatic}, {Automatic, "Degree"}}, 
 PhaseRange -> {-\[Pi], \[Pi]}, GridLines -> Automatic, 
 ImageSize -> Large, 
 PlotRange -> {{Full, {0, -400}}, {Full, {-180, 180}}}, 
 PlotStyle -> {Blue, Red, Green}]

Both MATLAB and Mathematica 11.3 provide the very same results.enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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