New to Matlab, help with vectors

  • Context: MATLAB 
  • Thread starter Thread starter Adam Holland
  • Start date Start date
  • Tags Tags
    Matlab Vectors
Click For Summary

Discussion Overview

The discussion revolves around using Matlab for vector calculations, particularly in the context of solving systems of equations related to physics problems involving forces and moments. Participants explore the representation of vectors, the use of symbolic variables, and methods for obtaining numerical solutions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion about using Matlab for vector notation and seeks guidance on maintaining the notation of unit vectors while performing operations like cross products.
  • Another participant points out that mixing notations can lead to confusion and suggests using numerical representations of vectors instead of symbolic unit vectors.
  • A participant asks how to obtain a numerical answer for a variable in their code, noting that it currently returns an equation with another variable still present.
  • One participant admits unfamiliarity with symbolic calculations in Matlab and hopes for further input from others.
  • Another participant suggests using the 'double' function to convert symbolic results to numerical values.
  • A later reply mentions finding success with the 'sym' function for creating symbolic variables, but still encounters issues with obtaining numerical results.
  • One participant recommends using the 'subs' function to substitute variables in symbolic expressions to achieve numerical results.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to maintain vector notation in Matlab or on the most effective methods for obtaining numerical results from symbolic calculations. Multiple competing views and methods are presented.

Contextual Notes

Some participants express uncertainty about the symbolic capabilities of Matlab, and there are references to specific commands that may not yield the expected results, indicating potential limitations in understanding or application.

Who May Find This Useful

Individuals new to Matlab, particularly those in physics or engineering courses that involve vector calculations and symbolic mathematics.

Adam Holland
Messages
7
Reaction score
0
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.
 
Physics news on Phys.org
Adam Holland said:
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K