Creating a Random Vector with Several Conditions

Hello everyone,
I have vector with known values. Z1=[x1,y1,z1]. I want to create a vector (Y1=[x2,y2,z2]) with several conditions. Conditions are;
0<y2<1, -1<z2<0, -1<x2<1
Also Z1 and Y1 must be perpendicular so that their dot product must be zero x1*x2+y1*y2+z1*z2=0 and Y1 should be a unit vector so that x2*x2+y2*y2+z2*z2=1.
I can not use the symbolic toolbox,fsolve or anonymous functions since the code won't be able to suitable to compile before run-time in simulink. I really appreciate any ideas.
Thanks.

 Réponse acceptée

John D'Errico
John D'Errico le 26 Déc 2019
Modifié(e) : John D'Errico le 26 Déc 2019

1 vote

Um, trivial? I suppose trivial is in the eyes of the beholder.
The equation dot(Z1,Y1) == 0 implies that Y1 lies in a plane in R^3, one that contains the origin. (That dot product is the orthogonality requirement.)
Next, the requirement that Y1 has unit length is just the equation of a sphere, with center at the origin.
The intersection of the sphere and plane will always be a unit circle, that lies in the plane of interest. You can parameterize that circle as a function of polar angle, in the rotated coordinate system.
So all you need do is choose a random point on the circle, one that satisfies the requirements on x,y,z. The constraints are just a constraint that specifies which octant the point can lie in. All of this can be done simply enough, if you understand how to transform the problem by rotating into the plane of interest. For example...
I would refuse to name the vectors Z1 and Y1, by the way. That is just poor coding practice, screaming to see you have bugs later on. You have a vector Z1, with elements [x1,y1,z1]? Then another vector named Y1, with elements [x2,y2,z2]? This is just asking for bugs. A bad idea. Learn to use more descriptive variable names. Your code will improve.

3 commentaires

Stephen23
Stephen23 le 26 Déc 2019
"Learn to use more descriptive variable names" ... without numbers in them.
Just for kicks, can we solve the problem using just pencil and paper? This should not be difficult, assuming I can do the algebra.
First, the orthogonality constraint...
dot([x1,y1,z1],[x2,y2,z2]) = 0
implies
x2 = -(z1*z2 + y1*y2))/x1
Substitute into the sphere equation.
y2^2 + z2^2 + ((z1*z2 + y1*y2))/x1)^2 = 1
This is the equation of an ellipse in the (y2,z2) plane, still centered at the origin.
So we have two variables remaining, y2 and z2. Converting to polar coordinates, we can write
y2 = r*sin(theta)
z2 = r*cos(theta)
The quadrant that has y2 positive, and z2 negative here will be when theta lives between pi/2 and pi (90-180 degrees, if you think in degrees.) We can think about the bounds on x2 later. Substitute...
r^2*sin(theta)^2 + r^2*cos(theta)^2 + r^2*((z1*cos(theta) + y1*sin(theta)))/x1)^2 = 1
...
r^2 = 1/(1 + ((z1*cos(theta) + y1*sin(theta)))/x1)^2)
Again, theta lives in the interval [pi/2, pi]. So any point in that interval should yield a solution. The only problem is if x2 still lives in the interval [-1,1]. That will depend on the constants [x1,y1,z1].
Onur Ersoy
Onur Ersoy le 27 Déc 2019
I never thought that way which transforms this problem into your solution. This simplifies the problem into basic math. Thank you Mr. D'Errico.

Connectez-vous pour commenter.

Plus de réponses (1)

darova
darova le 27 Déc 2019

1 vote

You can just rotate circle and select points you want. I used Rodriguez formula
img1.png

2 commentaires

Onur Ersoy
Onur Ersoy le 30 Déc 2019
I can not use anonymous functions because of compile before run-time. However, the method is very useful, thank you.
darova
darova le 30 Déc 2019
happy to help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Random Number Generation dans Centre d'aide et File Exchange

Produits

Version

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by