MATLAB How to Implement Steffensen's Method in Matlab for Solving Equations

  • Thread starter Thread starter aleee
  • Start date Start date
  • Tags Tags
    Matlab Method
AI Thread Summary
The discussion revolves around implementing Steffensen's method in MATLAB to find the root of the equation x = cos(x) with an initial value of x0 = 2 and a target of 14 iterations. The expected root is approximately 0.7390851332151607, but the user is encountering results around 10.xxxx, indicating a significant error in the output. The provided code defines a function for Steffensen's method, calculating the next iteration using a specific formula. The user tracks the iteration count and error but initially fails to converge to the expected root. Ultimately, the user mentions resolving the issue, inviting comments for further clarification.
aleee
Messages
17
Reaction score
0
i got to to write a program for steffensens method on matlab.
my equation is x=cosx and my initial value is x0=2 and got to do up to the 14 iterations.
error = x_n - a

a= 0.7390851332151607
i don't get an answer close to it.
i get something around 10.xxxx
here is my code.

function root = steff(x0,max_iterations)

format long e;
it_count = 0;
exact = 0.7390851332151607;
f = x0 - cos(x0);
g = (cos(cos(x0)) - x0 - 2*f) / f;

while it_count <= max_iterations;

x1 = x0 - f^2/(cos(cos(x0)) - x0 - 2*f);

error = x1 - exact;
iteration = [it_count x0 error]
x0 = x1;
it_count = it_count + 1;

end
format long e
root = x1
format long e
error
format long
it_count
 
Physics news on Phys.org
figured it out, comment if curious.
 
Back
Top