Iteration of a formula

35 vues (au cours des 30 derniers jours)
Sam Thorpe
Sam Thorpe le 28 Fév 2019
Commenté : Sam Thorpe le 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

Réponse acceptée

Yasasvi Harish Kumar
Yasasvi Harish Kumar le 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 commentaire
Sam Thorpe
Sam Thorpe le 1 Mar 2019
thanks a lot yasasvi. that is brilliant.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by