Zurück zum Inhaltsverzeichnis

Kapitel 22

Interpolation mit Polynomen

> restart:

> datax:=[seq(i,i=1..10)];

datax := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> datay:=[seq(rand(10)(), i=1..10)];

datay := [2, 2, 7, 5, 0, 6, 3, 7, 1, 8]

> dataxy:=zip( (x,y)->[x,y], datax, datay):

> f:=interp( datax, datay, x);

f := 313/120960*x^9-85/672*x^8+17761/6720*x^7-22201...
f := 313/120960*x^9-85/672*x^8+17761/6720*x^7-22201...

> p1:=plot(f, x=0.9..10.1):

> p2:=plot(dataxy,style=POINT,symbol=BOX,color=blue):

> with(plots):

> display( [p1,p2] );

[Maple Plot]

Stückweise Interpolation mit Splines

> #datax:=[seq(i,i=1..10)];

datax := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> #datay:=[seq( rand(10)(), i=1..10)];

datay := [5, 3, 7, 0, 4, 5, 6, 1, 7, 9]

> #dataxy:=zip( (x,y)->[x,y], datax, datay):

>

> f:=unapply(spline( datax, datay, x, 3),x):f(x);

PIECEWISE([2+46264/13515*x-23132/4505*x^2+23132/135...

> f(2.5);

4.55052719

> p1:=plot(f(x), x=1..10):

> p2:=plot(dataxy, style=point,symbol=box,color=blue):

> display( [p1,p2] );

[Maple Plot]

> fd:=unapply(diff(f(x), x),x);

fd := proc (x) options operator, arrow; piecewise(x...
fd := proc (x) options operator, arrow; piecewise(x...
fd := proc (x) options operator, arrow; piecewise(x...
fd := proc (x) options operator, arrow; piecewise(x...

> fd2:=unapply(diff(f(x),x,x),x):

> plot( {fd(x), fd2(x)}, x=1..10);

[Maple Plot]

Anpassung mit leastsquare

> restart:

> with(stats): with(fit): with(statplots): with(plots):

Warning, the name changecoords has been redefined

> datax:=[seq( rand(100)(), i=1..50)]:

> datay:=map( x->evalf(sin(x/100*Pi)+rand(30)()/100,4), datax):

> p1:=scatterplot(datax, datay):

> leastsquare[ [x,y], y=a0 + a1*x +a2*x^2, {a0,a1,a2}]( [datax,datay] );

y = .1866167892+.3911189592e-1*x-.3984157016e-3*x^2...

> f:=rhs(%);

f := .1866167892+.3911189592e-1*x-.3984157016e-3*x^...

> p2:=plot( f, x=0..100):

> display([p1,p2]);

[Maple Plot]

> fy:=a0 + a1*sin(x/10.)+ a2*sin(x/20.) + a3*sin(x/40.);

fy := a0+a1*sin(.1000000000*x)+a2*sin(.5000000000e-...

>

> f:=rhs(leastsquare[ [x,y], y=fy, {a0,a1,a2,a3}]( [datax,datay] ));

f := .3215259831e-1+.7882161750e-1*sin(.1000000000*...
f := .3215259831e-1+.7882161750e-1*sin(.1000000000*...

> p2:=plot( f, x=0..100):

> display( [p1,p2] );

[Maple Plot]

> fy:=a0 + a1*exp(x):

> f:=rhs(leastsquare[ [x,y], y=fy, {a0,a1}]( [datax,datay] )):

> p2:=plot( f, x=0..100):

> display( [p1,p2] );

[Maple Plot]

> datax1:=[1,2,3]: datax2:=[2,3,4]: datay:=[3,7,8]:

> f:=rhs(leastsquare[ [x1,x2,y], y=a1*x1+a2*x2, {a1,a2} ]([datax1, datax2, datay]));

f := 3/2*x1+x2

> seq( subs(x1=datax1[i], x2=datax2[i],f-datay[i]), i=1..3);

1/2, -1, 1/2

Zurück zum Inhaltsverzeichnis