How can i modify this code so x can = 0
    4 views (last 30 days)
  
       Show older comments
    
function qw=fNR(x)
qw=zeros(100,1)
qw(2)=x
i=2
while abs(qw(i)-qw(i-1))>1e-6
    qw(i+1)=qw(i)-ff1(qw(i))/fdf1(qw(i))
    i=i+1
end
qw=qw(2:i-1)
[qw(end) i] 
plot(qw)
end 
how can i model this functio so it can take fNR(0)
fdf1 
function y=fdf1(x)
y=3*x.^2-12*x+11;
end 
ff1
function y=ff1(x)
y=x.^3-6*x.^2+11*x-5;
end 
0 Comments
Answers (1)
  Image Analyst
      
      
 on 12 Feb 2022
        Try this:
function qw=fNR(x)
qw=zeros(100,1)
qw(2)=x
i=2
maxIterations = 1000;
loopCounter = 0;
while (abs(qw(i)-qw(i-1))>1e-6) && loopCounter < maxIterations
    qw(i+1)=qw(i)-ff1(qw(i))/fdf1(qw(i))
    i=i+1
    loopCounter = loopCounter + 1;
end
qw=qw(2:i-1)
if isempty(qw)
    message = sprintf('qw is empty')
    uiwait(warndlg(message));
    return;
end
[qw(end) i]
plot(qw)
end
5 Comments
  DGM
      
      
 on 12 Feb 2022
				Oops.  I didn't realize I was commenting on the answer instead of on the question.  
  Image Analyst
      
      
 on 13 Feb 2022
				@Muhammad Choudhury I know that was your error with the original code.  Obviously you never ran my code because my code throws up an alert that the array is empty rather than your error.
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

