How to improve the calculation accuracy of Matlab?

Sometimes when it comes to very small value calculation, the calculation accuracy of Matlab would not be enough.
There would be fluctuation in the result.
For instance:
l=4;
l1=l;%Tx Mode
l2=l;%Rx Mode
misalignment = -1:1e-3:1;
result = zeros(1,length(misalignment));
channel_func = @(d) 1./d.*exp(-1j.*2*pi./3e-3.*d);
phi_Tx = (0:8-1).*2.*pi./8;
phi_Rx = phi_Tx;
F_Tx = exp(1j.*l1.*phi_Tx).';
F_Rx = exp(-1j.*l2.*phi_Rx).';
%% For different misalignment, it output different result.
for i=1:length(misalignment)
distance_fun= @(x,y) sqrt(1e4-2*misalignment(i)/1e2.*cos(x)+2*misalignment(i)/1e2*cos(y)-2e-4*cos(x-y));
H = channel_func(distance_fun(phi_Tx,phi_Rx.'));
result(i) = abs(F_Rx.'*H*F_Tx);% The key calculation. How can I improve the accuracy of this matrix multiplication?
end
%% Image
figure(1);
set(0,'defaultfigurecolor','w')
set(gcf,'Position',[100 100 700 600]);
plot(misalignment,abs(result));
grid on;
xlabel('distance/meter');
ylabel('Intensity');
And in theory, this curve should be smooth. I think the fluctuation is caused by accuracy limit of Matlab.
Is there any suggestion? If it's possible, you can modify the code directly.
Any help is appreciated.

6 commentaires

VBBV
VBBV le 11 Avr 2023
Déplacé(e) : VBBV le 11 Avr 2023
you can use, smoothdata on the resulting matrix
l=4;
l1=l;%Tx Mode
l2=l;%Rx Mode
misalignment = -1:1e-3:1;
result = zeros(1,length(misalignment));
channel_func = @(d) 1./d.*exp(-1j.*2*pi./3e-3.*d);
phi_Tx = (0:8-1).*2.*pi./8
phi_Tx = 1×8
0 0.7854 1.5708 2.3562 3.1416 3.9270 4.7124 5.4978
phi_Rx = phi_Tx
phi_Rx = 1×8
0 0.7854 1.5708 2.3562 3.1416 3.9270 4.7124 5.4978
F_Tx = exp(1j.*l1.*phi_Tx).';
F_Rx = exp(-1j.*l2.*phi_Rx).';
%% For different misalignment, it output different result.
for i=1:length(misalignment)
distance_fun= @(x,y) sqrt(1e4-2*misalignment(i)/1e2.*cos(x)+2*misalignment(i)/1e2*cos(y)-2e-4*cos(x-y));
H = channel_func(distance_fun(phi_Tx,phi_Rx.'));
result(i) = abs(F_Rx.'*H*F_Tx);% The key calculation. How can I improve the accuracy of this matrix multiplication?
end
% smooth data with a suitable distribution Guassian
result1 = smoothdata(result,'gaussian',20);
% try with different values
% smooth data with a suitable distribution SGOLAY
result2 = smoothdata(result,'sgolay');
%% Image
figure(1);
set(0,'defaultfigurecolor','w')
set(gcf,'Position',[100 100 700 600]);
plot(misalignment,abs(result1),misalignment,abs(result2));
grid on;
xlabel('distance/meter');
ylabel('Intensity');
legend('Gaussian','Sgolay filter')
祥宇 崔
祥宇 崔 le 11 Avr 2023
Déplacé(e) : VBBV le 11 Avr 2023
Thanks!
But I think it would not increase the accuracy.
Anyway, I learned the function smoothdata.
VBBV
VBBV le 11 Avr 2023
Déplacé(e) : VBBV le 11 Avr 2023
Calculation accuracy depends on how you set the variable limits used in the program but not using vpa.
l=4;
l1=l;%Tx Mode
l2=l;%Rx Mode
digits(128);
format long
misalignment = (-1:1e-2:1) % 1e-2 to something in finer step 1e-4 or 1e-6 etc
misalignment = 1×201
-1.000000000000000 -0.990000000000000 -0.980000000000000 -0.970000000000000 -0.960000000000000 -0.950000000000000 -0.940000000000000 -0.930000000000000 -0.920000000000000 -0.910000000000000 -0.900000000000000 -0.890000000000000 -0.880000000000000 -0.870000000000000 -0.860000000000000 -0.850000000000000 -0.840000000000000 -0.830000000000000 -0.820000000000000 -0.810000000000000 -0.800000000000000 -0.790000000000000 -0.780000000000000 -0.770000000000000 -0.760000000000000 -0.750000000000000 -0.740000000000000 -0.730000000000000 -0.720000000000000 -0.710000000000000
misalignment = -1:rand(1)*1e-4:1 % this will increase accuracy of dependent variables
misalignment = 1×76132
-1.000000000000000 -0.999973729598183 -0.999947459196366 -0.999921188794550 -0.999894918392733 -0.999868647990916 -0.999842377589099 -0.999816107187283 -0.999789836785466 -0.999763566383649 -0.999737295981832 -0.999711025580015 -0.999684755178199 -0.999658484776382 -0.999632214374565 -0.999605943972748 -0.999579673570931 -0.999553403169115 -0.999527132767298 -0.999500862365481 -0.999474591963664 -0.999448321561848 -0.999422051160031 -0.999395780758214 -0.999369510356397 -0.999343239954581 -0.999316969552764 -0.999290699150947 -0.999264428749130 -0.999238158347313
misalignment = -1:rand(1)*1e-7:1 % same here
misalignment = 1×28566322
-1.000000000000000 -0.999999929987486 -0.999999859974973 -0.999999789962459 -0.999999719949946 -0.999999649937432 -0.999999579924919 -0.999999509912405 -0.999999439899891 -0.999999369887378 -0.999999299874864 -0.999999229862351 -0.999999159849837 -0.999999089837324 -0.999999019824810 -0.999998949812297 -0.999998879799783 -0.999998809787269 -0.999998739774756 -0.999998669762242 -0.999998599749729 -0.999998529737215 -0.999998459724701 -0.999998389712188 -0.999998319699674 -0.999998249687161 -0.999998179674647 -0.999998109662134 -0.999998039649620 -0.999997969637107
misalignment = vpa(-1:1e-2:1) % but not this ... time consuming and boring sometimes
misalignment = 
祥宇 崔
祥宇 崔 le 11 Avr 2023
Déplacé(e) : VBBV le 11 Avr 2023
Thanks!
But the real problem is that the result of my program is too small to calculate for Matlab under 16bit.
By using vpa, it seems more bits can be used that the result is more accurate. One obvious evidence is that the fluctuation is gone:
VBBV
VBBV le 11 Avr 2023
Déplacé(e) : VBBV le 11 Avr 2023
ok, Here is the program execution speed if you use vpa with 8 digits
clearvars, clc
l=4;
l1=l;%Tx Mode
l2=l;%Rx Mode
digits(8); % using 8 digits
tic
misalignment = vpa(-1:1e-2:1);
and it seems you are using 128 digits !! which probably take even much more time.
祥宇 崔
祥宇 崔 le 11 Avr 2023
Déplacé(e) : VBBV le 11 Avr 2023
hhhhhh, sure. But I have to take that expense since I need the accurate result.

Connectez-vous pour commenter.

 Réponse acceptée

By using vpa, I improve the accuracy
l=4;
l1=l;%Tx Mode
l2=l;%Rx Mode
digits(128);
misalignment = vpa(-1:1e-2:1);
result = zeros(1,length(misalignment));
channel_func = @(d) 1./d.*exp(-1j.*2*pi./3e-3.*d);
phi_Tx = (0:8-1).*2.*pi./8;
phi_Rx = phi_Tx;
F_Tx = exp(1j.*l1.*phi_Tx).';
F_Rx = exp(-1j.*l2.*phi_Rx).';
%% For different misalignment, it output different result.
for i=1:length(misalignment)
distance_fun= @(x,y) sqrt(1e4-2*misalignment(i)/1e2.*cos(x)+2*misalignment(i)/1e2*cos(y)-2e-4*cos(x-y));
H = channel_func(distance_fun(phi_Tx,phi_Rx.'));
result(i) = abs(F_Rx.'*H*F_Tx);% The key calculation. How can I improve the accuracy of this matrix multiplication?
end
%% Image
figure(1);
set(0,'defaultfigurecolor','w')
set(gcf,'Position',[100 100 700 600]);
plot(misalignment,abs(result));
grid on;
xlabel('distance/meter');
ylabel('Intensity');

Plus de réponses (0)

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by