How to get decimal answers in Matlab?

Discussion in 'Programmer's Corner' started by farmosh203, Dec 4, 2007.

  1. farmosh203

    Thread Starter Member

    Nov 26, 2006
    20
    0
    Hello, I recently tried using Matlab for my homework to solve some matrices but quickly gave up. Matlab was giving all my answers in exact fractions rather than decimal rounding, so my answers to my matrices were over 4 lines long.

    For example, how would I get Matlab to only display this as a decimal? I have the following code:

    >> syms Vin Rg s C0 Rg C1 Rd r0
    >> -1870615141224609/1152921504606846976*Vin*Rg/(1/s/C0+Rg)/(1/s/C1+1/Rd+1/r0)

    ans =

    -1870615141224609/1152921504606846976*Vin*Rg/(1/s/C0+Rg)/(1/s/C1+1/Rd+1/r0)


    I want Matlab to display the fraction "-1870615141224609/1152921504606846976" as a decimal (-.0016) so it looks cleaner. I can't figure out how to do this after searching the internet and using the help feature. If anyone could help it would be greatly appreciated, thanks.
     
  2. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    look up the format keyword

    format long

    There should also be a setting in the Preferences section.
     
  3. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    This isn't the default behaviour of Matlab, I assume this is through the Symbolic Math Toolbox?

    Either way, as nanovate says you can do this through the preferences. From the Matlab main window select: File>Preferences>Command Window - set the Text Display to their default: Numeric Format - short; Numeric display - loose. Apply the settings.

    Dave
     
  4. farmosh203

    Thread Starter Member

    Nov 26, 2006
    20
    0
    Thanks for the replies. I tried changing the numeric format but that didn't seem to work. It seems like whenever I have a variable next to a big fraction, it won't put the fraction in decimal format. I guess you can't change it... will have to use my TI89 to solve this.
     
  5. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    Can I ask, does it put the answer as a fraction if the answer is say 1/4? Or does it give the answer as 0.25

    I'm thinking there may be an overflow issue in Matlab that will force the answer into fractions for long and complex decimal answers. I couldn't imagine this being the case, but its worth eliminating it as a potential cause.

    Let us know, and if need be I'll have a look into it more.

    Dave
     
  6. farmosh203

    Thread Starter Member

    Nov 26, 2006
    20
    0
    Yeah, for example if the answer is 1/4, I can get it in the decimal form .25. It seems like when there is a variable in the answer, such as 1/4*x, then it will never display it as .25*x, which is the form I am trying to get.
     
  7. titanp321

    New Member

    Jan 30, 2008
    1
    0
    farmosh,
    I to had this very big problem plaguing me for some time, but it is so very simple. For your specific case just enter

    double(ans)
     
  8. thefoxer

    New Member

    Jun 6, 2009
    1
    0
    double() only works for expression swithout symbolic toolbox elements!
    in this case take the vpa() -command, which is also recommended by matlab if you try to use double()!!
    greetz
     
Loading...