Matlab - Use Decimals in Symbolic Expression - matlab

In this simple example, using the function vpa in a symbolic expression one can convert the outer fraction into decimal:
>> syms x
>> diff(2*x^(3.7))
(37*x^(27/10))/5
>> vpa(diff(2*x^(3.7)))
7.4*x^(27/10)
But the result still preserves a fraction in the exponent.
How could force Matlab to use decimals whenever possible?

Only for MATLAB 2019a and later:
>> sympref('FloatingPointOutput',true);
>> diff(2*x^(3.7))
ans =
7.4000*x^2.7000
And to return to the defaults:
>> sympref('FloatingPointOutput','default');
>> diff(2*x^(3.7))
ans =
(37*x^(27/10))/5
documentation

Related

Compare symbolic variables

I am trying to compare two symbolic variables (numbers). The whole problem boils down to the following code.
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))
Both comparisons should return 1, but the second returns 0.
My solution:
digits(51);
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))
Why you encounter this problem
vpa evaluates symbolic inputs with variable-precision floating-point arithmetic (32 significant digits by default)... So what's happening in your case is
>> R = vpa(0.555555555555555555555555555)
R =
0.55555555555555555555555555555556
>> R*(1-sym(10^(-50)))
ans =
0.55555555555555555555555555555556
32 digits are definitely not enough to store the actual value of 1-10^(-50).
How to fix it
Without stressing with vpa() you can declare both R and R * (1 - 10^(-50)) as symbolics (in fact 0.5555555... = 5/9), and compare them:
>> R = str2sym('5/9');
>> X = str2sym('5/9 * (1 - 10^(-50))');
>> isAlways(R > X)
ans =
logical
1

Can I represent a rational symbolic value like a decimal approximation?

For example, I have the symbolic value which is 's+5/2'. Is there a way to display it like 's+2.5'?
Yes, use vpa. Simply take your symbolic expression and use the vpa function to facilitate the conversion. vpa evaluates each term in the symbolic expression and converts each value to using up to 32 significant digits whenever possible. You can also override the amount of significant digits with the second parameter to vpa, but that's not needed in your case.
Here's a quick example:
>> syms s
>> A = s + 5/2
A =
s + 5/2
>> vpa(A)
ans =
s + 2.5
You can also set the initial approximation mode of the numeric literal to decimal mode, where the precision is dictated by digits, using the 'd' flag of the sym function:
>> expr = sym('s') + sym(5/2,'d')
expr =
s + 2.5

Is there any way to increase 'realmax' in MATLAB?

realmax on my machine is:
1.7977e+308
I know I have to write my code in a way to avoid long integer calculations, but is there any way to increase the limit?
I mean something like gmp library in C
You may find vpa (variable- precision arithmetic) helpful:
R = vpa(A) uses variable-precision arithmetic (VPA) to compute each element of A to at least d decimal digits of accuracy, where d is the current setting of digits.
R = vpa(A,d) uses at least d significant (nonzero) digits, instead of the current setting of digits.
Here's an example how to use it:
>> x = vpa('10^500/20')
ans =
5.0e498
Note that:
The output x is of symbolic (sym) type. Of course, you shouldn't convert it to double, because it would exceed realmax:
>> double(x)
ans =
Inf
Use string input in order to avoid evaluating large input values as double. For example, this doesn't work
>> vpa(10^500/20)
ans =
Inf
because 10^500 is evaluated as double, giving inf, and then is used as an input to vpa.

Displaying rational numbers in Matlab

I have two integer numbers m,n which together form a rational number in the form of m/n. Now I just want to display them in Matlab in this rational form.
I can do this by doing
char(sym(m/n))
So, if, e.g. m = 1, n = 2, Matlab will display 1/2. However, if m = 2, n = 4, I am also getting 1/2, whereas I want to get 2/4.
Any way of doing this without recurring to something like
fprintf( '%d/%d', m, n )
Thanks
You can change the display format to rat
>> format rat
>> 2/3
ans =
2/3
otherwise you can call rats function
>> rats(2/3)
ans =
2/3
>> class(ans)
ans =
char
However, in both cases the fractions will be reduced. To avoid that you should create your separate function or introduce it as an anonymous function
>> rat2 = #(m,n) num2str([m n], '%d/%d')
rat2 =
#(m,n)num2str([m,n],'%d/%d')
>> rat2(2,4)
ans =
2/4

undefined result during formatting in hexadecimal form

I can't understand what is reason of following result
>> format hex
>> 10
ans =
4024000000000000
>> 12
ans =
4028000000000000
as i know this numbers should be written in hexadecimal format,but why such result?i have tried different variant for example like this
>> x=20;
>> format hex
>> x
x =
4034000000000000
if i will try different format
>> format long
>> x=10
x =
10
>> x=10.456
x =
10.456000000000000
it works nice, so what is problem?
Matlab is behaving absolutely correct, x=12 creates a 64 bit floating point number witch has the presented hexadecimal representation. What you probably want is:
>>uint32(12)
ans =
0000000c