几个 数值分析 的算法例子 VBScript的 '几个参考点的数据已经给出,输入参数只有 Xfunction view(result,inputx) '输出结果,同时如果<1 and >0,就在前面补0 if result<1 and result>0 then result=0&result Response.Write "计算结果:"&"<br>" Response.Write "F("&inputx&")="& result end function '********分段线性Lagrange插值********** function Lagrange1(inputx) dim k,i dim x,y x=array("0.1","0.2","0.3","0.4") y=array("0.0998","0.1987","0.2955","0.3894")if inputx<x(0) then k=0 if inputx>x(3) then k=2for i=0 to 2 if inputx>=x(i) and inputx<=x(i+1) then k=i result=((inputx-x(k+1))/(x(k)-x(k+1)))*y(k) + ((inputx-x(k))/(x(k+1)-x(k)))*y(k+1) next result= view(result,inputx) end function '********分段三点二次Lagrange插值********** function Lagrange2(inputx) dim i,j,k,t dim x,y result=0 x=array("0.1","0.2","0.3","0.4") y=array("0.0998","0.1987","0.2955","0.3894")if inputx<=x(1) then k=0 if inputx>=x(2) then k=1 if inputx>x(1) and inputx<x(2) and abs(inputx-x(1))<=abs(inputx-x(2)) then k=0 else k=1 for j=k to k+2 t=1 for i= k to k+2 if i<>j then t= t * (inputx-x(i))/(x(j)-x(i)) end if next result = result + t*y(j) next result= view(result,inputx) end function '**********一元n点拉格朗日插值*********** function Lagrange3(inputx) dim i,j dim x,y result=0 x=array("0","0.1","0.195","0.4","0.401","0.5") y=array("0.39894","0.39695","0.39142","0.38138","0.36812","0.35206")for j=0 to 5 t=1 for i=0 to 5 if i<>j then t= t * (inputx-x(i))/(x(j)-x(i)) end if next result = result + t * y(j) next result= view(result,inputx) end function '***********牛顿(Newton)插值*********** function Newton(inputx) dim x,y dim i,j result=0.39894 t=1 x=array("0","0.1","0.195","0.3","0.401","0.5") y=array("0.39894","0.39695","0.39142","0.38138","0.36812","0.35206")for j=1 to 5 t=t*(inputx - x(j-1)) for i=0 to 5-j y(i)=(y(i+1) - y(i)) / (x(i+j) - x(i)) next result = result + t * y(0) next result= view(result,inputx) end function '***********埃特金(Aitken)插值*********** function Aitken(inputx) dim i,j dim x,y x=array("0.5","0.65","0.8","1.0") y=array("0.4794","0.6052","0.7174","0.8415") for j=1 to 3 for i=j to 3 y(i)=y(j-1) + (y(i)-y(j-1)) / (x(i)-x(j-1))* (inputx-x(j-1)) next nextresult= view(y(3),inputx) end function '***********分段两点三次埃(厄)尔米特(Hermit)插值*********** function Hermit(inputx) dim i,k dim x,y,yy x=array("0.1","0.3","0.5") y=array("0.099833","0.295520","0.479426") yy=array("0.995004","0.995336","0.877583")if inputx<=x(0) then k=0 if inputx>=x(2) then k=1 for i=0 to 1 if x(i)<=inputx and inputx<=x(i+1) then k=i nexthx1=(1+2*(inputx-x(k)) / (x(k+1)-x(k))) * mul((x(k+1)-inputx) / (x(k+1)-x(k))) hx2=(1+2*(x(k+1)-inputx) / (x(k+1)-x(k))) * mul((inputx-x(k)) / (x(k+1)-x(k))) hx3=(inputx - x(k))* mul((x(k+1)-inputx)/(x(k+1)-x(k))) hx4=(inputx - x(k+1))* mul((inputx-x(k))/(x(k+1)-x(k)))result=y(k)*hx1 + y(k+1)*hx2 + yy(k)*hx3 + yy(k+1)*hx4 result= view(result,inputx) end functionfunction mul(str) mul=str*str end function
那是不是这样: 用埃特金(Aitken)插值法,算出N个采样点 Y 值, 然后再用直线将所有的点连在一起。这样的话,那么采样点是需要非常非常多的,才有可能连成平滑的曲线。
public Lagrange(Point2D.Double[] pt) { this.pt = pt; }
public void compute(int inputx) { int k = 0; if (inputx < pt[0].x || inputx > pt[pt.length - 1].x) { throw new IllegalArgumentException("invalid inputx, must in range of " + pt[0].x + " and " + pt[pt.length - 1].x); } double result = 0; for (int i = 0; i < pt.length; i++) { if (inputx >= pt[i].x && inputx <= pt[i + 1].x) { k = i; result = (inputx - pt[k+1].x) / (pt[k].x - pt[k+1].x) * pt[k].y + (inputx - pt[k].x) / (pt[k+1].x - pt[k].x) * pt[k+1].y; System.out.println(inputx + ":" + result); } }
} public static void main(String[] args) { Lagrange algorithm = new Lagrange(new Point2D.Double[]{ new Point2D.Double(50, 98), new Point2D.Double(100, 198), new Point2D.Double(150, 298), new Point2D.Double(200, 198)}); for (int i = 50; i < 200; i+= 5) { algorithm.compute(i); }
看来还是去google搜索一下有没有算法实现
用 牛顿迭代或者埃特金插值 可以根据给出的几个点求出函数
然后再根据这个函数就可以画出曲线图了?
'几个参考点的数据已经给出,输入参数只有 Xfunction view(result,inputx) '输出结果,同时如果<1 and >0,就在前面补0
if result<1 and result>0 then result=0&result
Response.Write "计算结果:"&"<br>"
Response.Write "F("&inputx&")="& result
end function
'********分段线性Lagrange插值**********
function Lagrange1(inputx)
dim k,i
dim x,y
x=array("0.1","0.2","0.3","0.4")
y=array("0.0998","0.1987","0.2955","0.3894")if inputx<x(0) then k=0
if inputx>x(3) then k=2for i=0 to 2
if inputx>=x(i) and inputx<=x(i+1) then k=i
result=((inputx-x(k+1))/(x(k)-x(k+1)))*y(k) + ((inputx-x(k))/(x(k+1)-x(k)))*y(k+1)
next
result= view(result,inputx)
end function
'********分段三点二次Lagrange插值**********
function Lagrange2(inputx)
dim i,j,k,t
dim x,y
result=0
x=array("0.1","0.2","0.3","0.4")
y=array("0.0998","0.1987","0.2955","0.3894")if inputx<=x(1) then k=0
if inputx>=x(2) then k=1
if inputx>x(1) and inputx<x(2) and abs(inputx-x(1))<=abs(inputx-x(2)) then k=0 else k=1
for j=k to k+2
t=1
for i= k to k+2
if i<>j then
t= t * (inputx-x(i))/(x(j)-x(i))
end if
next
result = result + t*y(j)
next
result= view(result,inputx)
end function
'**********一元n点拉格朗日插值***********
function Lagrange3(inputx)
dim i,j
dim x,y
result=0
x=array("0","0.1","0.195","0.4","0.401","0.5")
y=array("0.39894","0.39695","0.39142","0.38138","0.36812","0.35206")for j=0 to 5
t=1
for i=0 to 5
if i<>j then
t= t * (inputx-x(i))/(x(j)-x(i))
end if
next
result = result + t * y(j)
next
result= view(result,inputx)
end function '***********牛顿(Newton)插值***********
function Newton(inputx)
dim x,y
dim i,j
result=0.39894
t=1
x=array("0","0.1","0.195","0.3","0.401","0.5")
y=array("0.39894","0.39695","0.39142","0.38138","0.36812","0.35206")for j=1 to 5
t=t*(inputx - x(j-1))
for i=0 to 5-j
y(i)=(y(i+1) - y(i)) / (x(i+j) - x(i))
next
result = result + t * y(0)
next
result= view(result,inputx)
end function
'***********埃特金(Aitken)插值***********
function Aitken(inputx)
dim i,j
dim x,y
x=array("0.5","0.65","0.8","1.0")
y=array("0.4794","0.6052","0.7174","0.8415")
for j=1 to 3
for i=j to 3
y(i)=y(j-1) + (y(i)-y(j-1)) / (x(i)-x(j-1))* (inputx-x(j-1))
next
nextresult= view(y(3),inputx)
end function
'***********分段两点三次埃(厄)尔米特(Hermit)插值***********
function Hermit(inputx)
dim i,k
dim x,y,yy
x=array("0.1","0.3","0.5")
y=array("0.099833","0.295520","0.479426")
yy=array("0.995004","0.995336","0.877583")if inputx<=x(0) then k=0
if inputx>=x(2) then k=1
for i=0 to 1
if x(i)<=inputx and inputx<=x(i+1) then k=i
nexthx1=(1+2*(inputx-x(k)) / (x(k+1)-x(k))) * mul((x(k+1)-inputx) / (x(k+1)-x(k)))
hx2=(1+2*(x(k+1)-inputx) / (x(k+1)-x(k))) * mul((inputx-x(k)) / (x(k+1)-x(k)))
hx3=(inputx - x(k))* mul((x(k+1)-inputx)/(x(k+1)-x(k)))
hx4=(inputx - x(k+1))* mul((inputx-x(k))/(x(k+1)-x(k)))result=y(k)*hx1 + y(k+1)*hx2 + yy(k)*hx3 + yy(k+1)*hx4
result= view(result,inputx)
end functionfunction mul(str)
mul=str*str
end function
用埃特金(Aitken)插值法,算出N个采样点 Y 值,
然后再用直线将所有的点连在一起。这样的话,那么采样点是需要非常非常多的,才有可能连成平滑的曲线。
result = (inputx - pt[k+1].x) / (pt[k].x - pt[k+1].x) * pt[k].y + (inputx - pt[k].x) / (pt[k+1].x - pt[k].x) * pt[k+1].y;
我的采样是5像素一个点,你看看吧
搂住最好不要再问我这个问题了,我说不清楚的,你真不如找本基础的书看看,其实我毕业后已经很久没看过数值分析的东西了,有些东西记不住了,说不定还是错的。import java.awt.Point;
import java.awt.geom.Point2D;//********分段线性Lagrange插值**********
public class Lagrange {
private Point2D.Double pt[] = null;
public Lagrange(Point2D.Double[] pt) {
this.pt = pt;
}
public void compute(int inputx) {
int k = 0;
if (inputx < pt[0].x || inputx > pt[pt.length - 1].x) {
throw new IllegalArgumentException("invalid inputx, must in range of " +
pt[0].x + " and " + pt[pt.length - 1].x);
}
double result = 0;
for (int i = 0; i < pt.length; i++) {
if (inputx >= pt[i].x && inputx <= pt[i + 1].x) {
k = i;
result = (inputx - pt[k+1].x) / (pt[k].x - pt[k+1].x) * pt[k].y +
(inputx - pt[k].x) / (pt[k+1].x - pt[k].x) * pt[k+1].y;
System.out.println(inputx + ":" + result);
}
}
}
public static void main(String[] args) {
Lagrange algorithm = new Lagrange(new Point2D.Double[]{
new Point2D.Double(50, 98),
new Point2D.Double(100, 198),
new Point2D.Double(150, 298),
new Point2D.Double(200, 198)});
for (int i = 50; i < 200; i+= 5) {
algorithm.compute(i);
}
}
}