简体   繁体   English

数值求解非线性方程

[英]Solving a nonlinear equation numerically

I have the following equation to solve: 我有以下方程式可以解决:

 aRatio = ((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient * m^2))^num_exponent))/ denom);

where variables aRatio , sqrt_gamma_ratio , squared_m_coefficient , num_exponent , and denom are all known constants (1, 1.046, .14, 4.07, and 1.728, respectively). 其中变量aRatiosqrt_gamma_ratiosquared_m_coefficientnum_exponentdenom均为已知常数(分别为squared_m_coefficientnum_exponentdenom )。 Just before the code above, m is defined symbolically. 在上面的代码之前, m是符号定义的。 It's a portion of some code of mine that includes iteration, so I need some help coming up with a generalized approach to approximate m (doesn't have to be too accurate, I'd say to the hundredths place is fine). 这是我的一些代码中包含迭代的一部分,因此我需要一些帮助,提出一种通用方法来近似m (不必太精确,我可以说是百分之一百了)。 I've tried using the vpasolve function as follows: 我试过使用vpasolve函数,如下所示:

f = vpasolve(aRatio == ((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient * m^2))^num_exponent))/ denom), m);

but it just returns 但它只是返回

Empty sym: 0-by-1

So, Matlab doesn't seem to be able to solve it symbolically. 因此,Matlab似乎无法象征性地解决它。 I'm guessing I have to take the numerical approach, which I'm much less familiar with. 我猜想我必须采用数值方法,而我对此不太熟悉。 The long term objective is to establish m as a function of aRatio . 长期目标是确定m作为aRatio的函数。 Eventually I hope to create an array of values for aRatio , whose range will be from 1-1000, in intervals of 10. This way I can (hopefully) generate a plot of aRatio vs. m . 最终,我希望为aRatio创建一个值数组,其范围为1-1000,间隔为10。这样,我可以(希望地)生成aRatiom This will be accomplished through iteration with a while loop. 这将通过使用while循环的迭代来完成。 I plan on changing a few other variables, but I think if I can get help with just this portion of the problem, it could go a long way. 我计划更改其他一些变量,但是我认为,如果我能仅就这一部分问题寻求帮助,那将有很长的路要走。

My question is why can't Matlab solve this relatively simple equation symbolically and what alternatives do I have? 我的问题是为什么Matlab不能用符号方式解决这个相对简单的方程式,我有什么选择? I've googled this extensively and I am very confused. 我已经在Google上进行了广泛的搜索,我感到非常困惑。

EDIT: here is the relevant code 编辑:这是相关的代码

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));

the following code is for debugging purposes, it belongs in the while loop. 以下代码用于调试目的,它属于while循环。 I presume that I need a nested while loop to iterate the gamE, but that really isn't my issue right now. 我想我需要一个嵌套的while循环来迭代gamE,但是现在这确实不是我的问题。 All I want is to get a value back for m, which I've yet to do at all 我想要做的就是找回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

Note: the values stored above are just from the first iteration of gamE, and will be iterated in the finish product as well, but I'm pretty comfortable with loops; 注意:上面存储的值仅来自gamE的第一次迭代,并且也会在最终产品中进行迭代,但是我对循环很满意。 it's Matlab that's giving me headaches! Matlab让我头疼!

f =((sqrt_gamma_ratio * (1/m) * ((1 + (squared_m_coefficient* 
m^2))^num_exponent))/ denom)-aRatio;
fn = fzero(matlabFunction(f),1);

when I run, I get this: 当我跑步时,我得到以下信息:

Operands to the || and && operators must be convertible to logical scalar values.

I'll be honest I have no idea what that means or how to fix it. 老实说,我不知道这意味着什么或如何解决。 I imagine I HAVE to be close at least? 我想我至少要亲近吗?

Below is just the while loop I hope to one day complete. 下面只是while循环,我希望可以完成一天。 For those of you who are interested I'm trying to get roughly 8000 data points, I'll store them all one array, but it's basically ALL ABOUT finding m as a function of aRatio, and once I get that figured out I just have to iterate the gamE variable above, which should be a piece of cake. 对于那些有兴趣的人,我想获取大约8000个数据点,我将它们全部存储在一个数组中,但是基本上所有关于将m作为aRatio的函数进行查找,一旦我弄清楚了,我就拥有了迭代上面的gamE变量,这应该是小菜一碟。 PS you can laugh at my unsatisfactory loop below if you like. 附言:如果您愿意,您可以嘲笑我下面令人不满意的循环。

%{
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);

Any ideas ? 有任何想法吗 ?

You used vpasolve , but claim that it did not work, so I decided to test solve, and then vpa , and this is what I get: 您使用了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM