debugging 八度脚本创建符号函数从?

nom7f22z  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(111)

我尝试使用嵌套的for循环来计算以下内容。等式的输入是真实的正双精度数,或者是其中包含真实的正双精度数的数组。没有符号输入。在代码的前面,我使用了符号变量,但我通过例如“double(x)"将它们转换为双精度数。
当公式在这个特定的脚本文件中运行时,它会出错,说试图将浮点数传递给syms是危险的,等等。
当我打开一个新的选项卡,并硬编码的变量与双精度,并运行脚本,看看输出的方程,它运行良好,我能够绘制我需要.
我不明白为什么在我写的主脚本中,等式把每个结果都变成了一个符号答案,最终创建了一个符号矩阵。
我知道这个脚本是有效的,因为它以前是有效的,所以起源是一个工作脚本。
代码公式如下所示:

fo_act = 1/(2*pi*sqrt(Lr_chosen*Cr_chosen))

fmin = double(Min_Norm_freq)*fo

fmax = double(Max_Norm_freq)*fo

FN_plot  = [0.1:0.001:fmax/fo*1.5]

QE_plot  = [0; Qe_minimum_load_value; Qe_act; Qe_ovrld_act]

LN_act = 3.5;

topology_Constant = 1;

for i = 1:size(QE_plot,1)
  for q = 1:size(FN_plot,2)
    MG(i,q) = (((LN_act*FN_plot(q)^2))/((((LN_act+1)*(FN_plot(q).^2)-1)+j*(((FN_plot(q)^2)-1).*FN_plot(q).*QE_plot(i)*LN_act))))*1/topology_Constant;
  endfor
endfor

在命令窗口中,当我在for循环之前得到每个变量的答案时,它们显示为真实的双精度型。
我添加了一个图像,显示Fn_plot是一个double,所有其他变量都是真实的double或整数,正如我所说的。
Scirpt Image
这种类型的错误将永远持续下去,除非你使用C++来阻止它。
当我执行codeword + C,然后打印函数的输出时,我得到下面的输出:
MG function Output
为了增加这个,我将代码复制并粘贴到一个新的脚本页面中。但是,在嵌套的for循环之前,我硬编码了如下所示的值。所以,上面的内容并不重要,因为正如你所看到的,这个公式只是使用我手动硬编码的值,上面的内容并不重要。
你不能运行这个,因为你需要其他文件中的其他函数。这是代码。

clear all;
clc;
pkg 

   x = input('For Max Gain Max load, Enter a value (knowing it will be divided by 100) for MG : ' );

    y = input('For Max Gain Max load,Enter a value (knowing it will be divided by 100) for QE : ' );

    z = input('For Max Gain Max load,Enter a value (knowing it will be divided by 10) for LN : ' );



%Select QE, MG, LN, for Max_gain -> minimum frequency
MG = sym(x)/100;
QE_maxLoad = sym(y)/100;
LN = sym(z)/10;
Des_rev = 0;
topology_Constant = 1;

Mg_Min = 0.79586;
Mg_max_ov = 1.2706;

Attainable_Peak_Gain_Graph(Mg_max_ov,Mg_Min,LN, Des_rev, topology_Constant)
printf(newline)
x0 = input('Enter Decimal Value for initial Guess for Numaerical Solver : ' );
printf(newline)
Min_Norm_freq = Fn_calc(MG, QE_maxLoad, LN, x0)
printf(newline)




#{

    x = input('For Minimum Gain Minimum load, Enter a value (knowing it will be divided by 100) for MG : ' );

    y = input('For Minimum Gain Minimum Load, Enter a value (knowing it will be divided by 100) for QE : ' );

    x0 = input('Enter Decimal Value for initial Guess for Numaerical Solver : ' );

MG = sym(x)/100;
QE_min_load = sym(y)/100;
printf(newline)
Max_Norm_freq = Fn_calc(MG, QE_min_load, LN, x0)
#}

printf(newline)
printf(newline)

n_act = 4;
Vout_nom = 42;
Iout = 56;

%Determine Re
Re = ((8 * n_act^2)/pi^2)*(Vout_nom/Iout) % Full load Re
Re_overLoad = Re/1.1 % Over load Re @ 110%

%Determine Resonant Components

MG = double(MG);
QE = double(QE_maxLoad)
LN = double(LN)

%Initial Chosen Resonant Frequency, 90% of Fmax

fo = input('Enter The Resonant Frequency of choice: ' )
printf(newline)

QE_CR = QE;

printf(newline)
printf(newline)
printf('Resonant Capacitance')
printf(newline)

Cr = 1/(2*pi*fo*Re*QE_CR)

Cr_chosen = input('Enter Chosen Value for Cr : ' )

printf(newline)
printf(newline)
printf('Resonant Inductances and Normalized Inductance')
printf(newline)
Lr = 1/(((2*pi*fo)^2)*Cr_chosen)
printf(newline)
Lr_actual = (QE_CR*Re)^2 * Cr_chosen
%Qe_Cr_Chosen = sqrt(Lr/Cr_chosen)/Re
printf(newline)
Lr_chosen = input('Enter Chosen Value for Lr : ' )
Lm_required_for_Chosen_LN = LN*Lr_chosen
LM_chosen = input('Enter Chosen Value for LM : ' )
LN_act = LM_chosen/Lr_chosen

%Verify Design and Produce Gain graph


printf(newline)
printf(newline)

printf(newline)
printf(newline)
printf('Full load and Overload Q factors')
printf(newline)

%Quality Factor at Full load
Qe_act = sqrt(Lr_chosen/Cr_chosen)/Re

%Quality Factor at Overload
Qe_ovrld_act = sqrt(Lr_chosen/Cr_chosen)/Re_overLoad

 printf(newline)
 printf(newline)


% Finding Max Normalized frequency for Minimum Load

% Finding Max Normalized frequency for Minimum Load

    pout = input('Enter the minimum output load in watts : ' );

   % y  = sqrt(Lr_chosen/Cr_chosen)/((8 * n_act^2)/pi^2)*(Vout_nom^2/pout); % QE_min_load
    printf(newline)
    Qe_minimum_load_value = sqrt(Lr_chosen/Cr_chosen)/(((8 * n_act^2)/pi^2)*(Vout_nom^2/pout))
    printf(newline)
    Mg_Min
    x = input('For Minimum Gain Minimum load, Enter a value (knowing it will be divided by 100) for MG : ' );

    y = input('Knowing Qe_minimum_load_value above, Enter the value (knowing it will be divided by 1000) for QE : ' );

    x0 = input('Enter Decimal Value for initial Guess for Numaerical Solver : ' );

MG = sym(x)/100;
QE_min_load = sym(y)/1000;
printf(newline)
Max_Norm_freq = Fn_calc(MG, QE_min_load, LN, x0)

printf(newline)
printf(newline)
printf('Design Frequencies')
printf(newline)
fo_act = 1/(2*pi*sqrt(Lr_chosen*Cr_chosen))
%fmin = double(Min_Norm_freq)*fo
fmin = 80000;
fmax = 250000;
%fmax = double(Max_Norm_freq)*fo
%FN_plot = [0.1:0.001:double(Max_Norm_freq)*1.5];
FN_plot  = [0.1:0.001:3];
%QE_plot = [0; double(QE_min_load); Qe_act; Qe_ovrld_act];
QE_plot  = [0; 0.35; 0.549; 0.60449];

for i = 1:size(QE_plot,1)
  for q = 1:size(FN_plot,2)
    MG(i,q) = (((3*FN_plot(q)^2))/((((3+1)*(FN_plot(q).^2)-1)+j*(((FN_plot(q)^2)-1).*FN_plot(q).*QE_plot(i)*3))))*1/1;
  endfor
endfor

%{
QE_plot = {0,"No Load"; Qe_minimum_load_value, "Min Load"; Qe_act, "Full Load"; Qe_ovrld_act, "Overload"};

figure()
%plot(FN_plot,abs(MG), 'MarkerSize', 10, 'LineWidth', 1.25, 'DisplayName', 'No load', 'DisplayName', 'Full Load', 'DisplayName', 'Overload')
plot(FN_plot,abs(MG), 'MarkerSize', 10, 'LineWidth', 1.25)

hold on
%semilogx(F_POS,F_cap_Q,'m','LineWidth', 1);
%semilogx(F_POS,F_cap_Q,'ro','LineWidth', 1,'MarkerSize', 5);
%title(['Design Rev ', num2str(Des_rev), " - MG_a_c_t_u_a_l vs F_n"] , 'fontsize', 25)

xlabel(" F_n (Normalized Frequency)",'fontsize', 24)
ylabel(" MG_a_c_t_u_a_l", 'fontsize', 24)
axis ([0, fmax/fo*1.4 , 0.6*0.79, 1.3*1.5])

然后,我写了完全相同的代码,除了我删除了所有的代码以上“fmin”,与硬编码的值到另一个新的脚本页面,它运行良好。只是为了显示它是完全相同的,下面的图片..我不明白这有什么意义。

fmin = 80000;
fmax = 250000;
%fmax = double(Max_Norm_freq)*fo
%FN_plot = [0.1:0.001:double(Max_Norm_freq)*1.5];
FN_plot  = [0.1:0.001:3];
%QE_plot = [0; double(QE_min_load); Qe_act; Qe_ovrld_act];
QE_plot  = [0; 0.35; 0.549; 0.60449];

for i = 1:size(QE_plot,1)
  for q = 1:size(FN_plot,2)
    MG(i,q) = (((3*FN_plot(q)^2))/((((3+1)*(FN_plot(q).^2)-1)+j*(((FN_plot(q)^2)-1).*FN_plot(q).*QE_plot(i)*3))))*1/1;
  endfor
endfor
zbq4xfa0

zbq4xfa01#

我昨晚终于找到了答案,我在变量MG被分配符号类的地方放了大量的百分比符号。代码之前工作正常,因为我重复使用了同一个变量(MG)两次,但是,在去那个主要方程之前,我把它变成了一个双精度型。当我移动我的代码片段时,我忘记了在进入那个方程之前给它分配一个双精度型,所以我遇到了问题

clear all;
clc;
pkg 

   x = input('For Max Gain Max load, Enter a value (knowing it will be divided by 100) for MG : ' );

    y = input('For Max Gain Max load,Enter a value (knowing it will be divided by 100) for QE : ' );

    z = input('For Max Gain Max load,Enter a value (knowing it will be divided by 10) for LN : ' );



%Select QE, MG, LN, for Max_gain -> minimum frequency

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MG = sym(x)/100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

QE_maxLoad = sym(y)/100;
LN = sym(z)/10;
Des_rev = 0;
topology_Constant = 1;

Mg_Min = 0.79586;
Mg_max_ov = 1.2706;

Attainable_Peak_Gain_Graph(Mg_max_ov,Mg_Min,LN, Des_rev, topology_Constant)
printf(newline)
x0 = input('Enter Decimal Value for initial Guess for Numaerical Solver : ' );
printf(newline)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Min_Norm_freq = Fn_calc(MG, QE_maxLoad, LN, x0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

printf(newline)





#{

    x = input('For Minimum Gain Minimum load, Enter a value (knowing it will be divided by 100) for MG : ' );

    y = input('For Minimum Gain Minimum Load, Enter a value (knowing it will be divided by 100) for QE : ' );

    x0 = input('Enter Decimal Value for initial Guess for Numaerical Solver : ' );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
**MG** = sym(x)/100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
QE_min_load = sym(y)/100;
printf(newline)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Max_Norm_freq = Fn_calc(**MG**, QE_min_load, LN, x0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#}

printf(newline)
printf(newline)

n_act = 4;
Vout_nom = 42;
Iout = 56;

%Determine Re
Re = ((8 * n_act^2)/pi^2)*(Vout_nom/Iout) % Full load Re
Re_overLoad = Re/1.1 % Over load Re @ 110%

%Determine Resonant Components

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MG = double(MG);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

QE = double(QE_maxLoad)
LN = double(LN)

%Initial Chosen Resonant Frequency, 90% of Fmax

fo = input('Enter The Resonant Frequency of choice: ' )
printf(newline)

QE_CR = QE;

printf(newline)
printf(newline)
printf('Resonant Capacitance')
printf(newline)

Cr = 1/(2*pi*fo*Re*QE_CR)

Cr_chosen = input('Enter Chosen Value for Cr : ' )

printf(newline)
printf(newline)
printf('Resonant Inductances and Normalized Inductance')
printf(newline)
Lr = 1/(((2*pi*fo)^2)*Cr_chosen)
printf(newline)
Lr_actual = (QE_CR*Re)^2 * Cr_chosen
%Qe_Cr_Chosen = sqrt(Lr/Cr_chosen)/Re
printf(newline)
Lr_chosen = input('Enter Chosen Value for Lr : ' )
Lm_required_for_Chosen_LN = LN*Lr_chosen
LM_chosen = input('Enter Chosen Value for LM : ' )
LN_act = LM_chosen/Lr_chosen

%Verify Design and Produce Gain graph


printf(newline)
printf(newline)

printf(newline)
printf(newline)
printf('Full load and Overload Q factors')
printf(newline)

%Quality Factor at Full load
Qe_act = sqrt(Lr_chosen/Cr_chosen)/Re

%Quality Factor at Overload
Qe_ovrld_act = sqrt(Lr_chosen/Cr_chosen)/Re_overLoad

 printf(newline)
 printf(newline)


% Finding Max Normalized frequency for Minimum Load

% Finding Max Normalized frequency for Minimum Load

    pout = input('Enter the minimum output load in watts : ' );

   % y  = sqrt(Lr_chosen/Cr_chosen)/((8 * n_act^2)/pi^2)*(Vout_nom^2/pout); % QE_min_load
    printf(newline)
    Qe_minimum_load_value = sqrt(Lr_chosen/Cr_chosen)/(((8 * n_act^2)/pi^2)*(Vout_nom^2/pout))
    printf(newline)
    Mg_Min
    x = input('For Minimum Gain Minimum load, Enter a value (knowing it will be divided by 100) for MG : ' );

    y = input('Knowing Qe_minimum_load_value above, Enter the value (knowing it will be divided by 1000) for QE : ' );

    x0 = input('Enter Decimal Value for initial Guess for Numaerical Solver : ' );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MG = sym(x)/100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

QE_min_load = sym(y)/1000;
printf(newline)
Max_Norm_freq = Fn_calc(MG, QE_min_load, LN, x0)

printf(newline)
printf(newline)
printf('Design Frequencies')
printf(newline)
fo_act = 1/(2*pi*sqrt(Lr_chosen*Cr_chosen))
%fmin = double(Min_Norm_freq)*fo
fmin = 80000;
fmax = 250000;
%fmax = double(Max_Norm_freq)*fo
%FN_plot = [0.1:0.001:double(Max_Norm_freq)*1.5];
FN_plot  = [0.1:0.001:3];
%QE_plot = [0; double(QE_min_load); Qe_act; Qe_ovrld_act];
QE_plot  = [0; 0.35; 0.549; 0.60449];

for i = 1:size(QE_plot,1)
  for q = 1:size(FN_plot,2)
    MG(i,q) = (((3*FN_plot(q)^2))/((((3+1)*(FN_plot(q).^2)-1)+j*(((FN_plot(q)^2)-1).*FN_plot(q).*QE_plot(i)*3))))*1/1;
  endfor
endfor

%{
QE_plot = {0,"No Load"; Qe_minimum_load_value, "Min Load"; Qe_act, "Full Load"; Qe_ovrld_act, "Overload"};

figure()
%plot(FN_plot,abs(MG), 'MarkerSize', 10, 'LineWidth', 1.25, 'DisplayName', 'No load', 'DisplayName', 'Full Load', 'DisplayName', 'Overload')
plot(FN_plot,abs(MG), 'MarkerSize', 10, 'LineWidth', 1.25)

hold on
%semilogx(F_POS,F_cap_Q,'m','LineWidth', 1);
%semilogx(F_POS,F_cap_Q,'ro','LineWidth', 1,'MarkerSize', 5);
%title(['Design Rev ', num2str(Des_rev), " - MG_a_c_t_u_a_l vs F_n"] , 'fontsize', 25)

xlabel(" F_n (Normalized Frequency)",'fontsize', 24)
ylabel(" MG_a_c_t_u_a_l", 'fontsize', 24)
axis ([0, fmax/fo*1.4 , 0.6*0.79, 1.3*1.5])

相关问题