Using 'solve' command in MATLAB R2008a

  • Thread starter Dell
  • Start date
  • Tags
    Matlab
In summary: R(ii) component given by the user ";...print "caused division by zero.";end;In summary, the conversation discusses a code written for vector mechanics that solves an equivalent system by finding the placement of Vector Ftot. The code uses the solve function to find three equations for x, y, z but encounters errors when trying to use the function with a matrix. The experts suggest using linsolve or eval to solve for the placement of R.
  • #1
Dell
590
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
  • #2
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 independant, 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('------------------------------')
 
  • #3
Dell: Perhaps try X=linsolve(RF,Mtot), or perhaps X=RF\Mtot. Let us know if this doesn't help.
 
  • #4
in my code, RF is called Mperp,
so...

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

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))
 
  • #5
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.
 
  • #6
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)
...print "Error: Mtot is not perpendicular to Ftot. Mtot must be ";
...print "perpendicular to Ftot to be a valid cross product.";
...exit;
end;

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

FAQ: Using 'solve' command in MATLAB R2008a

What is the purpose of the 'solve' command in MATLAB R2008a?

The 'solve' command in MATLAB R2008a is used to solve a system of linear or nonlinear equations. It is particularly useful for solving equations with multiple variables and finding the values of those variables that satisfy the equations.

What are the syntax and inputs for the 'solve' command?

The syntax for the 'solve' command is:
solve(equations, variables)
The equations can be written in either symbolic form or as strings. The variables are the unknowns that you want to solve for.

Can the 'solve' command handle complex equations?

Yes, the 'solve' command is capable of handling complex equations. It can solve equations with complex numbers and variables.

What happens if there is no solution to the equations given in the 'solve' command?

If there is no solution to the equations, MATLAB will return an empty matrix ([]). This indicates that there is no valid solution to the system of equations.

Are there any limitations to using the 'solve' command?

Yes, the 'solve' command may not be able to solve equations with extremely large or small coefficients, or equations with high degrees of complexity. It is also not recommended for use with systems of equations that have a large number of variables. In these cases, other methods of solving equations may be more suitable.

Similar threads

Back
Top