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:

\begin{equation} c[\alpha I+ k_f+k_d+k_ns+k_p(1-q)]-I \alpha =0 \end{equation}

\begin{equation} s[\lambda_b c P_C +\lambda_r (1-q)]- \lambda_b c P_C =0 \end{equation}

\begin{equation} q[\gamma +c k_p \frac{P_C}{P_Q}]- c k_p \frac{P_C}{P_Q}=0 \end{equation}

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.