Why do I get a fraction instead of a rational number when I solve two simple simultaneous equations
    25 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Keith Jones
 le 2 Oct 2016
  
    
    
    
    
    Modifié(e) : Walter Roberson
      
      
 le 21 Déc 2020
            These are the two equations I need to solve; for A and B, to calculate the viscosity of hydraulic fluid between to temperatures. The results are correct, but why does Matlab show them as fractions and not rational numbers?
syms A B
eqn1=A-520*B==3.7612;
eqn3=A-560*B==2.3026;
[X,Y] = equationsToMatrix([eqn1, eqn3], [A, B])
Z = linsolve(X,Y)
Z =
    22723/1000
   7293/200000
>> 22723/1000
ans =
     22.7230
>>  7293/200000
ans =
      0.0365
0 commentaires
Réponse acceptée
  John D'Errico
      
      
 le 2 Oct 2016
        Because you are (for some irrational reason) using symbolic computations to solve a simple linear system of equations. Sort of equivalent to the use of a Mack truck to bring a single pea to Boston - massive overkill. Therefore the result is a symbolic one, and is left in symbolic form.
If you want to convert it to a floating point number, then use vpa, or use double.
3 commentaires
  Steven Lord
    
      
 le 20 Déc 2020
				If the expressions contain symbolic variables use vpa to display the approximation of the symbolic result to a specified number of decimal places.
If the expressions are symbolic representations of numbers (no symbolic variables) use double.
sqrt5 = sqrt(sym(5))
double(sqrt5)
syms x
xsqrt5 = x*sqrt5
vpa(xsqrt5)
Calling double on xsqrt5 won't work because of the presence of the x in the expression.
double(xsqrt5)
Plus de réponses (1)
  Walter Roberson
      
      
 le 21 Déc 2020
        
      Modifié(e) : Walter Roberson
      
      
 le 21 Déc 2020
  
      Use sympref to set symbolic output to floating-point.
Voir également
Catégories
				En savoir plus sur Calculus dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!