Results 1 to 7 of 7
Like Tree2Thanks
  • 1 Post By Shakarri
  • 1 Post By Shakarri

Math Help - Newton-Raphson Method for Non-linear System of 3 variables in Matlab

  1. #1
    Junior Member
    Joined
    Mar 2014
    From
    uk
    Posts
    53

    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:
    \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}
    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.


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


    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.
    Last edited by grandy; June 16th 2014 at 06:38 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    Joined
    Oct 2012
    From
    Ireland
    Posts
    640
    Thanks
    181

    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) )
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Mar 2014
    From
    uk
    Posts
    53

    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:

    \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.
    Last edited by grandy; June 17th 2014 at 05:15 AM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member
    Joined
    Oct 2012
    From
    Ireland
    Posts
    640
    Thanks
    181

    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
    Thanks from grandy
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Mar 2014
    From
    uk
    Posts
    53

    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
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Super Member
    Joined
    Oct 2012
    From
    Ireland
    Posts
    640
    Thanks
    181

    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.
    Thanks from grandy
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Mar 2014
    From
    uk
    Posts
    53

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

    yes sir. thank you for your reply. I already found my another error

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

    From your questions:
    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.
    Last edited by grandy; June 17th 2014 at 11:23 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 6
    Last Post: June 5th 2014, 05:00 PM
  2. Newton raphson method
    Posted in the Calculus Forum
    Replies: 5
    Last Post: April 4th 2011, 10:44 AM
  3. Matlab: Newton-Raphson Method
    Posted in the Math Software Forum
    Replies: 5
    Last Post: September 12th 2009, 02:34 PM
  4. Newton Raphson Method?????? HELP!
    Posted in the Calculus Forum
    Replies: 3
    Last Post: March 5th 2007, 08:37 AM
  5. Help- Newton - Raphson Method
    Posted in the Calculus Forum
    Replies: 3
    Last Post: July 12th 2006, 06:49 AM

Search Tags


/mathhelpforum @mathhelpforum