# Thread: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

1. ## Newton-Raphson Method for Non-linear System of 3 variables in Matlab

I am trying to solve 3 non-linear system of 3 variables using the newton-raphson method in matlab. Here are the three equations:
$$c[\alpha I+ k_f+k_d+k_ns+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 \frac{P_C}{P_Q}]- c k_p \frac{P_C}{P_Q}=0$$
Solve the above systems of equation to get the values for $c,s$ and $q$? $tolerence= 10^{-4}$. initail values:$(c_0,s_0,q_0)=0$.
The values for the parameters are:
$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}$

=>
This is what I think to do in matlab, but i don't know how to start the code in matlab. I have define the function for 3 systems of equation.

$$f(c,s,q)=> c[\alpha I+ k_f+k_d+k_ns+k_p(1-q)]-I \alpha =0$$
$$g(c,s,q)=>s[\lambda_b c P_C +\lambda_r (1-q)]- \lambda_b c P_C =0$$
$$h(c,s,q)=>q[\gamma +c k_p \frac{P_C}{P_Q}]- c k_p \frac{P_C}{P_Q}=0$$

now, i have to do partial derivatives of $f(c,s,q), g(c,s,q), h(c,s,q)$ in terms of $c, s, q$.

Then set up the matrix $J$ and do the inverse matrix ($J^{-1}$). And apply the netwon iteration method, using $(c,s,q)= (c_0,s_0,q_0) - J^{-1} (f,g,h)$.

I need to do some iterations untill it converges to $tolerence= 10^{-4}$.

Can please help me how to do in matlab with the matlab code. I am beginner in matlab.

2. ## Re: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

You have to find the partial derivatives yourself and make functions for those too.
the function inv(J) finds the inverse of J.

You can do one if function to see if all three functions are close enough to 0 by using
if( (abs(f(c,s,q))<0.0001) & (abs(g(c,s,q))<0.0001) & (abs(h(c,s,q))<0.0001) )

3. ## Re: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

I am trying to solve 3 non-linear system of 3 variables using the newton-raphson method in matlab. Here are the 3 non-linear equations:

$$c[\alpha I+ k_f+k_d+k_ns+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 \frac{P_C}{P_Q}]- c k_p \frac{P_C}{P_Q}=0$$

I need to find the values of c,s, and q using the newton-raphson method.

=>
This is my matlab code :

Code:
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.
iter= 0; %iterations

%Defining the functions for c,s and q.
f(c,s,q) = c * (alpha*I + k_f + k_d + k_n * s + k_p*(1-q))-I *alpha;
g(c,s,q) = s * (lambda_b * c* P_C + lambda_r *(1-q))- lambda_b* c * P_C;
h(c,s,q) = 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.
df/dc = alpha*I + k_f + k_d + k_n * s + k_p*(1-q);
df/ds = k_n *s ;
df/dq = - k_p *c;

dg/dc = lambda_b * P_C *(s-1);
dg/ds = lambda_b * c* P_C + lambda_r *(1-q);
dg/dq = - lambda_r * s;

dh/dc = k_p *(P_C / P_Q)*(q-1);
dh/ds = 0;
dh/dq = gamma + c * k_p *(P_C / P_Q);

%Jacobian matrix
J = [df/dc df/ds df/ds; dg/ds dg/ds dg/ds; dg/ds dg/ds dg/ds];

% Defining the inverse of jacobian matrix.
A = J\;

%initial guess or values
c=1;
s=0.015;
q=0.98;
x0= [c;s;q];

% Applying the Newton-Raphson method
while abs(xnew -x0) > tol
iter= iter + 1;
xnew = x0 %Updating new values of x.
xnew = x0 - A * [f(c,s,q);g(c,s,q);h(c,s,h)];
disp(sprintf('t=%6.15f,  c=%6.15f,  s=%6.15f, q=%6.15f', iter,xnew));
end
can someone please check my code, there are some errors so, its not working. Thanks in advance.

4. ## Re: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

It has been a while since I used MATLAB but I don't think you can use punctuation in the names of variables so you should call the derivative dfdc instead of df/dc

5. ## Re: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

I have done some changes in the matlab code, but still its not working.

Code:
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 abs(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 *s ;
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 dfds; dgds dgds dgds; dhds dhds dhds];

% 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

6. ## Re: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

In the line
while abs(xnew -x0) > tol
You are comparing a vector to a scalar

You do not end the while loop

x0 is constant through all the iterations

When computing xnew you try to divide a matrix by a vector.

You need to recognize the difference between the variables c and q and s, and the vector xnew which contains the new values for those variables. c, q and s never change in your calculations but you are using them each loop as if they were updated when xnew was updated

You are in a better position to look for errors than us because you can see the error report. I'm not going to keep going through your code each time you correct it. The error reports may be difficult to understand but when you learn what they mean it is a lot easier to pick out the error.

7. ## Re: Newton-Raphson Method for Non-linear System of 3 variables in Matlab

Jacobian matrix should be
J = [dfdc dfds dfdq; dgdc dgds dgdq; dhdc dhds dhdq];

Not J = [dfdc dfds dfds; dgds dgds dgds; dhds dhds dhds];

In the line :
while abs(xnew -x0) > tol
You are right. So, it mean I have to define tol as a vector like this:
tol= [10.^4;10.^4:10.^4];
or
can I use while norm(xnew -x0) > tol instead

You do not end the while loop
=> how do I not end the while loop. So far, I manage to get only 1 iterations but unfortunately the values of c,s and q are incorrects. And I want to get at least more than 2 or 3 iterations automatically after I ran the program. So that, tolerance will stop it when it converges and it can give the accurate values of c,s and q.
so, if I take out end statement below, does tolerance will work to converge it?

x0 is constant through all the iterations
When computing xnew you try to divide a matrix by a vector.
=> I am not dividing a matrix by a vector. Here, J\ is a inverse matrix of J.