勾配の場(方向場、ベクトル場)

 投稿者:しばっち  投稿日:2020年 3月29日(日)19時39分6秒
  関数 f(x,y)を導関数(傾き)として勾配の場を描画します。

LET XS=-5
LET XE=5
LET YS=-5
LET YE=5
SET WINDOW XS,XE,YS,YE
SET COLOR MIX(15) 0,0,0
DRAW AXES
LET N=40
LET STX=(XE-XS)/N
LET STY=(YE-YS)/N
SET LINE COLOR "RED"
FOR Y=YS TO YE STEP STY
   FOR X=XS TO XE STEP STX
      LET THETA=ATN(F(X,Y))
      !'LET THETA=MOD(THETA+2*PI,2*PI)
      PLOT LINES:X,Y;X+STX*COS(THETA)*.6,Y+STY*SIN(THETA)*.6
      !'CALL ARROW(X,Y,X+STX*COS(THETA)*.6,Y+STY*SIN(THETA)*.6)
   NEXT X
NEXT Y
END

EXTERNAL  FUNCTION F(X,Y) !'   y'=F(X,Y) 傾き
WHEN EXCEPTION IN
!'LET F=-X/Y
!'LET F=X*X-X-2
!'LET F=Y
   LET F=Y*Y*X
   !'LET F=Y*X
   !'LET F=Y*X*X
   !'LET F=1/(X*X+1)
USE
   LET F=10000
END WHEN
END FUNCTION

EXTERNAL  SUB ARROW(X1,Y1,X2,Y2)
OPTION ANGLE DEGREES
PLOT LINES:X1,Y1;X2,Y2
LET TH=180-ANGLE(X1-X2,Y1-Y2)
LET L=SQR((X2-X1)^2+(Y2-Y1)^2)/2
LET X3=X2+L*COS(TH+160)
LET Y3=Y2-L*SIN(TH+160)
LET X4=X2+L*COS(TH-160)
LET Y4=Y2-L*SIN(TH-160)
PLOT LINES:X2,Y2;X3,Y3
PLOT LINES:X2,Y2;X4,Y4
END SUB
--------------------------------------------------------------------------------------------------
上記関数定義をdx/dt=f(x,y) dy/dt=g(x,y)として勾配の場を描画します。

LET XS=-5
LET XE=5
LET YS=-5
LET YE=5
SET WINDOW XS,XE,YS,YE
SET COLOR MIX(15) 0,0,0
DRAW AXES
LET N=40
LET STX=(XE-XS)/N
LET STY=(YE-YS)/N
SET LINE COLOR "RED"
FOR Y=YS TO YE STEP STY
   FOR X=XS TO XE STEP STX
      WHEN EXCEPTION IN
         LET M=G(X,Y)/F(X,Y) !' 傾き dy/dx
      USE
         LET M=10000*SGN(G(X,Y))
      END WHEN
      LET THETA=ATN(M)
      !'LET THETA=MOD(THETA+2*PI,2*PI)
      PLOT LINES:X,Y;X+STX*COS(THETA)*.6,Y+STY*SIN(THETA)*.6
      !'CALL ARROW(X,Y,X+STX*COS(THETA)*.6,Y+STY*SIN(THETA)*.6)
   NEXT X
NEXT Y
END

EXTERNAL  FUNCTION F(X,Y) !' dx/dt=f(x,y)
LET F=Y
END FUNCTION

EXTERNAL  FUNCTION G(X,Y) !' dy/dt=g(x,y)
LET G=-X
END FUNCTION
 

戻る