6
$\begingroup$

How to plot frequency response which includes magnitude response and phase response of a transfer function in Mathematica?

In Matlab, we can use [h,k]=freqz(b, a, N); to generate magnitude response we can plot abs(h) and to plot phase we can do it by angle(h).

So is there any kind of alternative for this Mathematica?

Example : 2y(n-2)+5y(n-1)+7y(n) = 3x(n-1)+5x(n)

$\endgroup$
1
  • 3
    $\begingroup$ Welcome to Mathematica SE. If you want good answers, please provide a minimal example of a differential equation so that people can demonstrate Mathematica's capabilities more concretely. $\endgroup$
    – Roman
    Apr 29, 2019 at 8:04

1 Answer 1

10
$\begingroup$

In Mathematica, you can work with the function h(z) directly, since Mathematica is symbolic based. In other words, while in Matlab one needs to work for numerical values of coefficients of numerator and denominator and make sure they are in the correct order and so on, in Mathematica, you work with the actual formula or expression itself (transfer function, either in s or z domain).

I just looked at example in Matlab home page for freqz, which used [h,w] = freqz(b,a,'whole',2001); and duplicated the output. This just gives you a start.

Mathematica graphics

In Mathematica:

Mathematica graphics

The code is

h[z_] := (0.05634*(1 + 1/z)*(1 - 1.0166/z + 1/z^2))/((1 - 
       0.683/z)*(1 - 1.4461/z + 0.7957/z^2)); 
sys = TransferFunctionModel[h[z], z, SamplingPeriod -> 1]; 
BodePlot[sys, {0.43, 6}]

There are many options you can adjust for scaling and units and such. See BodePlot and TransferFunctionModel

Update To reply to comment

I've compared the same function in Matlab and Mathematica, But I got different plots.Please check my plots at [drive.google.com/file/d/1UicgNUuNNmIz2OnDvKGErr-kKlElYoLh/…

That is because you did not use same scaling in Mathematica as in Matlab.

The Matlab code you used is

b=[7,8];
a=[2,9,2];
w=0:.1:6;
[h,t]=freqz(b,a,w);
subplot(2,1,1);
plot(t,abs(h));
subplot(2,1,2);
plot(t,angle(h));

Mathematica graphics

In Mathematica, to get the same output you need to use the same scaling, like this

h[z_] := (7/z + 8)/(2/z^2 + 9/z + 2); 
sys = TransferFunctionModel[h[z], z, SamplingPeriod -> 1]; 

BodePlot[sys, {1, 6}, 
    ScalingFunctions -> {{"Linear", "Absolute"}, {"Linear", "Radian"}},
    PhaseRange -> {-Pi, Pi}]

Mathematica graphics

For the second plot (phase plot), as why Matlab phase looks smoother than Mathematica at $\pi$, I can't answer now. But you can see this discussion on Phase plot.

how-to-fix-bodeplot-that-comes-with-mathematica

No time now for me now to look at this more.

$\endgroup$
3
  • $\begingroup$ This method resulted me with 2 plots, Are they Magnitude Response and Phase response? $\endgroup$ Apr 30, 2019 at 14:34
  • $\begingroup$ I've compared the same function in Matlab and Mathematica, But I got different plots.Please check my plots at [drive.google.com/file/d/1UicgNUuNNmIz2OnDvKGErr-kKlElYoLh/… $\endgroup$ Apr 30, 2019 at 14:50
  • $\begingroup$ @AjayDyavathi fyi, updated answer. $\endgroup$
    – Nasser
    Apr 30, 2019 at 20:04

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.