我有这个公式,我用它来计算给定Ps的Vo,它可以重新安排给Ps给出Vo:
Vo=(Ps)*(BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd
我想在MATLAB中为此目的制作一个多功能的功能。我首先声明一个符号函数并用数字替换解决它:
syms Ps BWc BWs Ro n LdB Rpd;
Vo(Ps,BWc,BWs,Ro,n,LdB,Rpd)=(Ps)*(BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd;
Vo(250,0.12,1000,0.01,4,0.5,75)
我希望能够为Ps重新安排符号功能并再次解决:
Ps(Vo,BWc,BWs,Ro,n,LdB,Rpd)= solve('Vo=(Ps)*(BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd','Ps');
Ps(0.0063,0.12,1000,0.01,4,0.5,75)
但是以上两行不起作用。我得到上面最后一行的错误:“操作符到||和&&运算符必须可以转换为逻辑标量值。”我也尝试过:
syms Vo Ps BWc BWs Ro n LdB Rpd;
Vo(Ps,BWc,BWs,Ro,n,LdB,Rpd)=(Ps)*(BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd;
Vo(250,0.12,1000,0.01,4,0.5,75)
Ps(Vo,BWc,BWs,Ro,n,LdB,Rpd)=(Vo)/((BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd);
Ps(0.0063,0.12,1000,0.01,4,0.5,75)
为此目的使用符号工具箱的正确方法是什么?
答案 0 :(得分:0)
问题可能是因为您有一个名为Vo
的变量,并且您还使用Vo
来定义函数。你不能这样做,仍然使用两者。它实际上是variable scope的问题。以下是您如何成功使用solve
:
syms Vo Ps BWc BWs Ro n LdB Rpd;
Vo_fun(Ps,BWc,BWs,Ro,n,LdB,Rpd) = Ps*(BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd
Vo_fun(250,0.12,1000,0.01,4,0.5,75)
Ps_fun(Vo,BWc,BWs,Ro,n,LdB,Rpd) = solve(Vo==Vo_fun,Ps)
Ps_fun(0.0063,0.12,1000,0.01,4,0.5,75)
在这种情况下,我根本不需要使用符号数学。简单的anonymous functions就足够了,例如:
Vo = @(Ps,BWc,BWs,Ro,n,LdB,Rpd)(1/10^((LdB*(2*n + 3))/10)*BWc*Ps*Ro*Rpd)/BWs;
Ps = @(Vo,BWc,BWs,Ro,n,LdB,Rpd)(10^((LdB*(2*n + 3))/10)*BWs*Vo)/(BWc*Ro*Rpd);
请注意,在命名函数时,范围没有问题。如果其他参数没有变化,您可以将它们作为Vo
和Ps
的函数:
BWc = ...
BWs = ...
Ro = ...
n = ...
LdB = ...
Rpd = ...
Vo = @(Ps)(1/10^((LdB*(2*n + 3))/10)*BWc*Ps*Ro*Rpd)/BWs;
Ps = @(Vo)(10^((LdB*(2*n + 3))/10)*BWs*Vo)/(BWc*Ro*Rpd);
或者您可以创建一个处理任一输入的常规函数:
function out=VoPs(name,value,BWc,BWs,Ro,n,LdB,Rpd)
if (ischar(name) && strcmp(name,'Ps')) || (isa(name,'sym') && logical(name==sym('Ps')))
Ps = value;
Vo = Ps*(BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd;
elseif (ischar(name) && strcmp(name,'Vo')) || (isa(name,'sym') && logical(name==sym('Vo')))
Vo = value;
Ps = Vo/((BWc/BWs)*Ro*(10^((-(3+2*n)*LdB)/10))*Rpd);
else
error('VoPs:InvalidName','Name input must be ''Vo'' or ''Ps''.')
end