Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab solve function in for loops

  1. Nov 29, 2011 #1

    NJP

    User Avatar

    Matlab "solve" function in "for" loops

    I need to solve a similar problem like shown in the below codes for a larger matrix,
    The 'x' here needs to be solved for each y(a,b). Each of this y(a,b) is equal to zero and 'x' will vary accordingly , so it will give a 2by 2 matrix for 'x' as well.

    syms x;
    y = [sin(x)+5 3*cos(x)-4
    cos(x)-1 cos(x)-0.6];

    C=zeros(2,2);
    for a=1:2
    for b=1:2

    B(a,b)= solve('y(a,b)=0',x);
    %B(a,b)=solve('y(a,b)=C(a,b)',x);
    end
    end


    When I try to solve it without the loop it gives the following error,
    (Warning: 4 equations in 1 variables.
    Warning: Explicit solution could not be found.
    > In solve at 81)

    When I try with the loop it gives me the following error,
    ??? Error using ==> mupadmex
    Error in MuPAD command: Invalid index
    • ;

      during evaluation of 'matchNonSingletonLHS'

      Error in ==> sym.sym>sym.subsasgn at 1420
      C = mupadmex('mllib::subsasgn',A.s,B.s,inds{:});



    • It will be great if someone can give me a clue to solve this problem.
     
  2. jcsd
  3. Nov 30, 2011 #2

    f95toli

    User Avatar
    Science Advisor
    Gold Member

    Re: Matlab "solve" function in "for" loops

    I don't often use the symbolic toolbox, but I think the issue is that you've defined y to be a 2x2 matrix of one variable (x).
    I am not even sure what you think y(a,b) would mean in this case?
     
  4. Nov 30, 2011 #3

    NJP

    User Avatar

    Re: Matlab "solve" function in "for" loops

    Thanks for the reply. Like I said y here is just a random matrix. My actual problem needs to be solved for voltage angle difference between busbars and contains a 96by10 matrix. However the the matrix I need to solve is quite similar to 'y'. But each element contains larger first order trigonometric equations.
     
  5. Nov 30, 2011 #4
    Re: Matlab "solve" function in "for" loops

    Try this:

    syms x,y,z,w;
    y = [sin(x)+5 3*cos(y)-4
    cos(z)-1 cos(w)-0.6];

    C=zeros(2,2);
    for a=1:2
    for b=1:2

    B(a,b)= solve('y(a,b)=0',x);
    B(a,b)= solve('y(a,b)=0',y);
    B(a,b)= solve('y(a,b)=0',z);
    B(a,b)= solve('y(a,b)=0',w);
    end
    end
     
  6. Nov 30, 2011 #5

    NJP

    User Avatar

    Re: Matlab "solve" function in "for" loops

    It gives me the below errors,
    :(

    ?? Error using ==> mupadmex
    Error in MuPAD command: Invalid index
    • ;

      during evaluation of 'matchNonSingletonLHS'

      Error in ==> sym.sym>sym.subsasgn at 1420
      C = mupadmex('mllib::subsasgn',A.s,B.s,inds{:});

      Error in ==> Untitled2 at 9
      B(a,b)= solve('y(a,b)=0',x);
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab solve function in for loops
  1. For loop in matlab (Replies: 3)

Loading...