Using 'solve' command in MATLAB R2008a

  • Thread starter Thread starter Dell
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around using the 'solve' command in MATLAB R2008a to find the components of a vector in a vector mechanics context. Participants are exploring how to solve a system of equations derived from vector cross products and forces, with a focus on automating the solution process for varying inputs.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes their approach to solving for the vector R (x, y, z) using the cross product of R and Ftot, leading to a system of equations.
  • Another participant suggests using the 'linsolve' function or matrix division as potential solutions to the problem of solving the equations derived from the matrix RF and Mtot.
  • A participant expresses frustration with the inability to directly use the 'solve' function on the contents of a matrix or vector, noting that manual input works but is not practical for their code.
  • Another participant proposes a workaround using 'eval' to construct a string of equations for the 'solve' function, although this is described as a brute force approach.
  • One participant advises that the user should input one component of R before attempting to solve for the others, emphasizing the need for orthogonality between Ftot and Mtot.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method to solve the equations. Multiple competing views and approaches are presented, with some expressing uncertainty about the effectiveness of suggested methods.

Contextual Notes

There are limitations regarding the assumptions made about the inputs and the dependencies on specific MATLAB functions and toolboxes, which may affect the applicability of the proposed solutions.

Dell
Messages
555
Reaction score
0
as a part of a code i have written to solve an equivalent system in vector mechanics, i have reached the point where i need to solve 3 equations to find x, y, z, (the placement of the Vector Ftot)

i know the total momentum, which i call Mtot, and i know the total force, which is Ftot, what i am looking for is R which i call (x, y, z) while i know that RxFtot=Mtot, so what i have done is

...

Code:
syms x y z
R=[x y z];
RF=cross(R,Ftot);

now what i want to do somehow is get MATLAB to solve the 3 equations i get

Code:
solve(RF(1)=Mtot(1),RF(2)=Mtot(2),RF(3)=Mtot(3))

but MATLAB doesn't recognise the contents of RF as an equation or Mtot as an answer, it wants me to type an actual equation which is not what i need since this is a code which needs to work for every system.

how can i use the solve function for the contents of a matrix?
 
Physics news on Phys.org
hopefully you guys understood what I am trying to do, if not this is my entire code, at the end you will see i get 3 equations to find x,y,z where in most cases only 2 will be linearly independent, so what i would like to do is get to some kind of equation wher i can plug in any x of my choice and MATLAB will give me the other 2,

Code:
nf=0;
nm=0;
nf=input('Enter number of Forces :');
nm=input('enter number of Moments :');
disp('------------------------------')

dim=['x' 'y' 'z'];

F=zeros(nf,3); %code for building  Force matrix
for i=1:nf
    for j=1:3;
        prompt=fprintf(1,'Enter magnitude (including direction) of F%d%s ',i,dim(j));
        F(i,j)=input(':'); 
    end
    disp('------------------------------')
end

 M=zeros(nm,3); %code for building  Moment matrix
 for i=1:nm;
     for j=1:3;
         prompt=fprintf(1,'Enter magnitude (including direction) of M%d%s ',i,dim(j));
         M(i,j)=input(':'); 
     end
     disp('------------------------------')
 end
 
 Ftot=[0 0 0]; %code for calculating total Force
 for i=1:nf;
     Ftot=Ftot+F(i,:);
 end
 
 Mm=[0 0 0];%code for calculating total moment excludinf moment from forces
 for i=1:nm;
     Mm=Mm+M(i,:);
 end
 disp('------------------------------')
 R=zeros(nf,3);%code for building matrix of moments due to moved forces
 for i=1:nf;
     for j=1:3;
         prompt=fprintf(1,'Enter placement R%s of F%d',dim(j),i' );
         R(i,j)=input(':');  
     end
     disp('------------------------------')
 end
 
 Mf=[0 0 0]; %code for calculating moment due to moved forces
 for i=1:nf;
     Mf=Mf+cross(R(i,:),F(i,:));
 end
 
  Mtot=Mm+Mf; %Total System Moment
 clc
 
disp('------------------------------')
     disp('EQUIVALENT SYSTEM AT SPECIFIED POINT') %display of Equivalent system
     fprintf(1,'\nF = %f*i + %f*j +%f*k\n',Ftot)
     fprintf(1,'|F| = %f\n\n',norm(Ftot))
     fprintf(1,'M = %f*i + %f*j +%f*k\n',Mtot)
     fprintf(1,'|M| = %f\n\n',norm(Ftot))
     
    Wrench=(Mtot*normr(Ftot)')*normr(Ftot); %calculating wrench
     Mp=Mtot-Wrench;%calculating moment perpendicular to Ftotal
 
    syms x y z; %building vector for x,y,z placement equations
     R=[x y z];
    Mperp=cross(R,Ftot);%Mperp=moment perpendicular to force
  disp('------------------------------')
    disp('SIMPLEST EQUIVALENT SYSTEM AT SPECIFIED POINT')%display of simplest system
    fprintf(1,'\nF = %f*i + %f*j +%f*k\n',Ftot)
    fprintf(1,'|F| = %f\n\n',norm(Ftot))
    fprintf(1,'Wrench = %f*i + %f*j +%f*k\n',Wrench)
    fprintf(1,'|Wrench| = %f\n\n',norm(Wrench))
    disp('The following equations define the placement of the simplest equivalent system')
for n=1:3
    fprintf(1,'(%d)  %f=',n,Mp(n))
    disp(Mperp(n))
end
  disp('------------------------------')
 
Dell: Perhaps try X=linsolve(RF,Mtot), or perhaps X=RF\Mtot. Let us know if this doesn't help.
 
in my code, RF is called Mperp,
so...

Code:
RR=Mperp;
X=linsolve(RF,Mtot), 
? Error using ==> linsolve
First and second arguments must be single or double.

didnt work, neither did X=RF\Mtot, but there is something in it, it gives me a matrix with 9 values, some of which seem right but i cannot see how i could use this to get a conclusive answer.

is there no way to use the solve function for the contents of a matrix/vector, once i get my 3 equations and manually use the solve function it works perfectly, for example, if
A=[2*x, x+y, z+y+2*x] and B=[2, 3, 6]
i have gotten to the point where MATLAB displays
2*x=2
x+y=3
z+y+2*x=6
but cannot advance from that, from there i need to manually plug in solve('2*x=2','x+y=3','z+y+2*x=6') to get solutions, but this is not good for my general code, as every time i get 3 different solutions, i would like to be able to solve(A,B) or solve(A=B) or even solve(A(1)=B(1),A(2)=B(2),A(3)=B(3))
 
Well, this is really an ugly brute force approach, but it's what first came to my mind. Using eval, assuming A is symbolic, and B numeric (otherwise you'd need to flip the char and num2str-stuff).

str = [];
for i = 1:length(A)
str = [str ',' char(A(i)) '=', num2str(B(i))];
end
str = str(2:end);
sol = eval(['solve(''' str ''')']);

If you have the relevant toolboxes (and maybe Maple), you could use the maple-command, which makes symbolic stuff real easy.
 
Dell: Try the following after you have computed Ftot and Mtot, and after you have input R(1), R(2), or R(3). In other words, you must give the program either the x, y, or z component of the unknown position vector R. Let ii equal the index number of the R component given by the user; i.e., ii = 1, 2, or 3. Please clean up my syntax, in case I did not use semicolons correctly. Let us know whether or not it works.

% check orthogonality.
if(abs(dot(Ftot,Mtot))>1.0e-6)
...[/color]print "Error: Mtot is not perpendicular to Ftot. Mtot must be ";
...[/color]print "perpendicular to Ftot to be a valid cross product.";
...[/color]exit;
end;

% solve for R.
if(Ftot(ii)~=0.0)
...[/color]U=cross(Ftot,Mtot)/dot(Ftot,Ftot);
...[/color]V=Ftot*(R(ii)-U(ii))/Ftot(ii);
...[/color]R=U+V;
...[/color]print R;
else print "No solution found.";
end;
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K