[英]Solving a nonlinear equation numerically
我有以下方程式可以解决:
aRatio = ((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient * m^2))^num_exponent))/ denom);
其中变量aRatio
, sqrt_gamma_ratio
, squared_m_coefficient
, num_exponent
和denom
均为已知常数(分别为squared_m_coefficient
, num_exponent
和denom
)。 在上面的代码之前, m
是符号定义的。 这是我的一些代码中包含迭代的一部分,因此我需要一些帮助,提出一种通用方法来近似m
(不必太精确,我可以说是百分之一百了)。 我试过使用vpasolve
函数,如下所示:
f = vpasolve(aRatio == ((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient * m^2))^num_exponent))/ denom), m);
但它只是返回
Empty sym: 0-by-1
因此,Matlab似乎无法象征性地解决它。 我猜想我必须采用数值方法,而我对此不太熟悉。 长期目标是确定m
作为aRatio
的函数。 最终,我希望为aRatio
创建一个值数组,其范围为1-1000,间隔为10。这样,我可以(希望地)生成aRatio
对m
。 这将通过使用while循环的迭代来完成。 我计划更改其他一些变量,但是我认为,如果我能仅就这一部分问题寻求帮助,那将有很长的路要走。
我的问题是为什么Matlab不能用符号方式解决这个相对简单的方程式,我有什么选择? 我已经在Google上进行了广泛的搜索,我感到非常困惑。
编辑:这是相关的代码
gam0 = 1.4; %specific heat ratio in middle of nozzle
gamE = 1.28:-.01:1.20 ; %establish exit sp heat ratio variation
denom = 1.728; % this is the denominator of the given eqn, evaluated at g* =
1.4
aRatio = 1:10:1001;
count = 1;
mach_number = zeros(1,length(aRatio));
以下代码用于调试目的,它属于while循环。 我想我需要一个嵌套的while循环来迭代gamE,但是现在这确实不是我的问题。 我想要做的就是找回m的值,而我还没有做
sqrt_gamma_ratio = sqrt(gam0 / gamE(1)); %1.046
squared_m_coefficient = .5 * (gamE(1) - 1); %.14
num_exponent = (gamE(1) + 1)/(2 * (gamE(1) - 1)); %4.07
注意:上面存储的值仅来自gamE的第一次迭代,并且也会在最终产品中进行迭代,但是我对循环很满意。 Matlab让我头疼!
f =((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient*
m^2))^num_exponent))/ denom)-aRatio;
fn = fzero(matlabFunction(f),1);
当我跑步时,我得到以下信息:
Operands to the || and && operators must be convertible to logical scalar values.
老实说,我不知道这意味着什么或如何解决。 我想我至少要亲近吗?
下面只是while循环,我希望可以完成一天。 对于那些有兴趣的人,我想获取大约8000个数据点,我将它们全部存储在一个数组中,但是基本上所有关于将m作为aRatio的函数进行查找,一旦我弄清楚了,我就拥有了迭代上面的gamE变量,这应该是小菜一碟。 附言:如果您愿意,您可以嘲笑我下面令人不满意的循环。
%{
while( count < length(compressive_area_ratio)+1 )
sqrt_gamma_ratio = sqrt(gam0 / gamE(1));
squared_m_coefficient = .5 * (gamE(1) - 1);
num_exponent = (gamE(1) + 1)/(2 * (gamE(1) - 1));
this_M = solve(compressive_area_ratio(count) == ((sqrt_gamma_ratio *
(1/m) * ((1 + (squared_m_coefficient * m^2))^num_exponent))/ denom), m );
disp(this_M);
%mach_number(count) = this_M;
count = count + 1;
end
%}
%plot(compressive_area_ratio, mach_number);
有任何想法吗 ?
您使用了vpasolve
,但是声称它不起作用,所以我决定先测试Solve,然后再测试vpa
,这就是我得到的:
gam0 = 1.4; %specific heat ratio in middle of nozzle
gamE = 1.28; %establish exit sp heat ratio variation
denom = 1.728; % this is the denominator of the given eqn, evaluated at g* = 1.4
aRatio = 1;
count = 1;
mach_number = zeros(1,length(aRatio));
sqrt_gamma_ratio = sqrt(gam0 / gamE(1)); %1.046
squared_m_coefficient = .5 * (gamE(1) - 1); %.14
num_exponent = (gamE(1) + 1)/(2 * (gamE(1) - 1)); %4.07
syms m
sol=solve(((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient * m^2))^num_exponent))/ denom)-aRatio);
vpa(sol)
> 4.3269393310990630350495077697516i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.