# I need help please !! Newton method

조회 수: 1(최근 30일)
Thierry Gelinas 2015년 4월 11일
댓글: Thierry Gelinas 2015년 4월 12일
Hi , I know there's a lot of subjects on this but I didn't find what I'm looking for... This is my problem: I have create this code and it works perfectly well :
fonc=inline('fonction');
dfonc=inline('derivée de la fonction');
x=0.8;
;xn=0;
eps=1e-6;
k=0;
fprintf(' k x_k f(x_k) \n');
fprintf('%4d %12.4e %12.4e \n',k, x,fonc(x));
k=k+1;
while abs(x-xn) > eps & k < 20
xn=x;
x=x-fonc(x)/dfonc(x);
fprintf('%4d %12.4e %12.4e \n',k, x,fonc(x));
k=k+1;
end
In this case fonction = x^4-1.3*x^3-2.97*x^2+5.929*x-2.662 and dérivée de la fonction is 4*x^3-3.9*x^2-5.94*x+5.929
k x_k f(x_k)
0 8.0000e-01 -7.5600e-02
1 9.0370e-01 -2.1963e-02
2 9.7064e-01 -6.4300e-03
3 1.0144e+00 -1.8908e-03
4 1.0432e+00 -5.5753e-04
5 1.0623e+00 -1.6467e-04
6 1.0749e+00 -4.8691e-05
7 1.0833e+00 -1.4407e-05
8 1.0889e+00 -4.2649e-06
9 1.0926e+00 -1.2629e-06
10 1.0951e+00 -3.7405e-07
...
29 1.1000e+00 0.0000e+00
My problem is that I have to add a new thing to the table that would be create by this fonction:
abs((xn2-xn)/(xn-x))
and I don't know how to do it... I really need help. It should be looking like this
k x_k f(x_k) |En+1/En|
0 8.0000e-01 -7.5600e-02 0,645515911
1 9.0370e-01 -2.1963e-02 0,653719749
2 9.7064e-01 -6.4300e-03 0,658135283
3 1.0144e+00 -1.8908e-03 0,663194444
4 1.0432e+00 -5.5753e-04 0,659685864
5 1.0623e+00 -1.6467e-04 ...
6 1.0749e+00 -4.8691e-05 ...
7 1.0833e+00 -1.4407e-05 ...
8 1.0889e+00 -4.2649e-06 ...
9 1.0926e+00 -1.2629e-06 ...
10 1.0951e+00 -3.7405e-07 ...
11 1.0967e+00 -1.1080e-07
Thank you. ( by the way , english is not my native language so I'm sorry for all the mistakes I may have made. )
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Thierry Gelinas 2015년 4월 11일
편집: Thierry Gelinas 2015년 4월 11일
Hi , xn2 represent the second point found with the newton method. For example :
x 0 8.0000e-01
xn 1 9.0370e-01
xn2 2 9.7064e-01
So the first iteration would be ( with abs((xn2-xn)/(xn-x)) )
abs((9.7064e-01- 9.0370e-01 )/( 9.0370e-01 - 8.0000e-01 )
Is it more clear now ?

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

### 채택된 답변

Geoff Hayes 2015년 4월 12일
Thierry - in order to calculate this new value, you will need to save the data from the previous iterations. So outside of the while loop, create a variable for the historical data as
maxIters = 20;
histData = zeros(maxIters+1,1);
histData(1) = x;
then inside the while loop handle the case for when you have enough data to do the calculation (i.e. at least three historical points)
while abs(x-xn) > eps && k < maxIters
xn = x;
x = x-fonc(x)/dfonc(x);
histData(k+1) = x;
fprintf('%4d %12.4e %12.4e ',k, x,fonc(x));
if k==1
fprintf('\n');
else
fprintf('%12.4e\n',abs((histData(k+1)-histData(k))/(histData(k)-histData(k-1))));
end
k = k + 1;
end
Try the above and see what happens!
##### 댓글 수: 1표시숨기기 없음
Thierry Gelinas 2015년 4월 12일
Thank you ! You are a MVP. Have a nice day !

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

### 범주

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

Translated by