1

I have a secant method in for loop trying to change it to while loop but problem is just showing one iteration and I want to make it like for loop but in while loop.

The code of For loop:

    clc;clear;
    x0=1;
    x1=0;
    f=@(x) 3*x+sin(x)-exp(x)
    if abs(x0) <abs(x1)
        t=x0;
        x0=x1;
        x1=t;
    end
    for i=1:5
        x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))))
        disp([x0,  x1,  x2,  f(x2)]);
        x0=x1;
        x1=x2;
    end

and this is my while loop code:

clc; clear;
 x0=1;
 x1=0;
 error=1
 tol=0.00001;
 f=@(x) 3*x+sin(x)-exp(x)
 if abs(f(x0)) < abs(f(x1))
     t=x0;
     x0=x1;
     x1=t;
 end
 x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))))
 while(abs(f(x2))<tol)

         disp(x1);

         x0=x1;
         x1=x2;
 end

4 Answers4

0

The reason you don't enther the while loop is because abs(f(x2))<tol isn't satisfied. I guess you want to refine your result till the next step is smaller than tol. In that case you should use while(abs(f(x2))>tol), because then, the loop will repeat itself till abs(f(x2))<=tol. Also don't forgot to put the function of x2 inside the while-loop, otherwise x2 won't be updated, and you'll loop indefinitely.

ViG
  • 1,848
  • 1
  • 11
  • 13
0

First you are not modifying x2 inside the loop so your while loop will either not run or loop endlessly. As ViG pointed out your logical condition is backwards as you want to loop while your error is greater than the tolerance. Also, it is a good idea to add a maximum loop count to the while condition in the event it does not converge. Finally you defined error but never used it. So I made it more clear what the code is doing by adding it to the loop for readability.

x0=1;
x1=0;
error=1;
tol=0.00001;
f=@(x) 3*x+sin(x)-exp(x);
loopCount = 0;
maxNumLoops = 100;

if abs(f(x0)) < abs(f(x1))
    t=x0;
    x0=x1;
    x1=t;    
end
while(abs(error)>tol) && loopCount < maxNumLoops  
    loopCount = loopCount +1;
    x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))));
    error = f(x2);
    x0=x1;
    x1=x2;    
end
if loopCount >= maxNumLoops  
   disp('Method did not converge!!') 
end
Aero Engy
  • 3,588
  • 1
  • 16
  • 27
  • thank you so much, i have a newton method to in for but i don't know how to make it in while can you help me with it too? –  Feb 02 '18 at 13:43
  • You can post it as a new question & I will take a look when I get some time. Otherwise someone else might get to it first. However, you might want to use this site's search function. There are several Matlab Newton Raphson method question son here already that might contain what you are looking for. Ex. https://stackoverflow.com/a/5640295/1106634 – Aero Engy Feb 02 '18 at 15:45
0

I have the following code, maybe it helps you:

secant <- function(fn, x0, x1, tol, max.iter){
xn.min1 <- x0
xn <- x1
iter <- 1
while((abs(xn-xn.min1) > tol) && (iter < max.iter)){
  xn.plus1 <- xn - f(xn)*(xn-xn.min1)/(f(xn)-f(xn.min1))
  xn.min1 <- xn
  xn <- xn.plus1
  iter <- iter + 1
}
if (abs(xn.plus1 - xn) > tol){
  cat("failed to converge\n")
  return(NULL)
} else {
  return(xn.plus1)
}
0

There is something wrong with your stopping criteria. See my code for a working example.

x.n.minus.1 <- 0.5
x.n <- 6
iter <- 1
while ((abs(x.n-x.n.minus.1) > tol) && (iter < max.iter)) {
  x.n.plus.1 <- x.n - f.acc(x.n)*(x.n-x.n.minus.1)/(f.acc(x.n)-f.acc(x.n.minus.1))
  x.n.minus.1 <- x.n
  x.n <- x.n.plus.1
  iter <- iter + 1
  cat("At iteration", iter, "value of x.n is:", x.n, "\n")
}