Matlab 2012b在mupadmex上求解两个方程组时速度太慢(声明)
我试图在Matlab中用for循环求解两个非线性方程组。我知道for循环可能会导致程序运行缓慢,但现在解决一个实例大约需要一个小时。调试代码时,我意识到这一行导致程序挂起:Matlab 2012b在mupadmex上求解两个方程组时速度太慢(声明),matlab,symbolic-math,mupad,Matlab,Symbolic Math,Mupad,我试图在Matlab中用for循环求解两个非线性方程组。我知道for循环可能会导致程序运行缓慢,但现在解决一个实例大约需要一个小时。调试代码时,我意识到这一行导致程序挂起: [res,status] = mupadmex(statement); 如果我终止该计划,我将获得以下信息: Operation terminated by user during mupadengine/evalin (line 97) In mupadengine/feval (line 150)
[res,status] = mupadmex(statement);
如果我终止该计划,我将获得以下信息:
Operation terminated by user during mupadengine/evalin (line 97)
In mupadengine/feval (line 150)
[S,err] = evalin(engine,stmt);
In solve (line 160) sol = eng.feval('symobj::solvefull',eqns,vars);
In algorithm2 (line 139)
answer = solve (f , g);
其中第97行表示同一行代码。下面是我调用solve函数的代码部分:
syms ro1 t_prime1
assume(t_prime1 > 0)
for i=1:1:length(n1ps)
n1p = n1ps(1,i);
n2p = n2ps(1,i);
w1 = (ro1/theta_p)*(1-exp((-1)*theta_p*t_prime1));
w2 = exp((-1)*theta_p*t_prime1);
w3 = (ro1/theta_p)*(1-exp((-1)*theta_p*T1))-q(1,1);
w4 = (1-exp((-1)*n1p*theta_p*T1))/(1-exp((-1)*theta_p*T1));
w5 = exp((-1)*theta_p*(T1-t_prime1));
Q1 = (w1 + w2*w3*w4)*w5 - q(1,1);
w6 = ((ro-ro1)/theta_p)*(1-exp((-1)*theta_p*(n1p*T1 + t_prime1 - n2p*T2)));
w7 = exp((-1)*theta_p*(n1p*T1 + t_prime1 - n2p*T2));
w8 = ((ro-ro1)/theta_p)*(1-exp((-1)*theta_p*T2))-q(1,2);
w9 = (1-exp((-1)*n2p*theta_p*T2))/(1-exp((-1)*theta_p*T2));
w10 = exp((-1)*theta_p*(T2-(n1p*T1 + t_prime1 - n2p*T2)));
Q2 = (w6 + w7*w8*w9)*w10 - q(1,2);
Q1_prime = q(1,1)*((exp((tN1 - n1p - 1)*theta_p*T1)-1)/(1-exp((-1)*theta_p*T1)));
Q2_prime = q(1,2)*((exp((tN2 - n2p - 1)*theta_p*T2)-1)/(1-exp((-1)*theta_p*T2)));
f=Q1_prime-Q1;
g=Q2_prime-Q2;
answer = solve (f , g);
end
你的问题是什么?您的代码是不可运行的,因为很多东西都没有定义。您甚至没有保存结果:例如,
answer
不是i
的函数。这个循环可能与速度缓慢无关。你能在没有循环的情况下运行一次迭代吗(只需设置i=1
)?您使用符号数学而不是像fzero
或fsolve
这样的数值方法有什么原因吗?只是一个一般性的建议;如果你想加快求解符号方程的速度,在变量上添加假设和约束,比如找到解的时间间隔,这会产生很大的不同