does 'VPA' changes the type of a data variable ?

5 views (last 30 days)
shirin mhd
shirin mhd on 27 Aug 2022
Answered: Chunru on 27 Aug 2022
Hi everyone
I have a question about the data type.
I know that all data are double floating-point in Matlab by default.
I've written this code and when I use 'whos function' for 'z', Matlab says that z is 'double' which is what I expect.
s=500;
t=[12 25 36 12 1 8 9 20 9 7 5 2 74];
z=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:10
z=z+t(i);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z=s-z;
whos function:
whos z
Name Size Bytes Class Attributes
z 1x1 8 double
I've added one line (line 8) and used VPA, when I repeat what I've said above, Matlab says that z is 'syms'.
s=500;
t=[12 25 36 12 1 8 9 20 9 7 5 2 74];
z=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:10
z=z+t(i);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z=vpa(z,10);
z=s-z;
whos function:
whos z
Name Size Bytes Class Attributes
z 1x1 112 sym
does VPA change the data type?
I really appreciate any explanation for this change and totally about syms.

Answers (1)

Chunru
Chunru on 27 Aug 2022
vpa(x) uses variable-precision floating-point arithmetic (VPA) to evaluate each element of the symbolic input x.
The input to x is supposed to be symbolic.
syms x
p = sym(pi);
piVpa = vpa(p)
piVpa = 
3.1415926535897932384626433832795
class(p) % The type is sym
ans = 'sym'
a = sym(1/3);
f = a*sin(2*p*x);
fVpa = vpa(f)
fVpa = 

Community Treasure Hunt

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

Start Hunting!