Results 1 to 3 of 3

Math Help - MATLAB translation

  1. #1
    Junior Member
    Joined
    Oct 2008
    Posts
    39

    MATLAB translation

    I was given the MATLAB function

    function y=dig(x,D)
    ex = D - ceil(log10(abs(x)));
    y = round(x.*(10.^ex)).*(10.^(-ex));


    I need to know exactly what this does. I know it takes in a vector x and returns D significant decimals, but I don't know how it gets the sig digits...I do not have access to MATLAB.

    Thanks
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Nov 2008
    Posts
    461
    Hi!

    I'm sorry but I can't give you the answer - because I don't understand it.

    Quote Originally Posted by CarmineCortez View Post
    I was given the MATLAB function

    function y=dig(x,D)
    ex = D - ceil(log10(abs(x)));
    y = round(x.*(10.^ex)).*(10.^(-ex));


    I need to know exactly what this does. I know it takes in a vector x and returns D significant decimals,
    That sounds good and makes sense.

    Quote Originally Posted by CarmineCortez View Post
    but I don't know how it gets the sig digits...I do not have access to MATLAB.
    So theoretically you are able to understand the algorithm for that problem, I don't..., but you have problems with the syntax? Is that it?

    function y=dig(x,D)

    This is like you said. x is a vector, D is the significant decimals and y is also a vector.

    ex = D - ceil(log10(abs(x)));

    ex is a vector, too. D is an integer like 10. And x is a vector [5,3,-1]

    abs(x) leads to the vector [|5|,|3|,|-1| ] = [5,3,1]

    log10(x) -> you get the vector [log(5),log(3),log(1)]

    So what does ceil do? For this syntax you should use google or go to the official website of matlab, because there is a good documentation.

    You find this:
    MATLAB

    In my opinion it explains it very good what ceil does, because there is a nice example.

    It is D = 10 for example and you solve ceil(...).
    For example you have 10 - [3,5,7] then matlab returns the vector [10-3, 10-5, 10-7]

    I don't know what happens in the next line if the vector has a negative number in it.

    y = round(x.*(10.^ex)).*(10.^(-ex));

    Use the Matlab documentation I showed you to find out how round() works.

    The critical thing here is the point ' . ' You need it if you calculate * or / or ^ or... then you get this: You have two vectors [x_1,...,x_n] and ex_1,...ex_n

    round(x.*(10.^ex)).*(10.^(-ex));
    = round([x_1*10^ex_1)*10^(-ex_1),...,[x_n*10^ex_n)*10^(-ex_n) ])

    But this could be wrong. I don't have matlab either. I just think this is what Matlab does. If not, I'm sorry, but no one hasn't responded yet, so I thought it might help.

    Kind regards
    Rapha
    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 CarmineCortez View Post
    I was given the MATLAB function

    function y=dig(x,D)
    ex = D - ceil(log10(abs(x)));
    y = round(x.*(10.^ex)).*(10.^(-ex));


    I need to know exactly what this does. I know it takes in a vector x and returns D significant decimals, but I don't know how it gets the sig digits...I do not have access to MATLAB.

    Thanks
    There are a number of issues here.

    The first is the vectorisation, the above is a vector version of the scalar code:

    Code:
    function y=dig(x,D)
      ex = D - ceil(log10(abs(x)));
      y = round(x*(10^ex))*(10^(-ex));
    where the function ceil(u) returns the smallest integer greater than u.

    ^ denotes raising to a power

    round(u) rounds u to the nearest integer.

    Lets take x=1234.56789, and D=2

    log10(abs(x))=3.09151..
    ceil(log10(abs(x))=4
    ex=2-4=-2
    y=round(x*10^-2)*10^2=round(12.3456789)*10^2=1200

    It reduces x to 2-significant digits

    If you want to try the code there is a free Matlab clone here

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Translation
    Posted in the Algebra Forum
    Replies: 3
    Last Post: May 21st 2010, 01:37 PM
  2. translation
    Posted in the Geometry Forum
    Replies: 1
    Last Post: January 28th 2009, 10:52 AM
  3. translation
    Posted in the Geometry Forum
    Replies: 1
    Last Post: January 28th 2009, 08:48 AM
  4. Translation of the cos-1
    Posted in the Trigonometry Forum
    Replies: 5
    Last Post: April 9th 2008, 05:25 PM
  5. translation
    Posted in the Pre-Calculus Forum
    Replies: 1
    Last Post: November 15th 2007, 01:57 PM

Search Tags


/mathhelpforum @mathhelpforum