Results 1 to 3 of 3

Math Help - Matlab matrix in function

  1. #1
    Junior Member
    Joined
    Apr 2008
    Posts
    72

    Matlab matrix in function

    Hey ppl,

    I have a matlab function which calls on another function like so:
    -------------------------------------------------------------------------
    Function 1:

    function E = solve_it(m,t,i)

    %where m is a 2 column matrix with 1 or more rows(amount is of the users choosing)

    r=m(:,1);
    s=m(:,2);
    A=r./s;
    invE=A+1/t+1/i;
    E=1/invE;

    -------------------------------------------------------------------------
    Function 2:

    function B = solve_them(m,f,g,t,i,t1,t2)

    E=solve_it(m,t,i);
    B=E*f*g*(t1-t2);

    -------------------------------------------------------------------------
    For the following inputs into B i get the correct answers:

    >> B = solve_them([2 4],4,5,2,3,7,2)

    B =

    75

    >> B = solve_them([3 5],4,5,2,3,7,2)

    B =

    69.7674

    ------------------------------------------------------------------------
    Yet putting in both as 1 matrix i get 1 correct and 1 always at 0

    >> B = solve_them([2 4;3 5],4,5,2,3,7,2)

    B =

    0 69.7674

    When the output i would desire is

    75
    69.7674
    -------------------------------------------------------------------------
    Can anyone identify what im doing wrong, im guessing its something to do with me using the m(:,1) and m(:,2) selections.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    You need to evaluate each row of variables in "m" if you intend to use the function the way you have shown. Try something like this:

    Code:
    function E = solve_it(m,t,i)
    
    %where m is a 2 column matrix with 1 or more rows(amount is of the users choosing)
    for j = 1:size(m,1);
        r=m(j,1);
        s=m(j,2);
        A=r./s;
        invE=A+1/t+1/i;
        E(j,1)=1/invE;
    end
    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by deragon999 View Post
    Hey ppl,

    I have a matlab function which calls on another function like so:
    -------------------------------------------------------------------------
    Function 1:
    Code:
     
    function E = solve_it(m,t,i)
     
    %where m is a 2 column matrix with 1 or more rows(amount is of the users choosing)
     
    r=m(:,1);
    s=m(:,2);
    A=r./s;
    invE=A+1/t+1/i;
    E=1/invE;
     
    -------------------------------------------------------------------------
    Function 2:
     
    function B = solve_them(m,f,g,t,i,t1,t2)
     
    E=solve_it(m,t,i);
    B=E*f*g*(t1-t2);
    Set a break point on the line "r=m(:,1);" then single step through to the line "E=1/invE;" checking what the result is at each stage. Is what you observe what you expect?

    In particular what do you expect 1/invE to be when invE is a vector?

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. matlab matrix help
    Posted in the Math Software Forum
    Replies: 3
    Last Post: May 17th 2010, 11:21 PM
  2. Matlab function with vector inputs and matrix outputs
    Posted in the Math Software Forum
    Replies: 3
    Last Post: August 6th 2009, 09:49 PM
  3. Replies: 0
    Last Post: August 5th 2009, 12:51 PM
  4. Matlab ( Matrix )
    Posted in the Math Software Forum
    Replies: 2
    Last Post: April 6th 2009, 12:32 AM
  5. Matlab help ........Matrix
    Posted in the Math Software Forum
    Replies: 0
    Last Post: April 4th 2009, 05:05 AM

Search Tags


/mathhelpforum @mathhelpforum