數(shù)值計算牛頓迭代法的matlab源程序
說明如下:
%fun----input,the part as the form of f(x) in the equation f(x)=0
% ini----input,sets the starting point to ini
% err----input,sets admissible error
% sol----output,returns the root of equation
編寫具有如下原型的函數(shù):int f(unsigned long x, int n, int& Lxn)
它負責將整數(shù)x的第n位(從左邊數(shù)第n位,n>0)的數(shù)值放到引用Lxn之中(將作為結(jié)果返回到主調(diào)函數(shù)的對應實參變量中),并將倒數(shù)第n位(從右邊數(shù)第n位,n>0)的數(shù)值作為函數(shù)結(jié)果返回去。并編制主函數(shù)對它進行調(diào)用以驗證其正確性。
例如,當x=123456789,n=7時,執(zhí)行語句“Rxn=f(x, n, Lxn) ”將使返回的Lxn為7,并使Rxn變?yōu)?;而執(zhí)行語句“Rxn=f(12345, 6, Lxn) ”將使Lxn與Rxn都變?yōu)闉?(超出數(shù)的“長度”即總位數(shù)時返回0)。