提问人:RogerDodger 提问时间:8/24/2023 最后编辑:RogerDodger 更新时间:8/24/2023 访问量:26
八度脚本从中创建符号函数?
Octave Script Creating Symbolic Function from?
问:
我正在尝试使用嵌套的for循环计算以下内容。方程的输入要么是实数正双精度值,要么是包含实正二精度值的数组。没有符号输入。在代码的前面,我使用符号变量,但我通过执行“double(x)”等方式将它们转换为双精度值。
当方程式在这个特定的脚本文件中运行时,它会出错,说试图将浮点传递给符号是危险的,依此类推。
当我打开一个新选项卡,用双精度值对变量进行硬编码,运行脚本,查看方程的输出时,它运行良好,我能够绘制我需要的东西......
我不明白为什么在我写的主脚本中,方程式将每个结果都变成一个符号答案,并最终创建一个符号矩阵......
我知道这个脚本有效,因为它以前在工作,所以起源是一个工作脚本。
代码公式如下所示:
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 是双精度,所有其他变量都是实数双精度或整数,如我所述。另外,显示我收到的警告。
这种类型的错误将永远存在,除非您按ctrl + C停止它。
当我执行ctrl + C,然后打印函数的输出时,我得到以下输出:
除此之外,我将代码复制并粘贴到一个新的脚本页面中。但是,在嵌套的 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
答:
昨晚我最终找到了答案,我在变量 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])
评论