drawing simple 3d vectors without manual inputing values

I have tried to find the answer to this but have not found explicitly what I'm looking for
All i want to do is define 2 vectors
a = [6,-8,2]
b = [4,6,12]
I want to plot in 3d
a+b
a-b
dot(a,b)
cross (a,b)
I have seen how to do it in mupad and i know you can use quiver3 but you have to manual input the points such as
"quiver3(x,y,x,u,v,w)" I want to be able to say quiver3("the name of the vector) and be done
How do i do this? Maylab is extremely powerful yet in all my searching i can not find the ability to simply plot 3 lousy R3 vectors
If someone could explain a simple way of what i want to do that would be great Thank you!

1 Comment

I did figure out how to do the following
i
a = [6,-8,2];
b = [4 ,6,12];
c = a + b
quiver3(0,0,0,c(1),c(2),c(3));
However, how do i get them to all show up on the same plot? Right now it over writes itself and erases the vector every time i use the quiver command. How do i get multiple vectors to show on the same plot.

Sign in to comment.

Answers (1)

I have to admit I’m embarrassed by this construction, but if you absolutely must avoid using quiver3 with its usual argument list and want to use vectors, this will work:
a = [6,-8,2];
b = [4,6,12];
qv = @(a,b) eval(sprintf('quiver3(%f,%f,%f,%f,%f,%f)', [a;b]'));
figure(1)
qv(a,b)
grid on

4 Comments

Is there any chance you could explain some of that? because i have no idea what you just did
Thanks
Sure!
The eval function takes a text string and evaluates it as a MATLAB expression. It is not necessarily considered to be good programming practice, but can be used to solve problems, as in ‘[A,b] = equationsToMatrix(eqns,vars), large amount of vars’ that used it to convert a collection of variables into a vector.
Here, I used the sprintf function to create a string that calls quiver3 with the two vectors you input to it, and used that string as an input to the eval function, that evaluated it as a MATLAB command and produced the plot.
You can see that string without evaluating it by using this call to sprintf:
check = sprintf('quiver3(%f,%f,%f,%f,%f,%f)', [a;b]')
It will display in the Command Window:
check =
quiver3(6,-8,2,4,6,12)
ok scratch that Way to complicated. This is borderline insane. There has got to be an easier way to simply plot 2 vectors. Wolfram alpa will do this in plain english.
If i use the following code i made
i
a = [6,-8,2];
b = [4 ,6,12];
c = a + b
quiver3(0,0,0,c(1),c(2),c(3));
Can you tell me how to plot multiple quivers on the same plot please?
It’s actually not complicated, but the eval step will slow it a bit.
To plot several quiver arrows in quiver3, you have to create the vectors you want to plot as matrices of quiver origins and directions. That’s best explained in Projectile Path Over Time.

Sign in to comment.

Categories

Asked:

on 1 Sep 2014

Commented:

on 1 Sep 2014

Community Treasure Hunt

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

Start Hunting!