MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Maria Coronell
on 20 May 2013

Hi, there.. I'm solving simple ecuation in matlab but when i run the programe the answer is displaying in fractional numbers. How can I change it to display decimal number?

*No products are associated with this question.*

Answer by Walter Roberson
on 21 May 2013

It looks to me like you have calculated values using the symbolic toolbox. To display those values in floating point, use double() on them. For example,

double(Nu2)

(If we had known you were using the symbolic toolbox we could have answered much earlier.)

Maria Coronell
on 21 May 2013

Thanks, it works.. but before, I didnt have to put any command to have my decimal numbers, thats what i want. I don't know if the problem is that i have to change some toolbox or what.. but if you know how to do it please, I'll appreciate.

Walter Roberson
on 21 May 2013

In most cases, the Symbolic Toolbox will convert formulae involving floating point numbers into their rational fractions.

When the Symbolic Toolbox is working with a rational formula, it will attempt to find a closed-form solution, but if it cannot do so then (if meaningful) it will attempt to find a fixed-point numeric solution instead.

When the Symbolic Toolbox recognizes that it is working with a fixed-point formula (rather than rational) then it will skip trying to find a closed-form solution and will proceed with trying to find a fixed-point numeric solution.

Because of the above behavior, two formulae that are the same except for a single decimal point in one of them, might be calculated different ways and can potentially arrive at very different answers; for example a formula with a complex term might have the term cancel out to 0 when calculated in full rational precision, but might have a large non-zero complex term when calculated in fixed-point.

For example, at the MuPAD command line,

int(x^2+1, x=0..10^10)

would proceed by closed-form, but if the "1" was replaced by "1." then it would do a numeric calculation.

Now, if you have a symbolic term that is in rational form and you add a constant, then if the constant is in rational form the addition would b done in rational form; if, however, the constant is in fixed-point form then the rational symbolic term will be evaluated down to a fixed-point number before the addition is done.

1 + RATIONALTERM gives a rational answer 1. + RATIONALTERM gives a fixed-point answer

Possibly your code path has changed by as little as one character in a constant.

Answer by Youssef KHMOU
on 20 May 2013

hi, Maria,

If you want to display decimal ( floating point) numbers try :

>>format long % or format short

If you want fractional display try :

>>format rat

and try :

>>doc format

Walter Roberson
on 20 May 2013

If you just enter a number at the command line, how does it come out? For example if you entered

2.345

then does it come out in decimal or does it come out in fraction form?

I am trying to determine here whether you MATLAB is set to display fractions automatically or if instead the program is designed to display as fractions deliberately.

Maria Coronell
on 21 May 2013

Yes, it show 2.345.. the thing is, that some values are showing as fraction and other values just in decimal form. I dont understand what happened! Look at this!

Re1 =

7.489184809171960e+004

Nu1 =

3.473689192970496e+002

h1 =

1.047513915597188e+004

Re2 =

1422543653887999360776996931534847612609041143314926486102736896/18415266480134414561195254170806684536779318144366885369925

Nu2 =

(70353137442027663*18415266480134414561195254170806684536779318144366885369925^(9/20)*1422543653887999360776996931534847612609041143314926486102736896^(11/20))/2073374681446543372437154885376994391455329959260590683959033514499768320000

Answer by Shashank Prasanna
on 20 May 2013

>> format

Resets MATLAB display format to default.

## 2 Comments

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76343#comment_149808

Please give examples. Do you mean like it's showing 1/4 and you want it to show 0.250000, or showing 5/2 and you want it so show 2.50000?

## Maria Coronell (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76343#comment_150007

thats what im saying, I want decimal numbers for my answers. Thanks