MATLAB Answers

Creating a Random Vector with Several Conditions

6 views (last 30 days)
Onur Ersoy
Onur Ersoy on 26 Dec 2019
Commented: darova on 30 Dec 2019
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.

Accepted Answer

John D'Errico
John D'Errico on 26 Dec 2019
Edited: John D'Errico on 26 Dec 2019
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.
Onur Ersoy
Onur Ersoy on 27 Dec 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.

Sign in to comment.

More Answers (1)

darova on 27 Dec 2019
You can just rotate circle and select points you want. I used Rodriguez formula

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by