Iteration of a formula

36 views (last 30 days)
Sam Thorpe
Sam Thorpe on 28 Feb 2019
Commented: Sam Thorpe on 1 Mar 2019
I have the following function y=4*(1-cos(x)).^2+x.^3. Using newtonian iteration how can iterate the formula 100 times with initial guess of x=4?
I have the following code so far which gets me the first root, but im not sure how to progress from here.
maxIter=100'; %number of iterations
x=4; %initial guess
i=0;
f=4*(1-cos(x)).^2+x.^3 %function
dfx=3*x.^2+8*sin(x)*(1-cos(x)) %derivative of function
y=x-(f/dfx)
newx=y
x=newx
i=i+1
Thanks for any help

Accepted Answer

Yasasvi Harish Kumar
Yasasvi Harish Kumar on 1 Mar 2019
Hi,
All you need is a loop. This should fix your problem.
maxIter=100'; %number of iterations
i = 1;
x(i)=4; %initial guess
while i<=maxIter
f=4*(1-cos(x(i))).^2+x.^3 %function
dfx=3*x(i)^2+8*sin(x(i))*(1-cos(x(i))) %derivative of function
y=x(i)-(f/dfx)
i=i+1
x(i) = y;
end
x is an array with all the roots.
Regards
  1 Comment
Sam Thorpe
Sam Thorpe on 1 Mar 2019
thanks a lot yasasvi. that is brilliant.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!