New to Matlab, help with vectors

1. Feb 20, 2016

So like the title says, I'm new to Matlab. I took a programming class on Fortran last year before my college changed the requirement so programming is not new to me all together. For a few of my classes we are allowed to use programs such as Matlab and Maple to help us solve problems. Most of them being physics classes (specifically statics) we deal a lot with vectors and use the notation xi+yj+zk. I was wondering if there was a way to solve systems of equations in Matlab such as the sum of the forces in x,y,z and the sum of the moments in x,y,z and being able to keep this notation of the unit vector multipliers. I tried just entering my vectors as 1:3 matrix and having each spot multiplied by the respective operator.

ex:
syms i j k
A=[2*i 3*j 1*k]
B=[1*i 2*j 3*k]

However I quickly found out that when taking cross products this can quickly get confusing when the program returns combinations of the operators which i would have to make sure and keep careful track of when getting my final answer and double checking.

ex:
cross(A,B)

ans=

[ 7*j*k, -5*i*k, i*j]

So is there a way to keep them separate as to and retain "i,j,k" and/or possibly have an if statement of something of the sort so that when you get say i*j Matlab will catch it and automatically replace it with k?

Thanks for the help. Sorry if I have trouble with any replies, like i said I don't know Matlab, just Fortran so please bear with me as I try to halfway teach myself this language.

2. Feb 20, 2016

Staff: Mentor

You're mixing notations here. When you write $\vec{A} = 2 \hat{i} + 3 \hat{j} + 1 \hat{k}$, the unit vectors correspond to [1 0 0], [0 1 0], and [0 0 1], respectively. So to reproduce that equation, you would write
A = 2*[1 0 0] + 3*[0 1 0] + 1*[0 0 1]

Of course, it is much easier, and clearer, to simply write
A = [2 3 1]
B = [1 2 3]
and A and B will behave as expected.

3. Feb 20, 2016

Oh I see what you mean about mixing notations. I don't know why I had trouble before. Seems pretty clear now. Just one more thing. How do I get a numerical answer for T2 in the code below? Right now it gives it to me as an equation with T1 still in as a variable.

>>a=[0 4 -8],b=[3.5 4 0], c=[-3.5 4 0], d=[0 12 -8], e=[0 12 0], g=[0 9 -8]

>> syms T1 T2

>> B=b-a, C=c-a, E=e-d

>> FB=T1*(B/norm(B)), FC=T2*(C/norm(C)), FE=T1*(E/norm(E)), W=[0 0 -4414.5]

>> sum_Fz=FB(3)+FC(3)+FE(3)+W(3)

>> sum_mom=cross(g-a,W)+cross(d-a,FE)

>> T1=solve(sum_mom,T1)

>> T2=solve(sum_Fz,T2)

4. Feb 20, 2016

Staff: Mentor

I'm not familiar with symbolic calculations in Matlab. Hopefully someone else will chime in.

5. Feb 20, 2016

Maylis

You can put
T1 = double(T1) and T2 = double(T2) after the lines where you declare T1 and T2

6. Feb 20, 2016

For whatever reason double wasn't doing what I need but I did find the command sym which doing what I want. Where x=sym('x') creates a variable x

http://www.mathworks.com/help/symbolic/sym.html

At some points Matlab will sub in variable values for the variable like I want, but in the end result I'm still not getting a number. Any ideas on what I'm doing wrong? In the code below I am trying to solve for the magnitude of a force F. I have written the magnitude as f and multiplied it by the x,y,z components and done the same with Fg.

>> alpha=10;phi=22;mu=.16;Fg=25;
f=sym('f');N=sym('N');S=sym('S');w=sym('w');h=sym('h');
F=f*[-cosd(alpha) sind(alpha) 0];Fg=25*[sind(phi) -cosd(phi) 0]; S=mu*N;
sumfy=Fg(2)+F(2)+N;sumfx=Fg(1)+F(2)+S;sumM=w*N+(h/2)*S;

N=solve(sumfy,N);

f=solve(sumfx,f);

This is a problem for my statics class so sumfx, sumfy, and sumM are sum of the forces in x, sum of the forces in y, and sum of the moments respectively.

Disclaimer:
I have already done this problem by hand so I'm not asking for how to do the problem. Just how to use Matlab as a tool in the future.

7. Feb 20, 2016

Maylis

Take a look at the function called subs

Code (Text):

a=[0 4 -8];b=[3.5 4 0]; c=[-3.5 4 0]; d=[0 12 -8]; e=[0 12 0]; g=[0 9 -8];

syms T1 T2

B=b-a; C=c-a; E=e-d;

FB=T1*(B/norm(B)); FC=T2*(C/norm(C)); FE=T1*(E/norm(E)); W=[0 0 -4414.5];

sum_Fz=FB(3)+FC(3)+FE(3)+W(3);

sum_mom=cross(g-a,W)+cross(d-a,FE);

T1=solve(sum_mom,T1);
T1 = double(T1)

T2=solve(sum_Fz,T2);
T2 = sym(T2);
T2 = subs(T2,T1);
T2 = double(T2);

T1 =

2.7591e+03
T2 =

-952.1267

Last edited: Feb 20, 2016