필터 지우기
필터 지우기

Iteration of a formula

조회 수: 20 (최근 30일)
Sam Thorpe
Sam Thorpe 2019년 2월 28일
댓글: Sam Thorpe 2019년 3월 1일
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

채택된 답변

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019년 3월 1일
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
Sam Thorpe
Sam Thorpe 2019년 3월 1일
thanks a lot yasasvi. that is brilliant.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by