How do I convert fractional display to decimal display ?

616 visualizaciones (últimos 30 días)
Ibrahim A
Ibrahim A el 22 de Mzo. de 2021
Editada: DGM el 13 de Oct. de 2024
I am getting bunch of fractional values in my output matrix and I have other matrixes outputting the same way as this one. I cannot find a way to change it to decimal notation, something like 1.678.

Respuesta aceptada

Steven Lord
Steven Lord el 22 de Mzo. de 2021
Call double or vpa on your symbolic variable.
  6 comentarios
Ibrahim A
Ibrahim A el 22 de Mzo. de 2021
I use this function to find the integral of 1, I think this is what you mentioned above. Is this the problem?
j1(t) = (j_max * (t.^0))
qdd(t) = vpa(int(j1,t))
Steven Lord
Steven Lord el 22 de Mzo. de 2021
two = sym(2);
sqrt2 = sqrt(two)
sqrt2 = 
V = vpa(sqrt2)
V = 
1.4142135623730950488016887242097
D = double(sqrt2)
D = 1.4142
whos two sqrt2 V D
Name Size Bytes Class Attributes D 1x1 8 double V 1x1 8 sym sqrt2 1x1 8 sym two 1x1 8 sym
V, sqrt2, and two are all sym. So in the code below the line assigning to f(t) creates a symbolic function:
syms t
f(t) = V
f(t) = 
2.6651441426902251886502972498731
D is a double. In the code below assigning to g(t) attempts to store D in element t of the array g.
g(t) = D
Error using sym/subsindex (line 864)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
Compare:
qdd(t) = V % works
qdd(t) = D % errors

Iniciar sesión para comentar.

Más respuestas (2)

Bjorn Gustavsson
Bjorn Gustavsson el 22 de Mzo. de 2021
You might have set format to rat somewhere, perhaps in a startup.m or the like. You could get back to more normal decimal output by something like this:
format short g
For additional options check the help and documentation to format.
HTH
  1 comentario
Ibrahim A
Ibrahim A el 22 de Mzo. de 2021
Unfortunately, didn't work. I also tried to change the format on preferences -> matlab -> command window -> numeric format (Short g, compact), that also didn't work. I should also mention that I am using symbolic math toolbox, I don't know if it is related or not. But thank you for your answer.

Iniciar sesión para comentar.


Dalton
Dalton el 13 de Oct. de 2024
Editada: DGM el 13 de Oct. de 2024
rats('your value')
example:
rats(1.6666667)
ans = ' 5/3 '

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by