How to improve the calculation accuracy of Matlab? (2024)

9 views (last 30 days)

Show older comments

祥宇 崔 on 11 Apr 2023

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab

Moved: VBBV on 11 Apr 2023

Accepted Answer: 祥宇 崔

Open in MATLAB Online

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');

How to improve the calculation accuracy of Matlab? (2)

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 Comments

Show 4 older commentsHide 4 older comments

VBBV on 11 Apr 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700639

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700639

Moved: VBBV on 11 Apr 2023

Open in MATLAB Online

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')

How to improve the calculation accuracy of Matlab? (4)

祥宇 崔 on 11 Apr 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700009

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700009

Moved: VBBV on 11 Apr 2023

Thanks!

But I think it would not increase the accuracy.

Anyway, I learned the function smoothdata.

VBBV on 11 Apr 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700129

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700129

Moved: VBBV on 11 Apr 2023

Open in MATLAB Online

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=How to improve the calculation accuracy of Matlab? (7)

祥宇 崔 on 11 Apr 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700219

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700219

Moved: VBBV on 11 Apr 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:

How to improve the calculation accuracy of Matlab? (9)

VBBV on 11 Apr 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700289

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700289

Moved: VBBV on 11 Apr 2023

Open in MATLAB Online

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);

How to improve the calculation accuracy of Matlab? (11)

and it seems you are using 128 digits !! which probably take even much more time.

祥宇 崔 on 11 Apr 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700349

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#comment_2700349

Moved: VBBV on 11 Apr 2023

hhhhhh, sure. But I have to take that expense since I need the accurate result.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

祥宇 崔 on 11 Apr 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#answer_1213364

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1944999-how-to-improve-the-calculation-accuracy-of-matlab#answer_1213364

Open in MATLAB Online

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 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABMathematics

Find more on Mathematics in Help Center and File Exchange

Tags

  • accuracy
  • matrix manipulation
  • fluctuation

Products

  • MATLAB

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to improve the calculation accuracy of Matlab? (14)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to improve the calculation accuracy of Matlab? (2024)
Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5823

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.