Results 1 to 4 of 4

Math Help - Matlab function with vector inputs and matrix outputs

  1. #1
    Junior Member
    Joined
    Apr 2008
    Posts
    72

    Matlab function with vector inputs and matrix outputs

    Hi all,

    I need to write a matlab function file (in editor) for a cylinder.
    The input values will be vectors containing the heights and radii of various different cylinders, and the outputs will be surface area, cross sectional area, and volume in a matrix.

    I know how to write a function with singular inputs, but i can't get vector inputs to work.

    Any help would be appreciated.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by deragon999 View Post
    Hi all,

    I need to write a matlab function file (in editor) for a cylinder.
    The input values will be vectors containing the heights and radii of various different cylinders, and the outputs will be surface area, cross sectional area, and volume in a matrix.

    I know how to write a function with singular inputs, but i can't get vector inputs to work.

    Any help would be appreciated.
    Do you want the input to be d=[height, radius] or: d=[height1, radius1;height2, radius2; ...]?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Apr 2008
    Posts
    72
    I want it like this:

    function [surface_area area volume]=cylinder(height,radius)

    So that the following can be typed into matlab command:

    cylinder([height1, height2, height3...],[radius1, radius2, radius3...])

    which will result in an output of:

    surfacearea1 area1 volume1
    surfacearea2 area2 volume2
    surfacearea3 area3 volume3
    etc etc etc

    If the output matrix has to be aligned a different way(my examples rows as columns instead) then thats fine, i just need to be able to get an output that gives all 3 areas/volumes from the given dimensions.

    Thanks for taking some time to look at this though.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by deragon999 View Post
    I want it like this:

    function [surface_area area volume]=cylinder(height,radius)

    So that the following can be typed into matlab command:

    cylinder([height1, height2, height3...],[radius1, radius2, radius3...])

    which will result in an output of:

    surfacearea1 area1 volume1
    surfacearea2 area2 volume2
    surfacearea3 area3 volume3
    etc etc etc

    If the output matrix has to be aligned a different way(my examples rows as columns instead) then thats fine, i just need to be able to get an output that gives all 3 areas/volumes from the given dimensions.

    Thanks for taking some time to look at this though.
    Code:
    function rv=cylinder(height, radius)
       
       % first make sure we have column vectors   
       
       sz1=size(height);
       if sz1(1) ~= 1
          ht=height';
       else
          ht=height;
       end
       
       sz1=size(radius);
       if sz1(1) ~= 1
          rd=radius';
       else
          rd=radius;
       end
    
       sarea=ht.*(2*rd*pi);
       xarea=pi*rd.^2;
       volume=ht.*xarea;
    
       rv=[sarea;xarea;volume]';
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Orthogonal inputs to a function - same expected value?
    Posted in the Discrete Math Forum
    Replies: 0
    Last Post: October 14th 2011, 05:32 AM
  2. Finding a function's coefficients from inputs and target outputs
    Posted in the Advanced Applied Math Forum
    Replies: 8
    Last Post: January 5th 2011, 12:37 PM
  3. Matlab Vector Factorial Function
    Posted in the Math Software Forum
    Replies: 2
    Last Post: January 31st 2010, 07:39 AM
  4. Matlab matrix in function
    Posted in the Math Software Forum
    Replies: 2
    Last Post: October 14th 2009, 11:24 PM
  5. Replies: 0
    Last Post: August 5th 2009, 01:51 PM

Search Tags


/mathhelpforum @mathhelpforum