How to improve the calculation accuracy of Matlab?

17 ビュー (過去 30 日間)
祥宇 崔
祥宇 崔 2023 年 4 月 11 日
移動済み: VBBV 2023 年 4 月 11 日
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 件のコメント
VBBV
VBBV 2023 年 4 月 11 日
移動済み: VBBV 2023 年 4 月 11 日
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.
祥宇 崔
祥宇 崔 2023 年 4 月 11 日
移動済み: VBBV 2023 年 4 月 11 日
hhhhhh, sure. But I have to take that expense since I need the accurate result.

サインインしてコメントする。

採用された回答

祥宇 崔
祥宇 崔 2023 年 4 月 11 日
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');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by