Writing an M-file for an iteration of x=cos(x)

20 views (last 30 days)
I've been asked this question: Find an iterative formula (using Newton-Raphson for example) for solving the equation x = cos(x) and write an m-file for finding the solution whereby x matches cos(x) to at least 8 decimal places. Using format long give your solution for x and the corresponding value for cos(x). I don't have that much experience with M-files. I've been trying to start this by getting Matlab to work out where y=x and y=cos(x) cross for a starting point for Newton-Raphson, I'm not sure how to get this to work in an M-file with the rest which is needed. Also, I'm not sure how to write the rest of the file, particularly how to make matlab stop the iteration once x matches cos(x) to 8 decimal places. Is there a particularly good Matlab book anyone would recommend I buy?

Answers (2)

Matt Tearle
Matt Tearle on 20 Mar 2011
Check out this discussion for some suggestions on learning resources.
For this specific problem, I'd suggest trying to write one iteration first. Once you have that, wrap it in a loop - free hint: you'll need a while loop. You'll also want the abs function for the stopping criterion.

Aaron
Aaron on 20 Mar 2011
I have had to do this assignment as well. The way I did was using the infinite series equation for cosine. I used a for loop and constrained it so that if the next term in the series changed the answer by less than 0.00000001 it would stop and give you the answer. Hope it helps.

Categories

Find more on Mathematics 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!