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

조회 수: 3 (최근 30일)
shirin mhd
shirin mhd 2022년 8월 27일
답변: Chunru 2022년 8월 27일
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.

답변 (1개)

Chunru
Chunru 2022년 8월 27일
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 = 

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by