MATLAB: From bode to transfer function

bode plotMATLABSystem Identification Toolboxtfesttransfer function

I'm trying to get a transfer function out of the bode plot data. I've tried multiple functions(using this and this) yet when I plot both an example function and the solution the functions give me, it doesn't match.
So first I made the example:
examp1=tf([1],[1 -4 3])
[AMP PHA W]=bode(examp1)
which is a function with 2 poles. Then with tfest I made(copy/paste):
gain = squeeze(AMP);
phase = squeeze(PHA);
whz = squeeze(W);
response = gain.*exp(1i*phase*pi/180);
Ts = 0.1; % your sampling time
w=whz*2*pi; %convert Hz to rad/sec
gfr = idfrd(response,w,Ts);
sys=tfest(gfr,2);
bode(sys)
hold on
bode(examp1)
within the tfest function the 2nd value should be 2, correct? Since we have 2 poles in the original function. Yet in the end, when we compare the 2 bodes, they aren't much alike.
How should I do this?

Best Answer

You need to set tfestoptions Focus property to "prediction" to allow for unstable systems (your system has two unstable poles).
Doc page explaining that is here, look under "Focus" property.
Here is the code that gives a perfect fit (also note that bode returns frequency vector in rad/time unit, so there is no need to convert to hz as you were doing).
examp1=tf([1],[1 -4 3]);
[AMP PHA W]=bode(examp1);
gain = squeeze(AMP);
phase = squeeze(PHA);
w = squeeze(W);
response = gain.*exp(i*phase*pi/180);
Ts = 0.1; % your sampling time
gfr = idfrd(response,w,Ts);
Options = tfestOptions;
Options.Focus = 'prediction';
tf1 = tfest(gfr, 2, 1, Options, 'Ts', 0.1);
P = bodeoptions;
P.PhaseWrapping = 'on';
bodeplot(examp1,tf1,P);