Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help with matlab code for newton-raphson (need help really bad)

  1. Jul 1, 2012 #1
    The code runs but I get the wrong answers can you take a look at the code for this .

    ANSWERS
    ******************
    x(1) = 1.035
    x(2) = 1.086
    x(3) = 0.927
    ******************

    clc
    clear


    F = @(x) [15*x(1)+(x(2)^2)-4*x(3)-13;...
    (x(1)^2)+10*x(2)^2-x(3)-11;...
    x(2)^2-25*x(3)+22];
    J = @(x) [15+2*x(2)-4;2*x(1)+10-1;0+2*x(2)-25];

    xo = [1 1 1];
    n = 4;


    es = .5*10^(2-n);

    [xn,cnt] = newton_raphson(es,xo,J,F);


    fprintf('******************\n')
    for i = 1:length(xo)
    fprintf(' x(%d) = %.3f\n',i',xn(i)')
    end
    fprintf('******************\n')
    fprintf('It took %d iterations to converge!\n',cnt')

    FUNCTION PROGRAM


    function [xn, cnt] = newton_raphson(es,xo,J,F)

    cnt = 0;
    ea = 1;

    while ea > es
    delx = J(xo)\-F(xo);
    xn = xo + delx;

    ea = 100*max(abs((xn - xo) ./ xn));
    xo = xn;
    cnt = cnt + 1;

    end
     
  2. jcsd
  3. Jul 1, 2012 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    J should be a 3 x 3 matrix of derivatives with ##J_{ij} = \partial F_i / \partial x_j##, not a vector.
     
  4. Jul 1, 2012 #3
    Ok then after now I get answers like inf inf and nan
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Need help with matlab code for newton-raphson (need help really bad)
  1. Need help with this (Replies: 0)

  2. NEED help (Replies: 3)

Loading...