What is wrong with my vector calculator code?
4 views (last 30 days)
Show older comments
I'm trying to make a function that spits out a vector in a plane when given the equation of the plane (specified by a normal vector and a point on the plane), a vector in the plane (specified by two points in the plane), and the magnitude of the vector in question. My code looks like this:
function [xnew,ynew,znew] = pointc(Theta,N,Mag,P0,P1)
syms x y z
V = [x,y,z];
eqn1 = N(1)*(x-P1(1)) + N(2)*(y-P1(2)) + N(3)*(z-P1(3)) == 0;
V0 = [P1(1)-P0(1),P1(2)-P0(2),P1(3)-P0(3)];
V0mag = sqrt(sum(V0.^2));
eqn2 = dot(V0,V) == cosd(Theta)*Mag*V0mag;
eqn3 = sqrt(sum(V.^2)) == Mag;
eqns = [eqn1,eqn2,eqn3];
vars = [x,y,z];
[xi,yi,zi] = solve(eqns,vars);
xnew = double(xi);
ynew = double(yi);
znew = double(zi);
end
And my inputs look like this:
THeta = 108;
N = [0,0,1];
Mag = 1;
P0 = [-0.5, -0.6882, 0];
P1 = [0.5, -0.6882, 0];
I know that the output should be [-0.3090,0.9511,0], but what gets returned is [-0.3090,-0.3090]. Clearly, the x-value is correct, and the y-value is mirroring it. Can anyone tell me what I'm doing wrong, and why the z-value isn't appearing? Shouldn't it be returning as 0?
Thanks,
Hunter
0 Comments
Answers (1)
Tyler
on 25 Jul 2018
Hi Hunter,
When I run your code, I get the following outputs:
xnew = [-0.3090;-0.3090]
ynew = [-0.9511;0.9511]
znew = [0;0]
It seems that your expected output is the second value in each of these three variables. The first value of each is the same result with a different sign on the second value. It's likely that you aren't outputting what you think you are outputting, even if the code is working correctly. It seems to be outputting two vectors that solve the equation, but instead of the vectors you want, it is outputting the x-values, y-values and z-values as their own respective vectors. But, that's just a guess since I'm really only looking at the outputs and haven't looked very hard at what the code is doing. If you can't figure it out it may be helpful to use simpler numbers and integers to debug before moving on to what you actually need.
Tyler
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!