1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using 'solve' command in MATLAB R2008a

  1. May 9, 2009 #1
    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 (Text):
    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 (Text):
    solve(RF(1)=Mtot(1),RF(2)=Mtot(2),RF(3)=Mtot(3))
    but matlab doesnt 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?
     
  2. jcsd
  3. May 9, 2009 #2
    hopefully you guys understood what im 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 (Text):
    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('------------------------------')

         
         
     
  4. May 9, 2009 #3

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    Dell: Perhaps try X=linsolve(RF,Mtot), or perhaps X=RF\Mtot. Let us know if this doesn't help.
     
  5. May 10, 2009 #4
    in my code, RF is called Mperp,
    so...

    Code (Text):

    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))
     
  6. May 10, 2009 #5

    Päällikkö

    User Avatar
    Homework Helper

    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.
     
  7. May 12, 2009 #6

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    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;
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Using 'solve' command in MATLAB R2008a
  1. Matlab command while (Replies: 4)

Loading...