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

Matlab question!

  1. Jan 9, 2007 #1
    I need some help with matlab! not sure which function to use!

    I think i should use xcount ycount so i can do it but i shall explain the problem!

    I have input some variables in to matlab

    p2=1e5
    t2=288
    p3=[2e5:1e4:10e5]
    y=1.4

    and now i wish to work out
    t3is which should be:

    t3is=t2*((p3/p2)^(y-1/y))

    but that cant be done because you cant ^ a matrix!!

    So could i use xcount to do it or what is the best way!

    Thanks in advance!

    Matlab Noob:confused:
     
  2. jcsd
  3. Jan 9, 2007 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You can use the function 'arrayfun' to apply a function to each element of an array.

    How about code like this?

    Code (Text):

    p2=1e5
    t2=288
    p3=[2e5:1e4:10e5]
    y=1.4

    t3is = p3/p2
    arrayfun(@(x) x^(y - 1/y), t3is)
    t3is = t3is * t2;
     
    The notation @(x) defines a new anonymous function that acts on x. In this case, all it does is raise x to the (y - 1/y) power.

    You can, of course, combine all three of the last lines into one, if you prefer:

    Code (Text):

    t3is = t2 * arrayfun(@(x) x^(y - 1/y), p3/p2)
     
    - Warren
     
  4. Jan 22, 2007 #3
    Alternatively you can vectorise all variables Since I have not used arrayfun before I do not know which is faster.

    Code (Text):
    p3=[2e5:1e4:10e5];
    p2=1e5*ones(size(p3));
    t2=288*ones(size(p3));
    y=1.4*ones(size(p3));
    t3is=t2.*((p3./p2).^(y-ones(size(p3))./y));
    The dots in front of the operators perform element by element operations between matrices of the same size. You don't need it for + or -. And I'm pretty sure you've found out this by now: putting semi-colons behind each line suppresses the outputs, keeping your command window neat and managable.
     
  5. Jan 25, 2007 #4
    You can simply do this...

    t2*((p3/p2).^(y-1/y))

    if you want each element of (p3/p2) raised to the power (y-1/y).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Matlab question!
  1. MATLAB questions (Replies: 0)

  2. MATLAB question (Replies: 2)

  3. MATLAB question (Replies: 1)

  4. Matlab question (Replies: 2)

  5. Matlab question (Replies: 10)

Loading...