Harj. 9 AV

27.3.02 HA

Alustukset

Kts. myös L/LSQ.mws ja L/newtopt.mws

> restart:

Warning, the name changecoords has been redefined

> with(LinearAlgebra): with(plots): alias(Tr=Transpose,Lsolve=LinearSolve):

> with(linalg):with(plottools):

Warning, the previous binding of the name GramSchmidt has been removed and it now has an assigned value

Warning, the protected names norm and trace have been redefined and unprotected

Warning, the name arrow has been redefined

> setoptions3d(axes=boxed,orientation=[-30,50]):

> V2L:=vek->convert(vek,list):

> read("/home/apiola/opetus/peruskurssi/v2-3/maple/v202.mpl");

Teht. 1

> xd:=[2,3,4,5]: yd:=[5,9,15,2]:

> CT:=Matrix([[1,1,1,1],xd]); C:=Tr(CT);

CT := _rtable[136279384]

C := _rtable[136126856]

> A:=CT.C;

>

A := _rtable[135321360]

> sum(xd[i]^2,i=1..4);

54

> b:=CT.Vector(yd);

b := _rtable[135505184]

> sum(yd[i],i=1..4);

31

> sum(xd[i]*yd[i],i=1..4);

107

> a:=Lsolve(A,b);

a := _rtable[134816732]

> datakuva:=plot([seq([xd[i],yd[i]],i=1..4)],style=point,symbol=circle,symbolsize=15):

> suora:=a[1]+a[2]*x;

suora := 44/5-3/10*x

> display(datakuva,plot(suora,x=1..6,color=blue));

[Maple Plot]

>

2.

> f:=(x1,x2)->2*(x1^2+x2^2)+ x1*x2 -5*(x1+x2);

f := proc (x1, x2) options operator, arrow; 2*x1^2+...

> gr:=Vector(grad(f(x1,x2),[x1,x2]));

gr := _rtable[135937596]

> H:=Matrix(hessian(f(x1,x2),[x1,x2]));

H := _rtable[134996812]

> p0:=<1,-2>; p:=p0:

p0 := _rtable[137028208]

> grp:=subs(x1=p[1],x2=p[2],gr);

grp := _rtable[138424696]

> h:=LinearSolve(H,-grp);

h := _rtable[134864784]

> p0+h;

_rtable[136192308]

> solve({gr[1]=0,gr[2]=0},{x1,x2});

{x1 = 1, x2 = 1}

> p0:=<10,20>; p:=p0:grp:=subs(x1=p[1],x2=p[2],gr);

p0 := _rtable[136219460]

grp := _rtable[135490356]

> h:=LinearSolve(H,-grp);

h := _rtable[136491544]

> p0+h;

_rtable[134842484]

Toden totta, tehtäväpaperissa oleva ennustus näyttää toteutuvan!

No miksi?

Newtonissa otetaan funktion muutokselle Taylorin 2. asteen polynomiapproksimaatio, jonka tarkka minimi haetaan (ratkaisemalla yksikäsitteinen KRP), mimi siksi, että oletetaan pos. def.

Nyt, kun kohdefunktio on suoraan 2. asteen polynomi, se yhtyy tarkalleen omaan Taylorin 2. asteen polynomiinsa.

Newtonaskel laskee sen minimin tarkasti.

3.

> restart:

Warning, the name changecoords has been redefined

> with(LinearAlgebra): with(linalg):with(plots): alias(Tr=Transpose,Lsolve=LinearSolve):

Warning, the previous binding of the name GramSchmidt has been removed and it now has an assigned value

Warning, the protected names norm and trace have been redefined and unprotected

> V2L:=vek->convert(vek,list):

> read("/home/apiola/opetus/peruskurssi/v2-3/maple/v202.mpl");

> f1:=x^2-x*y+2*y^2-10; f2:=x^3*y^2-2;

f1 := x^2-x*y+2*y^2-10

f2 := x^3*y^2-2

> fv:=x-><x[1]^2-x[1]*x[2]+2*x[2]^2-10,x[1]^3*x[2]^2-2>;

fv := proc (x) options operator, arrow; <x[1]^2-x[1...

> F:=(x,y)->[x^2-x*y+2*y^2-10,x^3*y^2-2];

F := proc (x, y) options operator, arrow; [x^2-y*x+...

> implicitplot({f1=0,f2=0},x=0..4,y=-3..3);

[Maple Plot]

> J:=Matrix(jacobian([f1,f2],[x,y]));

J := _rtable[136051272]

> p[0]:=<1,-2>; pp:=p[0]:

p[0] := _rtable[136180476]

> Jp:=subs(x=pp[1],y=pp[2],J);fp:=fv(pp);

Jp := _rtable[134611188]

fp := _rtable[136180516]

> h:=evalf(LinearSolve(Jp,-fp));

h := _rtable[136398668]

> p[1]:=p[0]+h;

p[1] := _rtable[134764144]

> pp:=p[1]:Jp:=subs(x=pp[1],y=pp[2],J);fp:=fv(pp);h:=evalf(LinearSolve(Jp,-fp));p[2]:=p[1]+h;

Jp := _rtable[135394788]

fp := _rtable[136561732]

h := _rtable[136562172]

p[2] := _rtable[135571044]

> for j to 5 do
pp:=p[j-1]:Jp:=subs(x=pp[1],y=pp[2],J);fp:=fv(pp);h:=-evalf(LinearSolve(Jp,fp));p[j]:=p[j-1]+h; od:

> p[5];

_rtable[135832076]

> Matrix(map(V2L,[seq(p[i],i=0..5)]));

>

_rtable[136118056]

> p[0]:=<2,-1>;

p[0] := _rtable[136137476]

> n:=20:for j to n do
pp:=p[j-1]:Jp:=subs(x=pp[1],y=pp[2],J);fp:=fv(pp);h:=-evalf(LinearSolve(Jp,fp));p[j]:=p[j-1]+h; od:

> #op(Matrix(map(V2L,[seq(p[i],i=0..n)]))):

> p[n];

_rtable[135574160]

Eipä ota supetakseen.

> p[0]:=<3,0>;

p[0] := _rtable[135560064]

> n:=1:for j to n do
pp:=p[j-1]:Jp:=subs(x=pp[1],y=pp[2],J);fp:=fv(pp);h:=-evalf(LinearSolve(Jp,fp));p[j]:=p[j-1]+h; od:

Error, (in LinearAlgebra:-LA_Main:-LinearSolve) inconsistent system

> fp;Jp;

_rtable[135862556]

_rtable[135542972]

Jakobiaani on singulaarinen, Newton jysähtää.

Tässä olen koonnut proc:ksi 2-ulotteisen Newtonin, sitä on mukava käytellä:

> print(Newtonsys2);

proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...
proc (Fun, x0, niter) local F, xc, X, i, Jc, Fc, h,...

> Newtonsys2(F,<1,1>,6);

[[1., 1.], [-.857142857142856984, 4.285714285714285...
[[1., 1.], [-.857142857142856984, 4.285714285714285...
[[1., 1.], [-.857142857142856984, 4.285714285714285...
[[1., 1.], [-.857142857142856984, 4.285714285714285...

> Newtonsys2(F,<3,1>,6);

[[3., 1.], [3.54732510288065850, .26337448559670784...
[[3., 1.], [3.54732510288065850, .26337448559670784...
[[3., 1.], [3.54732510288065850, .26337448559670784...
[[3., 1.], [3.54732510288065850, .26337448559670784...

> Newtonsys2(F,<3,-1>,5);

[[3., -1.], [2.78306878306878325, -.645502645502645...
[[3., -1.], [2.78306878306878325, -.645502645502645...
[[3., -1.], [2.78306878306878325, -.645502645502645...

>

4.

> restart:

Warning, the name changecoords has been redefined

> with(LinearAlgebra): with(plots): alias(Tr=Transpose,Lsolve=LinearSolve):

> with(linalg):with(plottools):

Warning, the previous binding of the name GramSchmidt has been removed and it now has an assigned value

Warning, the protected names norm and trace have been redefined and unprotected

Warning, the name arrow has been redefined

> f1:=d1*cos(alpha)+d2*cos(alpha+beta)-x0;f2:=d1*sin(alpha)+d2*sin(alpha+beta)-y0;

f1 := d1*cos(alpha)+d2*cos(alpha+beta)-x0

f2 := d1*sin(alpha)+d2*sin(alpha+beta)-y0

> F:=unapply([f1,f2],x,y);

F := proc (x, y) options operator, arrow; [d1*cos(a...

> J:=Matrix(jacobian([f1,f2],[alpha,beta]));

J := _rtable[135760776]

> JI:=MatrixInverse(J);

JI := _rtable[135887132]

> p0:=<alpha,beta>;

p0 := _rtable[135894632]

> p1:=p0-JI.Vector(F(alpha,beta));

p1 := _rtable[135896392]
p1 := _rtable[135896392]
p1 := _rtable[135896392]

> det(J);

-d2*cos(alpha+beta)*d1*sin(alpha)+d2*sin(alpha+beta...

> simplify(%);

-d2*d1*(cos(alpha+beta)*sin(alpha)-sin(alpha+beta)*...

> expand(%);

d2*d1*sin(alpha)^2*sin(beta)+d2*d1*cos(alpha)^2*sin...

> factor(%);

d2*d1*sin(beta)*(sin(alpha)^2+cos(alpha)^2)

> map(factor@expand,JI);

_rtable[136276636]

> subs(sin(alpha)^2+cos(alpha)^2=1,%);

_rtable[135901992]

> map(combine,%);

_rtable[136004536]

> JI:=%;

JI := _rtable[136004536]

> p1:=p0-JI.Vector(F(p0[1],p0[2]));

p1 := _rtable[135224604]
p1 := _rtable[135224604]
p1 := _rtable[135224604]

>

Aivan siedettävä iteraatiokaava rarkaistussa muodossakin tässä tapauksessa. (Ehkä parasta kuitenkin jättää matriisikertolaskut tehtäväksi numeerisesti.)