# New to Matlab, help with vectors

• MATLAB
In summary, I'm new to Matlab and 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. 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. Is there a way to keep them separate as to and retain "i,j,k" and
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.

syms i j k
A=[2*i 3*j 1*k]
B=[1*i 2*j 3*k]
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.

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)

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

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

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.

Take a look at the function called subs

Code:
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:

## 1. How do I create a vector in Matlab?

To create a vector in Matlab, you can use the square bracket notation and separate the elements with commas. For example, myVector = [1, 2, 3, 4, 5] will create a vector with 5 elements. You can also use the linspace function to create a vector with evenly spaced values.

## 2. How can I add or remove elements from a vector in Matlab?

To add elements to a vector, you can use the end keyword to append elements at the end of the vector. For example, myVector(end+1) = 6 will add the value 6 to the end of the vector. To remove elements, you can use the remove function or index the vector with the elements you want to keep. For example, myVector = myVector([1,3,5]) will remove the 2nd and 4th elements from the vector.

## 3. How do I perform operations on vectors in Matlab?

To perform operations on vectors, you can use the built-in functions and operators in Matlab. For example, to add two vectors together, you can use the + operator. If the vectors are of different sizes, they will be automatically expanded or truncated to match the size of the larger vector. You can also use functions like dot for dot product and cross for cross product.

## 4. Can I access specific elements or ranges of elements in a vector?

Yes, you can access specific elements or ranges of elements in a vector using indexing. The syntax is similar to indexing in other programming languages, where the first element has an index of 1. You can also use the end keyword to access the last element. For example, myVector(2:4) will return the 2nd, 3rd, and 4th elements of the vector.

## 5. How do I perform vector operations on multidimensional arrays in Matlab?

To perform vector operations on multidimensional arrays, you can use the reshape function to convert the array into a vector, perform the operation, and then reshape it back into the original dimensions. You can also use the dot and cross functions on multidimensional arrays, as they will automatically perform the operation on each pair of vectors within the array.

• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
925
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K