دستور آمادای command در نرمافزار Mathematica برای درونیابیِ نِویل Neville ندیدم. چند دستور که اسمشان با Neville شروع شوند هست که مربوط به این بحث نیستند. ولی چون الگوریتمِ نِویل بسیار ساده است و از چیزی غیر از جمع و تفریق و چند حلقهٔ for بیشتر استفاده نمیکند به راحتی خودتان نه تنها در Mathematica بلکه در سایر نرمافزارهای پیشرفتهٔ ریاضی و یا حتی زبانهای برنامهنویسی بدون نیاز به استفاده از هیچ بسته، مدول یا کتابخانهای میتوانید آن را بنویسید و پیاده کنید. اگر الگوریتم نویل را نمیشناسید میتوانید به کتاب Introduction to Numerical Analysis نوشتهٔ J. Stoer و R. Bulrisch که بوسیلهٔ R. Bartels، W. Gautschi و C. Witzgall به انگلیسی ترجمه و انتشارات Springer آن را چاپ کردهاست، ویرایش دوم، فصل ۲ بخش ۲.۱.۲ مراجعه کنید. حتی شِمای الگوریتم را هم در صفحهٔ ۴۲ آوردهاند و تنها کاری که شما باید انجام دهید نوشتنش به زبانِ نرمافزار یا برنامهنویسیتان است. در زیر من این الگوریتم را به ترتیب با زبانهای برنامهنویسیِ ++C و Python و Julia و سپس با نرمافزارهای Maple و Mathematica و Matlab آن را به صورت یک تابع یا پروسه نوشته و بر روی مثال خود کتاب که در صفحهٔ ۴۱ آمدهاست آن را فراخوانی و استفاده کردهام که پاسخ آن $\frac{10}{3}=3.\bar{3}$ است.
++C
# include <iostream>
using namespace std;
double Neville(int n,double xlist[], double flist[], double x)
{
double t[n];
for (int i=0;i<n;i++)
{
t[i]=0;
}
for (int i=0;i<n;i++)
{
t[i]=flist[i];
for (int j=i-1;j>-1;j--)
{
t[j]=t[j+1]+(t[j+1]-t[j])*(x-xlist[i])/(xlist[i]-xlist[j]);
}
}
return t[0];
}
int main()
{
int n=3;
double xlist[3]={0,1,3};
double flist[3]={1,3,2};
double x=2;
cout << Neville(n,xlist,flist,x) << "\n";
return 0;
}
تصویر آن در محیط Dev-Cpp در زیر قرار دادهشدهاست.
تصویر پس از compile & run کردن برنامه نیز در زیر آورده شدهاست.
Python
def Neville(xlist, flist, x):
n = len(xlist)
if len(flist) != n:
return('size of x_list and f_list are not the same, please recheck your input data.')
t = [0 for i in range(n)]
for i in range(n):
t[i] = flist[i]
for j in range(i-1, -1, -1):
t[j] = t[j+1]+(t[j+1]-t[j]) * (x-xlist[i])/(xlist[i]-xlist[j])
return(t[0])
اکنون فراخوانی آن بر روی نمونه
xlist = [0, 1, 3]
flist = [1, 3, 2]
x = 2
print(Neville(xlist, flist, x))
تصویر برنامه و خروجیاش در محیط VS code در زیر آورده شدهاند.
Julia
function Neville(xlist, flist, x)
n = length(xlist)
if length(flist) != n
return("size of x_list and f_list are not the same, please recheck your input data.")
end
t = [0.0 for i = 1:n]
for i = 1:n
t[i] = flist[i]
for j = (i - 1):-1:1
t[j] = t[j + 1] + (t[j + 1] - t[j]) * (x - xlist[i]) / (xlist[i] - xlist[j])
end
end
return(t[1])
end
فراخوانی آن بر روی نمونه
xlist = [0, 1, 3]
flist = [1, 3, 2]
x = 2
print(Neville(xlist, flist, x))
تصویر برنامه در محیط VS code در زیر آورده شدهاست.
تصویر خروجی اجرای برنامه نیز در زیر آورده شدهاست.
Maple
Neville := proc(xlist, flist, x)
local n, t, i, j;
n := nops(xlist);
if nops(flist) != n then
return "size of x_list and f_list are not the same, please recheck your input data";
end if;
t := [seq(0, i = 1 .. n)];
for i to n do
t[i] := flist[i];
for j from i - 1 by -1 to 1 do
t[j] := t[j + 1] + (t[j + 1] - t[j])*(x - xlist[i])/(xlist[i] - xlist[j]);
end do;
end do;
return t[1];
end proc;
فراخوانی بر روی نمونه
xlist := [0, 1, 3]:
flist := [1, 3, 2]:
x := 2:
Neville(xlist,flist,x);
تصویر در محیط Maple به همراه خروجیاش در زیر آورده شدهاست.
Mathematica
neville[xlist_, flist_, x_] := (
n = Length[xlist];
If[Length[flist] != n,
Return["Size of x_list and f_list are not the same, please recheck \
your input data"]
];
t = ConstantArray[0, n];
For[i = 1, i <= n, i++,
t[[i]] = flist[[i]];
For[j = i - 1, j >= 1, j--,
t[[j]] =
t[[j + 1]] + (t[[j + 1]] -
t[[j]])*(x - xlist[[i]])/(xlist[[i]] - xlist[[j]]);
]
];
Return[t[[1]]]
)
اکنون فراخوانی بر روی نمونه
xlist = {0, 1, 3}
flist = {1, 3, 2}
x = 2
neville[xlist, flist, x]
تصویر این برنامه در محیط خود Mathematica به همراه خروجی بکارگیری آن در نمونه در زیر آوردهشدهاست.
Matlab
function y=Neville(xlist,flist,x)
n=length(xlist);
if length(flist)~=n
y="size of x_list and f_list are not the same, please recheck your input data";
end
t=zeros(1,n);
for i=1:n
t(i)=flist(i);
for j=i-1:-1:1
t(j)=t(j+1)+(t(j+1)-t(j))*(x-xlist(i))/(xlist(i)-xlist(j));
end
end
y=t(1);
end
و توجه کنید که در Matlab باید فراخوانیِ تابع را پیش از مکانی که تابع تعریف شدهاست انجام دهید، پس برای فراخوانی بر روی نمونه، قسمت زیر باید بالای تابع بالا نوشتهشدهباشد.
xlist=[0,1,3];
flist=[1,3,2];
x=2;
Neville(xlist,flist,x)
تصویر برنامه و خروجیاش در محیط Matlab در زیر قرار دادهشدهاست.