Pure_gain = (M_Val/70) * wR_Larger * (1/M_Val);
Integrator = tf(Ts_Val,[1 -1],Ts_Val);
set(gcf,'Units','centimeters','position',[0 0 25 30]);
[~, Tenv_None, Nenv_None, ~, OSenv_None]...
= OuterLoop_TransferFcn_withEnv(omega_D_Val, wR_Larger, Ts_Val, (M_Val/70), Mn_Val, M_Val, Kfn_head_Val, Kfn_Val, Kf_Val, D_env_Val, K_env_Val, Cf_Val);
[Gain, ~, Frequency] = bode(Pure_gain * Integrator * Tenv_None * OSenv_None, {1e-4,6283.19});
semilogx(reshape(Frequency,[],1), 20*log10(reshape(Gain,[],1)), "-", 'Color', 'k', 'LineWidth',1.5);
[Gain, ~, Frequency] = bode(Integrator * Tenv_None * OSenv_None * Pure_gain, {1e-4,6283.19});
semilogx(reshape(Frequency,[],1), 20*log10(reshape(Gain,[],1)), "--", 'Color', 'k', 'LineWidth',1.5);
function [G_env, T_env, N_env, Compensator_env, OuterLoop_Sensitivity_Function_env]...
= OuterLoop_TransferFcn_withEnv(omega_D_Val, omega_R_Val, Ts_Val, Mn_head_Val, Mn_Val, M_Val, Kfn_head_Val, Kfn_Val, Kf_Val, D_env_Val, K_env_Val, Cf_Val)
syms omega_R omega_D Ts Mn_head Mn M Kfn_head Kfn Kf D_env K_env
beta = (Mn*Kfn_head)/(Mn_head*Kfn);
Assigned_Parameters = [omega_D, omega_R, Ts, Mn_head, Mn, M, Kfn_head, Kfn, Kf, D_env, K_env];
Assigned_Values = [omega_D_Val, omega_R_Val, Ts_Val, Mn_head_Val, Mn_Val, M_Val, Kfn_head_Val, Kfn_Val, Kf_Val, D_env_Val, K_env_Val];
Denominator = (2*(z - 1)*((2 + omega_R*Ts)*z + (-2 + omega_R*Ts))*(M*z^2 + (M*alpha*omega_D*Ts + D_env*Ts - 2*M)*z + (M + K_env*Ts^2 - D_env*Ts - M*alpha*omega_D*Ts)))...
+ (Mn_head*Cf*omega_R*Ts*((2 + omega_D*Ts)*z + (-2 + omega_D*Ts))*(M*beta*z^3 + (D_env*Ts*beta - 2*M*alpha - M*beta)*z^2 + (K_env*beta*Ts^2 + 4*M*alpha -M*beta)*z + (K_env*beta*Ts^2 - D_env*beta*Ts - 2*M*alpha + M*beta)));
Denominator = subs(Denominator, Assigned_Parameters, Assigned_Values);
G_tf_Numerator = (Mn_head*omega_R*Ts*((2 + omega_D*Ts)*z + (-2 + omega_D*Ts))*(M*beta*z^3 + (D_env*Ts*beta - 2*M*alpha - M*beta)*z^2 + (K_env*beta*Ts^2 + 4*M*alpha -M*beta)*z + (K_env*beta*Ts^2 - D_env*beta*Ts - 2*M*alpha + M*beta)));
G_tf_Denominator = (2*(z - 1)*((2 + omega_R*Ts)*z + (-2 + omega_R*Ts))*(M*z^2 + (M*alpha*omega_D*Ts + D_env*Ts - 2*M)*z + (M + K_env*Ts^2 - D_env*Ts - M*alpha*omega_D*Ts)));
G_tf_Numerator = subs(G_tf_Numerator, Assigned_Parameters, Assigned_Values);
G_tf_Denominator = subs(G_tf_Denominator, Assigned_Parameters, Assigned_Values);
[G_tf_Numerator, G_tf_Denominator] = numden(G_tf_Numerator/G_tf_Denominator);
[bG,~] = coeffs(G_tf_Numerator, z, 'All');
[bG_D,~] = coeffs(G_tf_Denominator, z, 'All');
T_env_Numerator = M*z^2 + (alpha*omega_D*Ts*M - 2*M)*z + (M - alpha*omega_D*Ts*M);
T_env_Denominator = M*z^2 + (D_env*Ts - 2*M + alpha*omega_D*Ts*M)*z + (K_env*Ts^2 - D_env*Ts + M - alpha*omega_D*Ts*M);
T_env_Numerator = subs(T_env_Numerator, Assigned_Parameters, Assigned_Values);
T_env_Denominator = subs(T_env_Denominator, Assigned_Parameters, Assigned_Values);
[T_env_Numerator, T_env_Denominator] = numden(T_env_Numerator/T_env_Denominator);
[bT,~] = coeffs(T_env_Numerator, z, 'All');
[bT_D,~] = coeffs(T_env_Denominator, z, 'All');
N_env_Numerator = M*z^2 + (D_env*Ts - 2*M)*z + (M + K_env*Ts^2 - D_env*Ts);
N_env_Denominator = M*z^2 + (D_env*Ts - 2*M + M*alpha*omega_D*Ts)*z + (M + K_env*Ts^2 - D_env*Ts - M*alpha*omega_D*Ts);
N_env_Numerator = subs(N_env_Numerator, Assigned_Parameters, Assigned_Values);
N_env_Denominator = subs(N_env_Denominator, Assigned_Parameters, Assigned_Values);
[N_env_Numerator, N_env_Denominator] = numden(N_env_Numerator/N_env_Denominator);
[bN,~] = coeffs(N_env_Numerator, z, 'All');
[bN_D,~] = coeffs(N_env_Denominator, z, 'All');
C_tf_Numerator = (Mn_head*Cf*omega_R*Ts*((2 + omega_D*Ts)*z + (-2 + omega_D*Ts))*(M*beta*z^3 + (D_env*Ts*beta - 2*M*alpha - M*beta)*z^2 + (K_env*beta*Ts^2 + 4*M*alpha -M*beta)*z + (K_env*beta*Ts^2 - D_env*beta*Ts - 2*M*alpha + M*beta)));
C_tf_Numerator = subs(C_tf_Numerator, Assigned_Parameters, Assigned_Values);
[C_tf_Numerator, C_tf_Denominator] = numden(C_tf_Numerator/Denominator);
[bC,~] = coeffs(C_tf_Numerator, z, 'All');
[bC_D,~] = coeffs(C_tf_Denominator, z, 'All');
OS_tf_Numerator = (2*(z - 1)*((2 + omega_R*Ts)*z + (-2 + omega_R*Ts))*(M*z^2 + (M*alpha*omega_D*Ts + D_env*Ts - 2*M)*z + (M + K_env*Ts^2 - D_env*Ts - M*alpha*omega_D*Ts)));
OS_tf_Numerator = subs(OS_tf_Numerator, Assigned_Parameters, Assigned_Values);
[OS_tf_Numerator, OS_tf_Denominator] = numden(OS_tf_Numerator/Denominator);
[bOS,~] = coeffs(OS_tf_Numerator, z, 'All');
[bOS_D,~] = coeffs(OS_tf_Denominator, z, 'All');
G_env = tf(double(bG)/double(bG_D(1)), double(bG_D)/double(bG_D(1)), Ts_Val);
T_env = tf(double(bT)/double(bT_D(1)), double(bT_D)/double(bT_D(1)), Ts_Val);
N_env = tf(double(bN)/double(bN_D(1)), double(bN_D)/double(bN_D(1)), Ts_Val);
Compensator_env = tf(double(bC)/double(bC_D(1)), double(bC_D)/double(bC_D(1)), Ts_Val);
OuterLoop_Sensitivity_Function_env = tf(double(bOS)/double(bOS_D(1)), double(bOS_D)/double(bOS_D(1)), Ts_Val);