clear;clf;cla reset;                                 % 초기화
X=[0 1 1 0];                                          % 개미 각각의 초기 X 좌표
Y=[0 0 1 1];                                          % Y 좌표
Z=[0 1 0 1];                                          % Z 좌표
r=[X;Y;Z];                                            % 개미들의 위치
v=0.01;                                                 % 속도
track=r;                                                % 개미의 위치 변화
tmax=400;                                            % 최대 시간
color=char('or','hb','+','d');                   % 표시 형식

hold on
for t=2:tmax
    direction=r(:,[2 3 4 1])-r(:,[1 2 3 4]);   % 방향 벡터
    udir=direction/norm(direction(:,1));     % 단위 벡터
    r=r+v*udir;                                        % 위치 계산
    if norm(r(:,1)-[0.5;0.5;0.5]) < 0.01       % 중앙 위치에 가까워지면 종료.
        break;
    end
    track(:,:,t)=r;                                     % 좌표를 track에 저장
end

for m=1:4                                              %SQUEEZE Remove singleton dimensions
    x(:,m)=squeeze(track(1,m,:));y(:,m)=squeeze(track(2,m,:));z(:,m)=squeeze(track(3,m,:));
end

plot3(x,y,z)                                           % 입체로 그래프 그림

for m=1:4                                               % 개미를 좌표에 표시
    p(m)=plot3(x(1,m),y(1,m),z(1,m),color(m));
end

view(3)                                                 % 입체로 보기
grid on;                                                 % 눈금선 표시

for t=1:length(x(:,1))                                % 개미의 이동을 애니메이션 처럼 표시
    for m=1:4
        set(p(m),'XData',x(t,m),'YData',y(t,m),'ZData',z(t,m))
    end
    drawnow
end
hold off 
사용자 삽입 이미지
크리에이티브 커먼즈 라이센스
Creative Commons License

"Matlab" 카테고리의 다른 글

Posted by downright

2008/07/01 23:07 2008/07/01 23:07

Leave a comment
[로그인][오픈아이디란?]

% 앞의 개미의 방향을 따라 일정한 속력으로 이동하는 개미의 궤적

X=[0 0 1 1];                                 % X 좌표
Y=[0 1 1 0];                                 % Y 좌표
r=[X;Y];
v=0.0005;                                    % 개미의 이동 속도
track=r;                                     % 초기 위치 좌표
tmax=10000;                                  % 최종 시간
color=char('or','hb','+','d');               % 4마리 개미 표시 설정

for t=2:tmax
    direction=r(:,[2 3 4 1])-r(:,[1 2 3 4]); % 앞의 개미에서 자신의 위치를 계산한 방향
    udir=direction/norm(direction(:,1));     % 방향 단위 벡터
    r=r+v*udir;                              % 속도에 방향을 곱하여 위치 계산
    if norm(r(:,1)-[0.5;0.5]) < 0.01         % 개미가 중앙에 오면 break 명령
        break;
    end
    track(:,:,t)=r;                          % 개미 4마리의 위치를 track에 저장
end

t=1;                                         % 값 초기화
x=[];y=[];
for m=1:4                                    % 각 개미의 x,y 좌표를 track으로 부터 계산
    x(:,m)=squeeze(track(1,m,:));
    y(:,m)=squeeze(track(2,m,:));
end

clf                                          % 기존 이미지를 지우고 재설정
cla reset;
hold on                                      % 이미지 유지 on
plot(x,y)                                    % 개미의 궤적을 그림
for m=1:4                                    % 개미 4마리를 초기 위치에 표시
    p(m)=plot(x(1,m),y(1,m),color(m),'EraseMode','xor');
end

for t=1:length(x(:,1))                       % 시간의 변화에 따라 set을 이용하여 개미 위치 변화
    for m=1:4
        set(p(m),'XData',x(t,m),'YData',y(t,m))
    end
    drawnow
end

axis([0 1 0 1])                              % x, y 축 설정
hold off

사용자 삽입 이미지

크리에이티브 커먼즈 라이센스
Creative Commons License

"Matlab" 카테고리의 다른 글

Posted by downright

2008/01/31 22:26 2008/01/31 22:26

Leave a comment
[로그인][오픈아이디란?]

Cramer rule - 크래머 공식

크래머 공식(Cramer's rule)은 선형연립방정식의 해를 행렬식으로 표현하는 선형대수학의 정리(theorem)이다.
이름은 가브리엘 크래머(Gabriel Cramer) (1704 - 1752)에게서 유래한다.

방정식이 많은 경우의 실제 해의 계산에 있어서는 그리 유용하지 않지만, 피봇팅(pivoting)이 필요하지 않은 경우 작은 크기의 행렬에서는 가우스 소거법보다 훨씬 효율적이다.
크래머 공식은 연립방정식의 해를 외재적으로 표현하기 때문에 이론의 전개에 유용하다.

연립방정식이 다음과 같은 행렬간의 곱으로 표현될 때.

Ax = c  식에서 정사각행렬(square matrix) A는 역행렬을 갖고, 벡터 x는 (xi)를, 벡터 c는 (ci)를 성분으로
갖는 열벡터이다.

2x2 행렬에서 공식을 적용해 보면, 주어진 연립방정식이 다음과 같을 때,

ax + by = e
cx + dy = f,

이 식은



로 쓸 수 있으며, 공식을 적용하면,

    
이 된다.

출처: 위키사전(크래머 공식)
참고자료: http://www.math.gatech.edu/~bourbaki/math2601/Web-notes/9.pdf

크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by downright

2007/11/10 01:20 2007/11/10 01:20

Leave a comment
[로그인][오픈아이디란?]

사용자 삽입 이미지
In mathematics, the spherical coordinate system is a coordinate system for representing geometric figures in three dimensions using three coordinates: the radial distance of a point from a fixed origin, the zenith angle from the positive z-axis, and the azimuth angle from the positive x-axis.

Definition
The three coordinates (ρ, θ, φ) are defined as:

   * ρ ≥ 0 is the distance from the origin to a given point P.
   * 0 ≤ θ ≤ 360° is the angle between the positive x-axis and the line from the origin to the P projected onto the xy-plane.
   * 0 ≤ φ ≤ 180° is the angle between the positive z-axis and the line formed between the origin and P.

θ is referred to as the azimuth, while φ is referred to as the zenith, colatitude or polar angle.
θ and φ and lose significance when ρ = 0 and θ loses significance when sin(φ) = 0 (at φ = 0 and φ = 180°).

To plot a point from its spherical coordinates, go ρ units from the origin along the positive z-axis, rotate φ about the y-axis in the direction of the positive x-axis and rotate θ about the z-axis in the direction of the positive y-axis.

Coordinate system conversions
As the spherical coordinate system is only one of many three-dimensional coordinate systems, there exist equations for converting coordinates between the spherical coordinate system and others.

   Cartesian coordinate system
The three spherical coordinates are obtained from Cartesian coordinates by:
    
Note that the arctangent must be defined suitably so as to take account of the correct quadrant of y / x. The atan2 or equivalent function accomplishes this for computational purposes.

Conversely, Cartesian coordinates may be retrieved from spherical coordinates by:
     

   Cylindrical coordinate system

The cylindrical coordinate system is a three-dimensional extrusion of the polar coordinate system, with an h coordinate to describe a point's height above or below the xy-plane. The full coordinate tuple is (r, θ, h).

Cylindrical coordinates may be converted into spherical coordinates by:
    

Spherical coordinates may be converted into cylindrical coordinates by:
    

출처: 위키백과

크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by downright

2007/09/23 20:31 2007/09/23 20:31

Leave a comment
[로그인][오픈아이디란?]

Cross product - 벡터 곱

수학에서 외적(外積)은 3차원 공간의 벡터들간의 이항연산의 일종이다.
연산의 결과가 스칼라인 내적(內積)과는 달리 연산의 결과가 벡터이기 때문에 벡터곱(vector product)이라고
불리기도 한다. 외적은 물리학의 각운동량, 로렌츠 힘등의 공식에 등장한다.

정의

두 벡터 ab의 외적은 a x b라 쓰고(수학자들은 a ^ b라고 쓰기도 한다.), 다음과 같이 정의된다.

\mathbf{a} \times \mathbf{b} = \mathbf\hat{n} \left| \mathbf{a} \right| \left| \mathbf{b} \right| \sin \theta

식에서 θ는 ab가 이루는 각을 나타내며, nab에 공통으로 수직인 단위벡터를 나타낸다.

위 정의에서의 문제점은 ab에 공통으로 수직인 방향이 두개라는 점이다. 즉, n이 수직이면, -n도 수직이다.

어느 것을 두 벡터의 외적으로 할 것인가는 벡터공간의 방향성(orientation)에 따라 달라진다.
오른손 좌표계에서는 a×b는, a, b, a×b가 오른손 좌표계 방향을 따르도록 정의되고, 왼손좌표계에선
마찬가지로 이 순서의 세 벡터가 왼손 좌표계 방향을 따르도록 정의된다.

--------------------------------------------------------------------------------------------------

In mathematics, the cross product is a binary operation on two vectors in a three-dimensional Euclidean space that results in another vector which is perpendicular to the two input vectors. By contrast, the dot product produces a scalar result. In many engineering and physics problems, it is handy to be able to construct a perpendicular vector from two existing vectors, and the cross product provides a means for doing so. The cross product is also known as the vector product, or Gibbs vector product.

The cross product is not defined except in three-dimensions (and the algebra defined by the cross product is not associative). Like the dot product, it depends on the metric of Euclidean space. Unlike the dot product, it also depends on the choice of orientation or "handedness." Certain features of the cross product can be generalized to other situations. For arbitrary choices of orientation, the cross product must be regarded not as a vector, but as a pseudovector. For arbitrary choices of metric, and in arbitrary dimensions, the cross product can be generalized by the exterior product of vectors, defining a two-form instead of a vector.

Definition

The cross product of two vectors a and b is denoted by a × b. In a three-dimensional Euclidean space, with a usual right-handed coordinate system, it is defined as a vector c that is perpendicular to both a and b, with a direction given by the right-hand rule and a magnitude equal to the area of the parallelogram that the vectors span.

The cross product is given by the formula

\mathbf{a} \times \mathbf{b} = a b \sin \theta \ \mathbf{\hat{n}}

where θ is the measure of the (non-obtuse) angle between a and b (0° ≤ θ ≤ 180°), a and b are the magnitudes of vectors a and b, and \mathbf{\hat{n}} is a unit vector perpendicular to the plane containing a and b. If the vectors a and b are collinear (i.e., the angle θ between them is either 0° or 180°), by the above formula, the cross product of a and b is the zero vector 0.

The direction of the vector \mathbf{\hat{n}} is given by the right-hand rule, where one simply points the forefinger of the right hand in the direction of a and the middle finger in the direction of b. Then, the vector \mathbf{\hat{n}} is coming out of the thumb (see the picture on the right).

Using the cross product requires the handedness of the coordinate system to be taken into account (as explicit in the definition above). If a left-handed coordinate system is used, the direction of the vector \mathbf{\hat{n}} is given by the left-hand rule and points in the opposite direction.

This, however, creates a problem because transforming from one arbitrary reference system to another (e.g., a mirror image transformation from a right-handed to a left-handed coordinate system), should not change the direction of \mathbf{\hat{n}}. The problem is clarified by realizing that the cross-product of two vectors is not a (true) vector, but rather a pseudovector. See cross product and handedness for more detail.

출처: 위키링크(외적), 위키링크(Cross product), 위키링크(vector)

크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by downright

2007/08/30 23:42 2007/08/30 23:42

Leave a comment
[로그인][오픈아이디란?]

Dot product - 스칼라 곱

스칼라 곱(scalar product, dot product)은 두 벡터로 스칼라를 계산하는 이항연산이다.
스칼라 곱을 사용하는 모든 유클리드 공간은 내적공간이다.

두 벡터 a = [a1, a2, … , an], b = [b1, b2, … , bn]의 스칼라 곱은 다음과 같다:

\mathbf{a}\cdot \mathbf{b} = a_1b_1 + a_2b_2 + \cdots + a_nb_n = \sum_{i=1}^n a_ib_i
--------------------------------------------------------------------------------------------------

The dot product of two vectors (from an orthonormal vector space)
a = [a1, a2, … , an] and b = [b1, b2, … , bn] is by definition:
\mathbf{a}\cdot \mathbf{b} = \sum_{i=1}^n a_ib_i = a_1b_1 + a_2b_2 + \cdots + a_nb_n

where Σ denotes summation notation.

For example, the dot product of two three-dimensional vectors [1, 3, −5] and [4, −2, −1] is

\begin{bmatrix}1&3&-5\end{bmatrix} \cdot \begin{bmatrix}4&-2&-1\end{bmatrix} = (1)(4) + (3)(-2) + (-5)(-1) = 3.

Using matrix multiplication and treating the (column) vectors as n×1 matrices,
the dot product can also be written as:

\mathbf{a} \cdot \mathbf{b} = \mathbf{a}^T \mathbf{b} \,

where aT denotes the transpose of the matrix a.

Using the example from above, this would result in a 1×3 matrix (i.e., vector) multiplied by a 3×1 vector (which, by virtue of the matrix multiplication, results in a 1×1 matrix, i.e., a scalar):

\begin{bmatrix}
    1&3&-5
\end{bmatrix}\begin{bmatrix} 
    4\\-2\\-1
\end{bmatrix} = \begin{bmatrix}
    3
\end{bmatrix}.

--------------------------------------------------------------------------------------------------
The dot product of two vectors a and b (sometimes called inner product, or, since its result is a scalar, the scalar product) is denoted by a ∙ b and is defined as:

\mathbf{a}\cdot\mathbf{b}
=\left\|\mathbf{a}\right\|\left\|\mathbf{b}\right\|\cos\theta

where ||a|| and ||b|| denote the norm (or length) of a and b, and θ is the measure of the angle between a and b (see trigonometric function for an explanation of cosine). Geometrically, this means that a and b are drawn with a common start point and then the length of a is multiplied with the length of that component of b that points in the same direction as a.

The dot product can also be defined as the sum of the products of the components of each vector:

\mathbf{a} \cdot \mathbf{b} = \langle a_1, a_2, \dots, a_n \rangle \cdot \langle b_1, b_2, \dots, b_n \rangle = a_1 b_1 + a_2 b_2 + \dots + a_n b_n

where a and b are vectors of n dimensions; a1, a2, …, an are coordinates of a; and b1, b2, …, bn are coordinates of b.

This operation is often useful in physics; for instance, work is the dot product of force and displacement.

출처: 위키 링크(Dot Product), 위키 링크(Vector)
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by downright

2007/08/30 23:30 2007/08/30 23:30

Leave a comment
[로그인][오픈아이디란?]