# Frequency Response

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)

• 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. Apr 29, 2019 at 8:04

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. In Mathematica: 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

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)); 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}] 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.

• This method resulted me with 2 plots, Are they Magnitude Response and Phase response? Apr 30, 2019 at 14:34
• 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/… Apr 30, 2019 at 14:50
• @AjayDyavathi fyi, updated answer. Apr 30, 2019 at 20:04