Смекни!
smekni.com

Метод наискорейшего спуска (стр. 3 из 3)

gy:=-2*sin(x0)*sin(y0*y0)*y0;

end;

x0:=x0+hx*gx;

y0:=y0+hy*gy;

if(ComboBox1.ItemIndex=0) then

f1:=(exp(x0)+exp(y0))/(exp(x0*x0)+exp(y0*y0));

if(ComboBox1.ItemIndex=1) then

f1:=sin(x0)*sin(x0)/(sin(y0)*sin(y0)+1);

if(ComboBox1.ItemIndex=2) then

f1:=sin(x0)*cos(y0*y0);

StringGrid1.Cells[i,0]:=IntToStr(i+1);

StringGrid1.Cells[i,1]:=FloatToStr(x0);

StringGrid1.Cells[i,2]:=FloatToStr(y0);

StringGrid1.Cells[i,3]:=FloatToStr(f1);

i:=i+1;

StringGrid1.ColCount:=i;

if f1<f0 then begin

hx:=hx/2;

hy:=hy/2;

end;

end;

Label1.Caption:=FloatToStr(f1);

refresh;

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

{====================================================

Конец работы приложения}

wglMakeCurrent(0, 0);

wglDeleteContext(hrc);

ReleaseDC (Panel1.Handle, DC);

DeleteDC (DC);

end;

procedure TForm1.FormResize(Sender: TObject);

begin

glViewport(0, 0, Panel1.ClientWidth, Panel1.ClientHeight);

glLoadIdentity;

glFrustum (-1, 1, -1, 1, 3, 10); // задаем перспективу

// этот фрагмент нужен для придания трёхмерности

glTranslatef(0.0, 0.0, -5); // перенос объекта - ось Z

InvalidateRect(Panel1.Handle, nil, False);

end;

procedure TForm1.FormPaint(Sender: TObject);

var

i,j: real;

zz,xl,yl,zl,zlm,stg,h: glfloat;

fy,fx,clr,clb,obl : real;

it,wn : integer;

mm1,mm2,mm3,mm4 : array [0..5] of TM;

begin

glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // очистка буфера цвета

xl:=0;

yl:=0;

zl:=0;

zlm:=StrToFloat(StringGrid1.cells[0,3]);

stg:=StringGrid1.ColCount;

clr:=0;

clb:=1.0;

h:=StrToFloat(Edit3.text);

wn:=StrToInt(Edit5.text);

obl:= StrToFloat(Edit6.Text);

//ox oy oz

glscalef(masht,masht,masht);

glLineWidth(3);

glcolor3f(1.0,1.0,1.0);

glBegin(GL_Lines);

glVertex3f(-1,-1,1);

glVertex3f(1,-1,1);

glVertex3f(-1,-1,1);

glVertex3f(-1,1,1);

glVertex3f(-1,-1,1);

glVertex3f(-1,-1,-1);

glend;

glLineWidth(1);

glcolor3f(1.0,1.0,0.0);

glBegin(GL_Lines);

//x

glVertex3f(1.1-0.03,-1+0.1,1);

glVertex3f(1.1+0.03,-1-0.1,1);

glVertex3f(1.1+0.03,-1+0.1,1);

glVertex3f(1.1-0.03,-1-0.1,1);

//z

glVertex3f(-1-0.03,1.1,1);

glVertex3f(-1+0.03,1.1,1);

glVertex3f(-1-0.03,1.1,1);

glVertex3f(-1+0.03,1.2,1);

glVertex3f(-1-0.03,1.2,1);

glVertex3f(-1+0.03,1.2,1);

//y

glVertex3f(-1,-1,-1-0.1);

glVertex3f(-1,-1+0.1,-1-0.1);

glVertex3f(-1,-1+0.1,-1-0.1);

glVertex3f(-1-0.03,-1+0.2,-1-0.1);

glVertex3f(-1,-1+0.1,-1-0.1);

glVertex3f(-1+0.03,-1+0.2,-1-0.1);

glend;

// if(ComboBox1.ItemIndex=1) then begin

/f1:=sin(x0)*sin(x0)/(sin(y0)*sin(y0)+1);

/f1:=sin(x0)/(x0+y0*y0+1);

i:=-wn;

j:=-wn;

while j<=wn do begin

i:=-wn;

yl:=j;

//glBegin(GL_points);

glBegin(GL_Lines);

glcolor3f(clr,0.0,clb);

while i<=wn do begin

xl:=i; //f(x) ==

if(ComboBox1.ItemIndex=0) then

zl:=(exp(xl)+exp(yl))/(exp(xl*xl)+exp(yl*yl));

if(ComboBox1.ItemIndex=1) then

zl:=sin(xl)*sin(xl)/(sin(yl)*sin(yl)+1);

if(ComboBox1.ItemIndex=2) then

zl:=sin(xl)*cos(yl*yl);

if zl>=zlm then begin

zlm:=zl;

clr:=clr+0.01;

clb:=clb-0.01;

glcolor3f(clr,0.0,clb);

end;

glVertex3f(xl*obl,zl*obl,yl*obl); //+++

i:=i+0.1;

xl:=i;

if(ComboBox1.ItemIndex=0) then

zl:=(exp(xl)+exp(yl))/(exp(xl*xl)+exp(yl*yl));

if(ComboBox1.ItemIndex=1) then

zl:=sin(xl)*sin(xl)/(sin(yl)*sin(yl)+1);

if(ComboBox1.ItemIndex=2) then

zl:=sin(xl)*cos(yl*yl);

glVertex3f(xl*obl,zl*obl,yl*obl); //++++

end;

j:=j+h;

end;

glend;

//----------------------

i:=-wn;

j:=-wn;

clr:=0;

clb:=1.0;

zlm:=StrToFloat(StringGrid1.cells[0,3]);

while j<=wn do begin

i:=-wn;

xl:=j;

glBegin(GL_Lines);

glcolor3f(clr,0.0,clb);

while i<=wn do begin

yl:=i; //f(x) ||

if(ComboBox1.ItemIndex=0) then

zl:=(exp(xl)+exp(yl))/(exp(xl*xl)+exp(yl*yl));

if(ComboBox1.ItemIndex=1) then

zl:=sin(xl)*sin(xl)/(sin(yl)*sin(yl)+1);

if(ComboBox1.ItemIndex=2) then

zl:=sin(xl)*cos(yl*yl);

if zl>=zlm then begin

zlm:=zl;

clr:=clr+0.01;

clb:=clb-0.01;

glcolor3f(clr,0.0,clb);

end;

glVertex3f(xl*obl,zl*obl,yl*obl); //+++

i:=i+0.1;

yl:=i;

if(ComboBox1.ItemIndex=0) then

zl:=(exp(xl)+exp(yl))/(exp(xl*xl)+exp(yl*yl));

if(ComboBox1.ItemIndex=1) then

zl:=sin(xl)*sin(xl)/(sin(yl)*sin(yl)+1);

if(ComboBox1.ItemIndex=2) then

zl:=sin(xl)*cos(yl*yl);

glVertex3f(xl*obl,zl*obl,yl*obl); //+++

end;

j:=j+h;

end;

glend;

//------

it:=0;

glLineWidth(2);

glBegin(GL_Line_strip);

glcolor3f(0.0,1.0,0.0);

while it<StringGrid1.ColCount do begin

xl:=StrToFloat(StringGrid1.cells[it,1]); //+

yl:=StrToFloat(StringGrid1.cells[it,2]); //+

zl:=StrToFloat(StringGrid1.cells[it,3]); //+

glVertex3f(xl*obl,zl*obl,yl*obl);

it:=it+1;

end;

glend;

glLineWidth(1);

//tmax

glPointSize(6);

glBegin(GL_points);

glcolor3f(1.0,0.0,0.0);

glVertex3f(xl*obl,zl*obl,yl*obl);

glcolor3f(0.0,0.0,1.0);

glVertex3f(StrToFloat(StringGrid1.cells[0,1])*obl,StrToFloat(StringGrid1.cells[0,3])*obl,StrToFloat(StringGrid1.cells[0,2])*obl);

glend;

glscalef(1.0/masht,1.0/masht,1.0/masht);

glRotatef(verx,1,0,0);

glRotatef(rt,0,1,0);

SwapBuffers(DC);

Label2.Caption:=FloatToStr(zlm);

end;

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

if(Button=mbleft) then begin

radiobutton1.Checked:=true;

end;

end;

procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

radiobutton1.Checked:=false;

end;

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);

begin

rt:=0;

verx:=0;

if radiobutton1.Checked=true then begin

rt:=x- xmaus;

verx:=y-ymaus;

refresh;

end;

xmaus:=x;

ymaus:=y;

end;

procedure TForm1.Button2Click(Sender: TObject);

var

hx,hy,h,x0,y0,f0,gx,gy,g1x,g1y,e,f1: real;

i : integer;

begin

StringGrid1.ColCount:=1;

h :=0.001;

hx:=0.001;

hy:=0.001;

e:=0.002;

x0:=StrToInt(Edit1.Text);

y0:=StrToInt(Edit2.Text);

if(ComboBox1.ItemIndex=0) then begin

f0:=(exp(x0)+exp(y0))/(exp(x0*x0)+exp(y0*y0));

gx:=exp(x0)/(exp(x0*x0)+exp(y0*y0))-exp(x0*x0)*x0*(2*(exp(x0)+exp(y0))/((exp(x0*x0)+exp(y0*y0))*(exp(x0*x0)+exp(y0*y0))));

gy:=exp(y0)/(exp(y0*y0)+exp(x0*x0))-exp(y0*y0)*y0*(2*(exp(y0)+exp(x0))/((exp(y0*y0)+exp(x0*x0))*(exp(y0*y0)+exp(x0*x0))));

end;

if(ComboBox1.ItemIndex=1) then begin

f0:=sin(x0)*sin(x0)/(sin(y0)*sin(y0)+1);

gx:=2*sin(x0)*cos(x0)/(sin(y0)*sin(y0)+1);

gy:=-2*sin(x0)*sin(x0)*cos(y0)*sin(y0)/((sin(y0)*sin(y0)+1)*(sin(y0)*sin(y0)+1));

end;

if(ComboBox1.ItemIndex=2) then begin

f0:=sin(x0)*cos(y0*y0);

gx:= cos(x0)*cos(y0*y0);

gy:=-2*sin(x0)*sin(y0*y0)*y0;

end;

g1x:=gx;

g1y:=gy;

x0:=x0+hx*gx;

y0:=y0+hy*gy;

if(ComboBox1.ItemIndex=0) then

f1:=(exp(x0)+exp(y0))/(exp(x0*x0)+exp(y0*y0));

if(ComboBox1.ItemIndex=1) then

f1:=sin(x0)*sin(x0)/(sin(y0)*sin(y0)+1);

if(ComboBox1.ItemIndex=2) then

f1:=sin(x0)*cos(y0*y0);

StringGrid1.Cells[0,0]:='1';

StringGrid1.Cells[0,1]:=FloatToStr(x0);

StringGrid1.Cells[0,2]:=FloatToStr(y0);

StringGrid1.Cells[0,3]:=FloatToStr(f1);

if f1<f0 then begin

hx:=h;

hy:=h;

end

else begin

hx:=hx*2;

hy:=hy*2;

end;

i:=1;

while not((abs(g1x)<e) and (abs(g1y)<e))do

begin

f0:=f1;

x0:=x0+hx*gx;

y0:=y0+hy*gy;

if(ComboBox1.ItemIndex=0) then begin

g1x:=exp(x0)/(exp(x0*x0)+exp(y0*y0))-exp(x0*x0)*x0*(2*(exp(x0)+exp(y0))/((exp(x0*x0)+exp(y0*y0))*(exp(x0*x0)+exp(y0*y0))));

g1y:=exp(y0)/(exp(y0*y0)+exp(x0*x0))-exp(y0*y0)*y0*(2*(exp(y0)+exp(x0))/((exp(y0*y0)+exp(x0*x0))*(exp(y0*y0)+exp(x0*x0))));

f1:=(exp(x0)+exp(y0))/(exp(x0*x0)+exp(y0*y0));

end;

if(ComboBox1.ItemIndex=1) then begin

g1x:=2*sin(x0)*cos(x0)/(sin(y0)*sin(y0)+1);

g1y:=-2*sin(x0)*sin(x0)*cos(y0)*sin(y0)/((sin(y0)*sin(y0)+1)*(sin(y0)*sin(y0)+1));

f1:=sin(x0)*sin(x0)/(sin(y0)*sin(y0)+1);

end;

if(ComboBox1.ItemIndex=2) then begin

f1:=sin(x0)*cos(y0*y0);

g1x:= cos(x0)*cos(y0*y0);

g1y:=-2*sin(x0)*sin(y0*y0)*y0;

end;

StringGrid1.Cells[i,0]:=IntToStr(i+1);

StringGrid1.Cells[i,1]:=FloatToStr(x0);

StringGrid1.Cells[i,2]:=FloatToStr(y0);

StringGrid1.Cells[i,3]:=FloatToStr(f1);

i:=i+1;

StringGrid1.ColCount:=i;

if f1<f0 then begin

if(ComboBox1.ItemIndex=0) then begin

gx:=exp(x0)/(exp(x0*x0)+exp(y0*y0))-exp(x0*x0)*x0*(2*(exp(x0)+exp(y0))/((exp(x0*x0)+exp(y0*y0))*(exp(x0*x0)+exp(y0*y0))));

gy:=exp(y0)/(exp(y0*y0)+exp(x0*x0))-exp(y0*y0)*y0*(2*(exp(y0)+exp(x0))/((exp(y0*y0)+exp(x0*x0))*(exp(y0*y0)+exp(x0*x0))));

end;

if(ComboBox1.ItemIndex=1) then begin

gx:=2*sin(x0)*cos(x0)/(sin(y0)*sin(y0)+1);

gy:=-2*sin(x0)*sin(x0)*cos(y0)*sin(y0)/((sin(y0)*sin(y0)+1)*(sin(y0)*sin(y0)+1));

end;

if(ComboBox1.ItemIndex=2) then begin

gx:= cos(x0)*cos(y0*y0);

gy:=-2*sin(x0)*sin(y0*y0)*y0;

end;

hx:=h;

hy:=h;

end

else begin

hx:=hx*2;

hy:=hy*2;

end;

end;

Label1.Caption:=FloatToStr(f1);

refresh;

end;

procedure TForm1.Edit3Change(Sender: TObject);

var

c : real;

begin

try

c:=StrToFloat(Edit3.text);

except

Edit3.text:='0,05';

end;

end;

procedure TForm1.Edit1Change(Sender: TObject);

var

c : integer;

begin

try

c:=StrToInt(Edit1.text);

except

Edit1.text:='-1';

end;

end;

procedure TForm1.Edit2Change(Sender: TObject);

var

c : integer;

begin

try

c:=StrToInt(Edit2.text);

except

Edit2.text:='-1';

end;

end;

procedure TForm1.Edit4Change(Sender: TObject);

begin

masht:=StrToFloat(Edit4.text)/10;

refresh;

end;

procedure TForm1.Edit5Change(Sender: TObject);

var

c : integer;

begin

try

c:=StrToInt(Edit5.text);

except

Edit5.text:='1';

end;

end;

procedure TForm1.Edit6Change(Sender: TObject);

var

c : real;

begin

try

c:=StrToFloat(Edit6.text);

except

Edit6.text:='1,0';

end;

end;

end.

Приложение Б

Тест 1:

Найдём максимум функции f(x,y)=(exp(x)+exp(y))/(exp(x^2)+exp(y^2))

Простой градиентный метод

программный изделие технический средство

Метод наискорейшего спуска

Тест 2:

Найдём максимум функции f(x,y)=sin(x0)*sin(x0)/(sin(y0)*sin(y0)+1)

Простой градиентный метод

Метод наискорейшего спуска

Тест 3:

Найдём максимум функции f(x,y)=sin(x0)*cos(y0*y0)

Простой градиентный метод


Метод наискорейшего спуска

При изменении начальных точек, может меняться и положение максимума