# Thread Subject: Matlab question for Newton Raphson

 Subject: Matlab question for Newton Raphson From: Carlos Mallqui Date: 26 Apr, 2012 03:11:39 Message: 1 of 15 Need to do 5 iterations...for this problem not including 0 which is 1 and 0 f1(x1, x2) = 10x1sinx2 + 2 = 0 f2(x1, x2) = 10x1^2? 10x1cos x2 + 1 = 0 where x0 = [ 1 ; 0] x01 = 1 x02 = 0 Please need the code plus the first five iterations for x1 and x2.....ive been trying but...keep getting errors..and its due tomorrow lol :( at 5:30 latest
 Subject: Matlab question for Newton Raphson From: James Tursa Date: 26 Apr, 2012 03:28:23 Message: 2 of 15 "Carlos Mallqui" wrote in message ... > Need to do 5 iterations...for this problem not including 0 which is 1 and 0 > > f1(x1, x2) = 10x1sinx2 + 2 = 0 > f2(x1, x2) = 10x1^2? 10x1cos x2 + 1 = 0 > > where x0 = [ 1 ; 0] > > x01 = 1 > x02 = 0 > > > Please need the code plus the first five iterations for x1 and x2.....ive been trying but...keep getting errors..and its due tomorrow lol :( at 5:30 latest Please post your code so we can comment on it. James Tursa
 Subject: Matlab question for Newton Raphson From: prabhakaran m Date: 26 Apr, 2012 04:26:17 Message: 3 of 15 maxiter = 5; tol=1e-6 x=[1 0]%initial extimates f=(10*x(1)*sin(x(2)))+2;%function fx1=10*sin(x(2));%derivative of function w.r.t x1 fx2=10*x1*cos(x(2));%derivative of function w.r.t x2 J=[fx1 fx2] %Jacobian matrix of function xold = x(:); xnew = xold - J\f for k=1:maxiter xold = xnew; niter = k; xnew = xold - J\f; if abs(xold-xnew)/abs(xnew) < tol (niter == maxiter) break end end r = xnew like this try for the next function also
 Subject: Matlab question for Newton Raphson From: Matt J Date: 26 Apr, 2012 08:14:08 Message: 4 of 15 "prabhakaran m" wrote in message ... > > f=(10*x(1)*sin(x(2)))+2;%function > fx1=10*sin(x(2));%derivative of function w.r.t x1 > fx2=10*x1*cos(x(2));%derivative of function w.r.t x2 This should be  fx2=10*x(1)*cos(x(2)); Once I make this change, your code runs without errors. However, the code you've written attempts to solve the single equation in 2 unknowns, (10*x(1)*sin(x(2)))+2=0 which of course could have infinite solutions. Are you sure that's what you're meant to do? It's more likely that you were meant to solve the following 2 equations simultaneously, i.e., as a system of 2 equations in 2 unknowns f1(x1, x2) = 10x1sinx2 + 2 = 0 f2(x1, x2) = 10x1^2? 10x1cos x2 + 1 = 0 Doing so would make the solution more uniquely defined.
 Subject: Matlab question for Newton Raphson From: Matt J Date: 26 Apr, 2012 08:21:07 Message: 5 of 15 "prabhakaran m" wrote in message ... > maxiter = 5; > tol=1e-6 > x=[1 0]%initial extimates > f=(10*x(1)*sin(x(2)))+2;%function > fx1=10*sin(x(2));%derivative of function w.r.t x1 > fx2=10*x1*cos(x(2));%derivative of function w.r.t x2 > J=[fx1 fx2] %Jacobian matrix of function > xold = x(:); > xnew = xold - J\f > for k=1:maxiter > xold = xnew; > niter = k; > xnew = xold - J\f; > if abs(xold-xnew)/abs(xnew) < tol > (niter == maxiter) > break > end > end > r = xnew ==================== Another question you should be asking yourself is whether the Jacobian J is supposed to stay constant throughout your iterations 1:maxiter. If so, what you've implemented is called the secant method, not Newton-Raphson.
 Subject: Matlab question for Newton Raphson From: prabhakaran m Date: 26 Apr, 2012 08:51:06 Message: 6 of 15 "Matt J" wrote in message ... > "prabhakaran m" wrote in message ... > > maxiter = 5; > > tol=1e-6 > > x=[1 0]%initial extimates > > f=(10*x(1)*sin(x(2)))+2;%function > > fx1=10*sin(x(2));%derivative of function w.r.t x1 > > fx2=10*x1*cos(x(2));%derivative of function w.r.t x2 > > J=[fx1 fx2] %Jacobian matrix of function > > xold = x(:); > > xnew = xold - J\f > > for k=1:maxiter > > xold = xnew; > > niter = k; > > xnew = xold - J\f; > > if abs(xold-xnew)/abs(xnew) < tol > > (niter == maxiter) > > break > > end > > end > > r = xnew > ==================== > > > Another question you should be asking yourself is whether the Jacobian J is supposed to stay constant throughout your iterations 1:maxiter. If so, what you've implemented is called the secant method, not Newton-Raphson. Yes Matt, you are right i tried to solve 2 equation with 2 unknowns only, since i am a beginner, i saw this question and tried myself doing newton raphson. and thank you very much for your correction, i had learned few things form it, and the Jacobian should change its value on each iteration, it should not have same value,. may i know what change i should do to make it Newton raphson method
 Subject: Matlab question for Newton Raphson From: prabhakaran m Date: 26 Apr, 2012 09:45:08 Message: 7 of 15 maxiter = 5; tol=1e-6 x=[1 0]%initial extimates xold = x(:); for k=1:maxiter % xold = xnew;     f1=(10*x(1)*sin(x(2)))+2;%function     f2=10*x(1)*cos(x(2))+1;%function     f1x1=10*sin(x(2));%derivative of function w.r.t x1     f1x2=10*x(1)*cos(x(2));%derivative of function w.r.t x2     f2x1=10*cos(x(2));     f2x2=-10*x(1)*sin(x(2));     f=[f1 f2]'     J =[f1x1 f1x2; f2x1 f2x2] %Jacobian matrix of function     xnew = xold - J\f;     x=xnew     if abs(xold-xnew)/abs(xnew) < tol         r=xnew         break     end end i tried to solve with one equation itself. after reading your comment only i came to know my blunt. now i tried 2 equation with 2 unknowns, and the value of J is changing in each iterations. am i right now ??
 Subject: Matlab question for Newton Raphson From: Carlos Mallqui Date: 26 Apr, 2012 12:32:25 Message: 8 of 15 "Matt J" wrote in message ... > "prabhakaran m" wrote in message ... > > maxiter = 5; > > tol=1e-6 > > x=[1 0]%initial extimates > > f=(10*x(1)*sin(x(2)))+2;%function > > fx1=10*sin(x(2));%derivative of function w.r.t x1 > > fx2=10*x1*cos(x(2));%derivative of function w.r.t x2 > > J=[fx1 fx2] %Jacobian matrix of function > > xold = x(:); > > xnew = xold - J\f > > for k=1:maxiter > > xold = xnew; > > niter = k; > > xnew = xold - J\f; > > if abs(xold-xnew)/abs(xnew) < tol > > (niter == maxiter) > > break > > end > > end > > r = xnew > ==================== > > > Another question you should be asking yourself is whether the Jacobian J is supposed to stay constant throughout your iterations 1:maxiter. If so, what you've implemented is called the secant method, not Newton-Raphson. thanks but...which one should i try inputting into Matlab...im guessing Matts'....also...the questions is use Newtons Raphsons method and solve x1 and x2 5 iterations...nothing else aside of that...o well and the given initial conditions
 Subject: Matlab question for Newton Raphson From: Carlos Mallqui Date: 26 Apr, 2012 13:29:26 Message: 9 of 15 "prabhakaran m" wrote in message ... > maxiter = 5; > tol=1e-6 > x=[1 0]%initial extimates > xold = x(:); > for k=1:maxiter > % xold = xnew; > f1=(10*x(1)*sin(x(2)))+2;%function > f2=10*x(1)*cos(x(2))+1;%function > f1x1=10*sin(x(2));%derivative of function w.r.t x1 > f1x2=10*x(1)*cos(x(2));%derivative of function w.r.t x2 > f2x1=10*cos(x(2)); > f2x2=-10*x(1)*sin(x(2)); > f=[f1 f2]' > J =[f1x1 f1x2; f2x1 f2x2] %Jacobian matrix of function > xnew = xold - J\f; > x=xnew > if abs(xold-xnew)/abs(xnew) < tol > r=xnew > break > end > end > > i tried to solve with one equation itself. after reading your comment only i came to know my blunt. now i tried 2 equation with 2 unknowns, and the value of J is changing in each iterations. am i right now ?? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> this is...the example the professor posted...for a similar problem...with some code...maybe it will help answer matts question...whethere he wants them together or not..... clear, clc x = [1;1]; % initial conditions nit=10; % number of iterations for i=1:nit f(:,i) = [x(1,i)*x(1,i)-x(2,i)-1; x(2,i)*x(2,i)-x(1,i)-1]; delta = 4*x(1,i)*x(2,i)-1; Jinv = (1/delta)*[ 2*x(2,i) 1; 1 2*x(1,i)]; dx(:,i) = -Jinv*f(:,i); x(:,i+1) = x(:,i) + dx(:,i); end numofit = (0:nit); output = [numofit; x]; fprintf(’\n nofit \t x(1) \t x(2) \n’); fprintf(’-------------------------------\n’); fprintf(’%7.0f \t %7.4f \t %7.4f\n’,output); Use the Newton-Raphson method to solve f1(x1, x2) = x1^2? x2 ? 1 = 0 f2(x1, x2) = x2^2? x1 ? 1 = 0 intial conditions are both 1 in the solution the find the J marix....as well Delta...etc but this is the code for the iterations where nit is number of iterations... please help moi..thanks :D
 Subject: Matlab question for Newton Raphson From: Matt J Date: 26 Apr, 2012 13:29:26 Message: 10 of 15 "prabhakaran m" wrote in message ... > > i tried to solve with one equation itself. after reading your comment only i came to know my blunt. now i tried 2 equation with 2 unknowns, and the value of J is changing in each iterations. am i right now ?? ============== I think you're computing J correctly, but now xold is not getting updated with every iteration. You should be able to check whether everything is working by running with large maxiter and seeing if it converges to a solution.
 Subject: Matlab question for Newton Raphson From: Carlos Mallqui Date: 26 Apr, 2012 13:45:12 Message: 11 of 15 "Matt J" wrote in message ... > "prabhakaran m" wrote in message ... > > > > i tried to solve with one equation itself. after reading your comment only i came to know my blunt. now i tried 2 equation with 2 unknowns, and the value of J is changing in each iterations. am i right now ?? > ============== > > I think you're computing J correctly, but now xold is not getting updated with every iteration. > > You should be able to check whether everything is working by running with large maxiter and seeing if it converges to a solution. >>>>>>> is it updating fine up to a certain iteration?....
 Subject: Matlab question for Newton Raphson From: Matt J Date: 26 Apr, 2012 15:13:21 Message: 12 of 15 "Carlos Mallqui" wrote in message ... > > > is it updating fine up to a certain iteration?.... You tell me.
 Subject: Matlab question for Newton Raphson From: Carlos Mallqui Date: 26 Apr, 2012 17:02:07 Message: 13 of 15 "Matt J" wrote in message ... > "Carlos Mallqui" wrote in message ... > > > > > > is it updating fine up to a certain iteration?.... > > You tell me. >>>>> x =    -0.1000    -0.2000 x =     1.0417     2.1588 x =    -0.1527     0.1864 x =     1.0173     1.1660 x =    -0.2406     0.0129 im thinking it updates for....the 2nd and 4th iteration..the other numbers...that it spits out are just...odd :S
 Subject: Matlab question for Newton Raphson From: Matt J Date: 26 Apr, 2012 17:24:07 Message: 14 of 15 "Carlos Mallqui" wrote in message ... > > x = > > -0.1000 > -0.2000 > > x = > > 1.0417 > 2.1588 > > x = > > -0.1527 > 0.1864 > > x = > > 1.0173 > 1.1660 > > x = > > -0.2406 > 0.0129 > > im thinking it updates for....the 2nd and 4th iteration..the other numbers...that it spits out are just...odd :S =========== Here you're displaying the progress of the variable x. I asked you to check the progress of xold. You will see that it is not changing at all throughout the iterations, because there is no statement in the for-loop that updates it.
 Subject: Matlab question for Newton Raphson From: Carlos Mallqui Date: 26 Apr, 2012 17:31:22 Message: 15 of 15 "Matt J" wrote in message ... > "Carlos Mallqui" wrote in message ... > > > > x = > > > > -0.1000 > > -0.2000 > > > > x = > > > > 1.0417 > > 2.1588 > > > > x = > > > > -0.1527 > > 0.1864 > > > > x = > > > > 1.0173 > > 1.1660 > > > > x = > > > > -0.2406 > > 0.0129 > > > > im thinking it updates for....the 2nd and 4th iteration..the other numbers...that it spits out are just...odd :S > =========== > > Here you're displaying the progress of the variable x. > I asked you to check the progress of xold. > > You will see that it is not changing at all throughout the iterations, because there is no statement in the for-loop that updates it. >>>>>> Matlab and me dont go along lol...ill have to learn at some other time i have this due in like few hours hours..ill have to make up something and go for part marks...if i at least had the values for 5 iterations for x1 and x2 would be great...if not thanks for taking time in replying..Both

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.