How to get decimal answers in Matlab?

Thread Starter

farmosh203

Joined Nov 26, 2006
20
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.
 

Dave

Joined Nov 17, 2003
6,970
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
 

Thread Starter

farmosh203

Joined Nov 26, 2006
20
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.
 

Dave

Joined Nov 17, 2003
6,970
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.
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
 

Thread Starter

farmosh203

Joined Nov 26, 2006
20
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.
 

titanp321

Joined Jan 30, 2008
1
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)
 

thefoxer

Joined Jun 6, 2009
1
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
 
Top