I need help please !! Newton method
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2015-4-11
Thierry - in your equation
abs((xn2-xn)/(xn-x))
does xn2 really mean xn^2 or does it mean something else?
采纳的回答
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!