The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Matlab question -- UPDATE2 yet another question, 12:30pm

urahonkyurahonky Cynical Old ManRegistered User regular
edited September 2010 in Help / Advice Forum
Hi guys,

I'm working with Matlab and I can't seem to get something right here. Basically I need to create a vector that starts at 0, and then goes to n (where n is a number I enter, which I can do easily). But on each one of the values in this large vector I need to run an equation against it, and then sum up all the values. If that makes sense. Here's what I've got:

>>n = [0:100];
This creates the vector from 0-> 100 as the values.

>> S = ((-1)^(i))*(1/(2*i+1));

'i' is supposed to be what each value of 'n' is. But I'm not sure how to do this. The book only explains that if you do something like: n.^4 it raises each value of 'n' to the power of 4. But I don't know how to take an equation and apply it to each value in the vector. I hope it makes more sense now.

Does anyone have any insight on how to do this?

e: QUESTION 2:

Any idea why my script:

function V = Vfuel(h)
r=0.6; %converted to meters to keep the units the same
SV = ((4/3)*(pi)*(r^3))/2; %sphere's total volume
disp(r)
disp(h)
disp(h+r)

When I enter 0.2 as my h, gives me:

>> number24 0.2
0.6000

0.2
48.6000 46.6000 50.6000

It's like it treats the r=0.6 as a vector.


QUESTION 3:

So I wrote a user function:
function V = Vfuel(h)
r = 0.6; %converted to meters to keep the units the same
SV = ((4/3)*(pi)*(r^3))/2; %sphere's total volume
if (r > h) %if it's just the sphere
    V = SV - ((pi*(h^2)*((3*r) - h))/3);
elseif (r < h) %above the sphere
    V = SV;
    cylH = h-r;
    if (cylH <= 1.4) %doesn't fill the entire cylinder
        V = V + (pi*(r^2)*cylH);
    elseif (cylH > 1.4)
        V = V + (pi*(r^2)*cylH);
        coneH = cylH - 1.4;
        V = V + (1/3)*pi*(r^2)*coneH;
    end
end

Now I need to run this against h, from 0 -> 2.8, and then plot it. But I can't seem to figure out how to use h against it. I defined h as: h = (0:0.1:2.8), and then tried to do: S = number24 (h), and put the dot in various places, but it still didn't go. If I use just 0.2 and 1.8 the numbers go in the function just fine.

The error I get is:

??? One or more output arguments not assigned during call to 'C:\MATLAB7\work\number24.m (number24)'.

urahonky on

Posts

  • BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited September 2010
    I can't remember if the shorthand handles scalar^vector type stuff. When in doubt, use loops.

    Let's call n the max number and N the vector.
    Define s=0 to hold the sum.
    Set up a loop to run from i=1 to n+1. Matlab stupidly starts indexing at 1.
    Inside the loop do s=s+((-1)^N(i))*(1/(2*N(i)+1));

    BoomShake on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    That's exactly how I did it before, and got the correct answer. But the problem is that at the end of the problem in the book it says to do it the way I said in the OP. :(

    Which annoys me because the book doesn't tell you AT ALL on how to do something like this, and Google searching this situation is an exercise in frustration as well.

    urahonky on
  • BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited September 2010
    Ah, gotcha. Lemme load up Matlab and try some things.

    BoomShake on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Don't spend more than a few minutes please... I don't want to waste someone else's time for something like this. But I do appreciate it. :)

    urahonky on
  • BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited September 2010
    Awesome, it works just like I thought.
    >>n=[0:100];
    >>s=sum(((-1).^n).*(1./(2*n+1)))
    
    s =
    
        0.7879
    

    There are three places to put the period: before the exponential, before the multiplication in the center, and before the division.

    1. The first one tells it to raise -1 to each element of n, generating a new vector.
    2. The division one is similar, telling it to divide 1 by each element of the 2n+1 vector. The 2n+1 gets handled without special treatment.
    4. Since the first quantity and second quantity are both vectors, we need to use the .* to multiply corresponding elements.

    For simplicity, we can run the sum() on the resulting vector in the same line.

    Also, fuck MATLAB and all involved in its creation.

    BoomShake on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Oh my God... I would never have thought to put the period right there! I seriously put it everywhere just to see if it changed anything. But there threw me off.

    Also I agree. Matlab is terrible and so is this class for making me use it.

    Thank you so, so, so much. I've been wracking my brain on this problem for hours.

    urahonky on
  • DemerdarDemerdar Registered User regular
    edited September 2010
    Pretty much hit it spot on. Just remember that the "." operator allows to treat each element of your vector as a scalar and to do that command on it. Remember that A^2 /= A.^2 if A is a Tensor (be it 1st order, 2nd order, or nth order).

    A^2 does the linear algebra operation of A*A while A.^2 just squares every element in A.

    Also, don't dog on MATLAB. It has some great built-in graphing tools and its ability to create vectors and matrices is really powerful.

    Demerdar on
    y6GGs3o.gif
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Updated OP with another question... Sorry to bother you guys again, but I can't understand why my .m file is doing this....

    urahonky on
  • DemerdarDemerdar Registered User regular
    edited September 2010
    urahonky wrote: »

    e: QUESTION 2:

    Any idea why my script:

    function V = Vfuel(h)
    r=0.6; %converted to meters to keep the units the same
    SV = ((4/3)*(pi)*(r^3))/2; %sphere's total volume
    disp(r)
    disp(h)
    disp(h+r)

    When I enter 0.2 as my h, gives me:

    >> number24 0.2
    0.6000

    0.2
    48.6000 46.6000 50.6000

    It's like it treats the r=0.6 as a vector.

    I'm not sure what you are trying to accomplish here. In the function you call a variable "h" into it yet you don't use it to calculate SV.

    What is h supposed to be? Also, I don't see any vectors in this script. Tell me what this function is supposed to be doing and maybe I can give you a little bit more advice.

    Demerdar on
    y6GGs3o.gif
  • ecco the dolphinecco the dolphin Registered User regular
    edited September 2010
    I'm guessing it's because you want

    number24(0.2) <- see the parenthesis?

    Difference between "command" and "function" syntax.

    Edit: The specific action that you're seeing is example 7 under "Common Mistakes"

    The 0.2 is being changed into a vector:

    In quasi-C

    [ '0' '.' '2' ]

    which numerically is

    [ 48 46 50 ]

    The disp(h) recognises the original data type, and converts back to the string "0.2", but h+r adds 0.6 to the numeric vector, which is the result you're seeing.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Sorry guys I went to bed shortly after posting that. eeccccoo was right, as usual, I wasn't putting the thing in parenthesis. The code I posted was just a snippet of what I was supposed to be doing. Sorry for the confusion. And thank you eeccccoo. :)

    urahonky on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    This is the last matlab question I'll have. If you can help me out that would be great!

    urahonky on
  • DaenrisDaenris Registered User regular
    edited September 2010
    urahonky wrote: »
    This is the last matlab question I'll have. If you can help me out that would be great!

    You can't use the . notation to do scalar operations with functions -- as far as I know it only works on many basic math operators. So you'll need to loop, or else write your function to deal with a vector input and provide a vector of output (essentially just do the loop inside your function).

    Daenris on
  • BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited September 2010
    Question 3:
    arrayfun()

    Remember, make sure your function is defined in it's own functionname.m file, it's in the work path, and you use @functionname for the function handle in arrayfun()

    BoomShake on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Oohhh yeah that makes sense. I'll try the for loop but I think it wants me to do something else..?

    Boom, if I've got a number23.m file, and inside that is my script I have up above... How do I use arrayfun?

    I tried S = arrayfun(number23,h) but obviously that's wrong. You say to use the @functionname... So is it... S=arrayfun(number23,@Vfuel,h)?

    urahonky on
  • BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited September 2010
    When you're defining a standalone function, the filename has to be the exact same as the function name

    The function name you're using is Vfuel. Your file must be Vfuel.m

    Then you'd do S = arrayfun(@Vfuel,h);

    I honestly have no idea where this number23 and number24 stuff is coming from given the code you've posted.

    BoomShake on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Oh I'm sorry. That's the problem number in the book. Helps me keep track of the stuff I do. I'll try what you suggested right now.

    e: Yeah see every time I try that I get the following error:

    >> S = arrayfun(@Vfuel,h)
    ??? Undefined command/function 'arrayfun'.

    Something about the @ symbol does that.

    urahonky on
  • BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited September 2010
    urahonky wrote: »
    >> S = arrayfun(@Vfuel,h)
    ??? Undefined command/function 'arrayfun'.

    Something about the @ symbol does that.

    No, it's saying that arrayfun() itself isn't defined. A quick bit of googling turned up:
    "ARRAYFUN was introduced in MATLAB 7.1 (R14SP3). If you have an older
    version, you will not be able to use this function."

    BoomShake on
  • DaenrisDaenris Registered User regular
    edited September 2010
    urahonky wrote: »
    Oh I'm sorry. That's the problem number in the book. Helps me keep track of the stuff I do. I'll try what you suggested right now.

    e: Yeah see every time I try that I get the following error:

    >> S = arrayfun(@Vfuel,h)
    ??? Undefined command/function 'arrayfun'.

    Something about the @ symbol does that.

    What do you get if you do "help arrayfun"?

    The undefined command thing suggests it's not finding arrayfun, but it should be. When I try it with your code it works fine.

    Daenris on
  • urahonkyurahonky Cynical Old Man Registered User regular
    edited September 2010
    Yep that's what it was. Dang it. Alright thanks guys. (Working at the computer lab at the university now)

    urahonky on
Sign In or Register to comment.