我试图在MATLAB中使用Newton-Raphson方法求解3个变量的3个非线性系统。以下是3个非线性方程:
c * (alpha*I + k_f + k_d + k_n * s + k_p*(1-q))-I *alpha = 0
s * (lambda_b * c* P_C + lambda_r *(1-q))- lambda_b* c * P_C = 0
q * ( gamma + c * k_p *(P_C / P_Q))- (c * k_p * (P_C / P_Q)) = 0
我需要使用Newton-Raphson方法找到c
,s
和q
的值。
这是我到目前为止所拥有的:
format long
clear;
%values of parameters
I=1200;
k_f= 6.7*10.^7;
k_d= 6.03*10.^8;
k_n=2.92*10.^9;
k_p=4.94*10.^9;
lambda_b= 0.0087;
lambda_r =835;
gamma =2.74;
alpha =1.14437*10.^-3;
P_C= 3 * 10.^(11);
P_Q= 2.87 * 10.^(10);
tol = 10.^-4; %tol is a converge tolerance
%initial guess or values
c=1;
s=0.015;
q=0.98;
x0= [c;s;q];
iter= 0; %iterations
xnew =[100;100;100];
while norm(xnew -x0) > tol
iter= iter + 1;
%Defining the functions for c,s and q.
f = c * (alpha*I + k_f + k_d + k_n * s + k_p*(1-q))-I *alpha;
g = s * (lambda_b * c* P_C + lambda_r *(1-q))- lambda_b* c * P_C;
h = q * ( gamma + c * k_p *(P_C / P_Q))- (c * k_p * (P_C / P_Q));
%Partial derivatives in terms of c,s and q.
dfdc = alpha*I + k_f + k_d + k_n * s + k_p*(1-q);
dfds = k_n *c ;
dfdq = - k_p *c;
dgdc = lambda_b * P_C *(s-1);
dgds = lambda_b * c* P_C + lambda_r *(1-q);
dgdq = - lambda_r * s;
dhdc = k_p *(P_C / P_Q)*(q-1);
dhds = 0;
dhdq = gamma + c * k_p *(P_C / P_Q);
%Jacobian matrix
J = [dfdc dfds dfdq; dgdc dgds dgdq; dhdc dhds dhdq];
% Applying the Newton-Raphson method
xnew = x0 - J\[f;g;h];
disp(sprintf('iter=%6.15f, c=%6.15f, s=%6.15f, q=%6.15f', iter,xnew));
end
有人可以检查我的代码,有一些错误,所以,它无法正常工作。提前致谢。
答案 0 :(得分:1)
您没有在迭代之间更新c
,s
和q
(x0
)的猜测。请尝试以下方法:
%initial guess or values
c=1;
s=0.015;
q=0.98;
xnew =[c;s;q];
xold = zeros(size(xnew));
while norm(xnew - xold) > tol
iter= iter + 1;
xold = xnew;
% update c, s, and q
c = xold(1);
s = xold(2);
q = xold(3);
%Defining the functions for c,s and q.
f = c * (alpha*I + k_f + k_d + k_n * s + k_p*(1-q))-I *alpha;
g = s * (lambda_b * c* P_C + lambda_r *(1-q))- lambda_b* c * P_C;
h = q * ( gamma + c * k_p *(P_C / P_Q))- (c * k_p * (P_C / P_Q));
%Partial derivatives in terms of c,s and q.
dfdc = alpha*I + k_f + k_d + k_n * s + k_p*(1-q);
dfds = k_n *c ;
dfdq = - k_p *c;
dgdc = lambda_b * P_C *(s-1);
dgds = lambda_b * c* P_C + lambda_r *(1-q);
dgdq = - lambda_r * s;
dhdc = k_p *(P_C / P_Q)*(q-1);
dhds = 0;
dhdq = gamma + c * k_p *(P_C / P_Q);
%Jacobian matrix
J = [dfdc dfds dfdq; dgdc dgds dgdq; dhdc dhds dhdq];
% Applying the Newton-Raphson method
xnew = xold - J\[f;g;h];
disp(sprintf('iter=%6.15f, c=%6.15f, s=%6.15f, q=%6.15f', iter,xnew));
end
我在上面将x0
更改为xold
,并确保在每次循环迭代时更新它。基本上,您正试图使用此方法“推测”您的猜测更接近标称值。查看this website以获得有关此算法所涉及的基本概念的详细讨论。
通过上述更改,您的代码在六次迭代后最终收敛:
iter=1.000000000000000, c=0.000000000389029, s=0.015000000287216, q=0.979999999955780
iter=2.000000000000000, c=0.000000001356998, s=0.158028331191731, q=0.923765241962920
iter=3.000000000000000, c=0.000000001181617, s=0.104156261426515, q=0.952886937302707
iter=4.000000000000000, c=0.000000001216663, s=0.085849634576983, q=0.958360887077671
iter=5.000000000000000, c=0.000000001224388, s=0.084367460093642, q=0.958463129596494
iter=6.000000000000000, c=0.000000001224423, s=0.084367992582976, q=0.958463625488450