, . , . , . , , . , . .
X 1
40 30 20
40 30
X 2
. 3.1.
, .
function[]=Scanirovanie2D_050809();
function z=fz_xy2(x,y)
nx=length(x); %
ny=length(y); %
% ,
%
for i=1:nx
for j=1:ny
z(i,j)=-sqrt(256-x(i)*x(i)-y(j)*y(j));
end
end
end
% 2-
function[x,y,z,masx,masy,masz,xleft,xright,yleft,yright,hx]=scan2();
disp(' ');
xleft=input(' X !');
xright=input(' X !');
hx=input(' X');
yleft=input(' Y !');
yright=input(' Y !');
hy=input(' Y');
%
masx=xleft:hx:xright;
masy=yleft:hy:yright;
masz=fz_xy2(masx,masy);
%
min=masz(1,1); %
numx=1;
numy=1;
nx=length(masx);
ny=length(masy);
for i=1:nx
for j=1:ny
if masz(i,j)<min
min=masz(i,j);
numx=i;
numy=j;
end
end
end
% ()
x=masx(numx);
y=masy(numy);
z=min;
end
%
[x,y,z,masx,masy,masz,xleft,xright,yleft,yright,hx]=scan2();
%
choiceTab=input(' ? =1 =0 ');
choiceGraf=input(' ? =1 =0');
%
if choiceTab==1
disp(' ');
for i=1:length(masx)
for j=1:length(masy)
a=(length(masy))*(i-1)+j;
string=print(= %4i\t x= %7.3f\t y= %7.3f\t= %7.3f,a,masx(i),masy(j),masz(i,j));
disp(string);
end
end
end
%
% num2str
disp(' ');
sxy=strcat(' : =',num2str(x),' =',num2str(y));
|
|
sz=strcat(' =',num2str(z));
disp(sxy) %
disp(sz)
%
if choiceGraf==1
n=floor(abs((xright-xleft)/hx));
hy=(yright-yleft)/n;
%
masx=xleft:hx:xright;
masy=yleft:hy:yright;
masz=fz_xy2(masx,masy);
mesh(masx,masy,masz); %
grid on;
title(z=-sqrt(256-x.^2-y.^2));
xlabel(X);
ylabel(Y);
zlabel(Z);
text(x,y,z,\leftarrow Minimum);
zeroMas=masx*0;
hold on;
%surf(zeroMas,zeroMas,masz); %
%surf(zeroMas,masy,zeroMas); %
%surf(masx,zeroMas,zeroMas); %
legend(z(i,j)=-sqrt(256-x(i)*x(i)-y(j)*y(j)),0); %
end
end
, :
X !-10
X !10
X.5
Y !-10
Y !10
Y.5
? =1 =0 1
? =1 =0 1
= 1 x=-10.000 y=-10.000 = -7.483
= 2 x=-10.000 y= -9.500 = -8.109
= 3 x=-10.000 y= -9.000 = -8.660
= 4 x=-10.000 y= -8.500 = -9.152
= 5 x=-10.000 y= -8.000 = -9.592
= 6 x=-10.000 y= -7.500 = -9.987
=1677 x= 10.000 y= 8.000 = -9.592
=1678 x= 10.000 y= 8.500 = -9.152
=1679 x= 10.000 y= 9.000 = -8.660
=1680 x= 10.000 y= 9.500 = -8.109
=1681 x= 10.000 y= 10.000 = -7.483
: =0 =0
=-16
. 3.2.
3.2.
(. 3.3). , () . . , , . . () , , .
. 3.3.
, .
Function[]=GaussZeidel2D_170809();
function z=fz_xy2(x,y);
nx=length(x);
ny=length(y);
for i=1:nx
for j=1:ny
z(i,j)=-sqrt(256-(x(i))^2-(y(j))^2);
end
end
end
disp(' ');
disp(' ');
|
|
disp(' : z(i,j)=-sqrt(256-(x(i))^2-(y(j))^2);
disp(' , ');
function[x,z,masx,masz]=scanx1(xleft,xright,h,y);
%
n=ceil(abs((xright-xleft)/h)); %
% ceil
masx(1)=xleft;
masz(1)=fz_xy2(xleft,y);
for i=2:n
masx(i)=masx(i-1)+h;
masz(i)=fz_xy2(masx(i),y);
end
%
min=masz(1); %
num=1;
for i=2:n
if masz(i)<min
min=masz(i);
num=i;
end
end
% ()
x=masx(num);
z=masz(num);
end
function[y,z,masy,masz]=scany1(yleft,yright,hy,x);
%
n=ceil(abs((yright-yleft)/hy));
masy(1)=yleft;
masz(1)=fz_xy2(x,yleft);
for i=2:n
masy(i)=masy(i-1)+hy;
masz(i)=fz_xy2(x,masy(i));
end
%
min=masz(1); %
num=1;
for i=2:n
if masz(i)<min
min=masz(i);
num=i;
end
end
% ()
y=masy(num);
z=masz(num);
end
% 2-
function[x,y,z,xleft,xright,yleft,yright,xn]=scan2();
xleft=input(' X =');
xright=input(' X =');
hx=input(' X=');
yleft=input(' Y =');
yright=input(' Y =');
hy=input(' Y=');
eps=input(' ( ):');
choiceTab=input(' ? =1 =0 ');
%
xn=ceil(abs((xright-xleft)/hx)); %
%
x=xleft;
y=yleft;
z=-1;
disp(' ');
for i=1:xn
%
[x,z]=scanx1(xleft,xright,hx,y); %
%
if choiceTab==1
string=print(= %4i\t x= %7.3f\t y= %7.3f\t= %7.3f\t ,i,x,y,z);
disp(string);
end
zx=z;
[y,z]=scany1(yleft,yright,hy,x); %
%
if choiceTab==1
string=print(= %4i\t x= %7.3f\t y= %7.3f\t= %7.3f\t y,i,x,y,z);
disp(string);
end
zy=z;
dz(i)=abs(zx-zy);
if dz(i)<eps
disp(dz<eps);
break;
end
end
end
%
[x,y,z,xleft,xright,yleft,yright,xn]=scan2();
%
choiceGraf=input(' ? =1 =0 ');
%
% num2str
disp(' ');
sxy=strcat(' : =',num2str(x),' =',num2str(y));
sz=strcat(' =',num2str(z));
disp(sxy) %
disp(sz)
%
if choiceGraf==1
hx=(xright-xleft)/xn;
hy=(yright-yleft)/xn;
%
masx=xleft:hx:xright;
masy=yleft:hy:yright;
masz=fz_xy2(masx,masy);
surf(masx,masy,masz);
grid on;
title(z=-sqrt(256-x.^2-y.^2));
xlabel(X);
ylabel(Y);
zlabel(Z);
smin=strcat(\leftarrow Minimum (,num2str(x),,,num2str(y),,,num2str(z),));
text(x,y,z,smin);
zeroMas=masx*0;
hold on;
%surf(zeroMas,zeroMas,masz); %
%surf(zeroMas,masy,zeroMas); %
%surf(masx,zeroMas,zeroMas); %
legend(z(i,j)=-sqrt(256-x(i)*x(i)-y(j)*y(j)),0);
end
end
, :
: z(i,j)=-sqrt(256-(x(i))^2-(y(j))^2)
,
X =-10
|
|
X =10
X=.5
Y =-10
Y =10
Y=.5
( ):.1
? =1 =0 1
= 1 x= 0.000 y=-10.000 =-12.490
= 1 x= 0.000 y= 0.000 =-16.000 y
= 2 x= 0.000 y= 0.000 =-16.000
= 2 x= 0.000 y= 0.000 =-16.000 y
dz<eps
? =1 =0 1
: =0 =0
=-16
. 3.4.
:
;
;
, .
, () . (, ) . .
, .
function[]=ProbShag2D_170809();
function [f]=Function(xVector);
nVar=length(xVector); % nVar
f=1;
for i=1:nVar
f=f+(xVector(i)-2)^2;
end
end
%
disp(' ');
disp(' ');
disp(' : z(i,j)=-sqrt(256-(x(i))^2-(y(j))^2)');
disp(' , ');
function[xOptVector,fMinValue,leftBorderVector,rightBorderVector,stepNumberVector]=FunctionPS();
numberOfVariables=input(' :');
% , ,
for i=1:numberOfVariables
lBVstring=strcat(' (',num2str(i),'):');
leftBorderVector(i)=input(lBVstring);
rBVstring=strcat(' (',num2str(i),'):');
rightBorderVector(i)=input(rBVstring);
sNVstring=strcat( (,num2str(i),):);
stepNumberVector(i)=input(sNVstring);
OK=input( =1 =0);
if OK==0
i=i-1;
continue;
end
end
k=1;
for i=1:numberOfVariables
xVector(i)=(leftBorderVector(i)+rightBorderVector(i))/2; %
end
funVector(k)=Function(xVector); %
df=-1;
%
control=1;
while (df<0)&(control>0)
if k>1000
disp('k>1000');
break;
end
%
%
l=0;
for i=1:(numberOfVariables*2)
funChangeVector(i)=0; % ,
end
disp(' ');
for i=1:numberOfVariables
if (leftBorderVector<=xVector(i))&(xVector(i)<=rightBorderVector(i))
xVector(i)=xVector(i)+stepNumberVector(i);
l=l+1;
funChangeVector(l)=Function(xVector)-funVector(k);
disp(strcat(funChangeVector(,num2str(l),)=,num2str(funChangeVector(l))));
xVector(l)=xVector(l)-stepNumberVector(l);
|
|
else
control=-1;
disp(control=-1;);
end
end
% %
l=0;
disp(' ');
for i=1:numberOfVariables
if (leftBorderVector<=xVector(i))&(xVector(i)<=rightBorderVector(i))
xVector(i)=xVector(i)-stepNumberVector(i);
l=l+1;
funChangeVector(l+numberOfVariables)=Function(xVector)-funVector(k);
disp(strcat(funChangeVector(,num2str(l),)=,num2str(funChangeVector(l+numberOfVariables))));
xVector(l)=xVector(l)+stepNumberVector(l);
else
control=-1;
end
end
disp(strcat(xVector ,num2str(k), ,num2str(xVector), funChangeVector ,num2str(k), ,num2str(funChangeVector)));
minCF=0;
for i=1:(numberOfVariables*2)
if minCF>funChangeVector(i)
minCF=funChangeVector(i);
if i<=numberOfVariables
indexOfArg=i;
znak=1;
else
indexOfArg=i-numberOfVariables;
znak=-1;
end
end
end
disp(strcat( ,num2str(indexOfArg), =,num2str(xVector(indexOfArg))));
% ,
% ( )
if znak==1
xVector(indexOfArg)=xVector(indexOfArg)+stepNumberVector(indexOfArg);
else
xVector(indexOfArg)=xVector(indexOfArg)-stepNumberVector(indexOfArg);
end
k=k+1;
funVector(k)=Function(xVector); %
df=funVector(k)-funVector(k-1); %
if df>0 %
%
if znak==1
xVector(indexOfArg)=xVector(indexOfArg)-stepNumberVector(indexOfArg);
else
xVector(indexOfArg)=xVector(indexOfArg)+stepNumberVector(indexOfArg);
end
funVector(k)=Function(xVector); %
disp(df>0);
end
disp(strcat(xVector: ,num2str(xVector), function= , num2str(funVector(k))));
end
%
xOptVector=xVector;
fMinValue=funVector(k);
end %
%
[xOptVector, fMinValue, leftBorderVector, rightBorderVector, stepNumberVector] =FunctionPS();
%
disp(' ');
for i=1:length(xOptVector)
string=sprint( = %4i\t = %7.3f\t,i,xOptVector(i));
disp(string);
end
string=sprint( = %7.3f,fMinValue);
disp(string);
%
%
if length(xOptVector)==5
hx=(rightBorderVector(1)-leftBorderVector(1))/100;
hy=(rightBorderVector(2)-leftBorderVector(2))/100;
x=leftBorderVector(1):hx:rightBorderVector(1);
y=leftBorderVector(2):hy:rightBorderVector(2);
z(2,2)=0;
for i=2:100
for j=2:100
x(i)=x(i-1)+hx;
y(j)=y(j-1)+hy;
xV(1)=x(i);
xV(2)=y(j);
z(i,j)=Function(xV);
end
end
surf(x,y,z);
title(f=f+(xVector(i)-2)^2;);
xlabel(X);
ylabel(Y);
zlabel(Z);
text(xOptVector(1),xOptVector(2),fMinValue,\leftarrow Minimum );
legend(f=f+(xVector(i)-2)^2,0);
end
end
- (, . 3.4).
, :
: z(i,j)=-sqrt(256-(x(i))^2-(y(j))^2)
,
:2
(1):-10
(1):10
(1):0.5
=1 =01
(2):-10
(2):10
(2):0.5
=1 =01
:1 =-1.75
:2 =-1.75
:1 =2.25
:2 =2.25
:1 :0 0
:1 =0
:0.5 0 function=7.25
:1 =-1.25
|
|
:2 =-1.75
:1 =1.75
:2 =2.25
:2 :0.5 0
:2 =0
:0.5 0.5 function=5.5
:1 =-0.75
:2 =-0.75
:1 =1.25
:2 =1.25
:5 :1 1
:1 =1
:1.5 1 function=2.25
:1 =-0.25
:2 =-0.75
:1 =0.75
:2 =1.25
:6 :1.5 1
:2 =1
:1.5 1.5 function=1.5
:1 =0.25
:2 =-0.25
:1 =0.25
:2 =0.75
:8 :2 1.5
:2 =1.5
:2 2 function=1
:1 =0.25
:2 =0.25
:1 =0.25
:2 =0.25
:9 :2 2
:1 =2
df>0
:2 2 function=1
= 1 = 2.000
= 2 = 2.000
= 1.000