Home > Software engineering >  Components of a vector from its modulus and angles
Components of a vector from its modulus and angles

Time:11-07

there is a way in matlab to write the component of a vector starting from its modulus?. Maybe in a randomic way, but so that, by doing the reverse operation, I obtain the starting module

for example:

V_0 = 60  % starting modulus

perhaps I should use a for loop, But How?

CodePudding user response:

I assume you're looking for a way, starting with the length of the vector to generate a vector with a random orientation but the specified length?

I'll consider a couple of cases here. First, for a 2-dimensional, real, vector. This is simply an application of polar-to-rectangular coordinate conversions.

v0 = 60; % Desired length

theta = 2 * pi * rand(1,1); % Random angle between 0 and 2pi

x0 = v0 * cos(theta);
y0 = v0 * sin(theta);

v = [x0 y0]; % Assemble the new vector
% Could also reduce line count by: v = v0 * [cos(theta) sin(theta)]

vLength = norm(v); % Should be the same as v0

The second case is if you're interested in complex numbers with the specified modulus. He we make use of Euler's formula.

v0 = 60; % Desired length

theta = 2 * pi * rand(1,1); % Random angle between 0 and 2pi

v = v0 * exp(1j * theta);

vLength = abs(v); % Should be the same as v0

For the third case, I'll consider arbitrary dimensioned vectors. This leverages a technique from What is a good method to find random points on the n-sphere when n is large?

v0 = 60; % Desired length
N = 7; % Number of dimensions

rndVector = randn(1, N); % Random, Gaussian, vector with N elements
rndVectorNorm = rndVector / norm(rndVector); % Normalized to unit length

v = v0 * rndVectorNorm; % Scale to desired length

vLength = norm(v); % Should be the same as v0
  • Related