Avoiding drift while creating angular rotation with variable degree increments
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
With the following code, I am trying to create a variable "deg" which should have small degree increments (ddeg_min) for deg<=angle and large increments (ddeg_max) for rest of a complete revolution. I want to repeat the same increments after completion of each revolution. For example, the increment should be 0.1 for 0<=deg<=16.0 and it should repeat the same thing after 360 degrees i.e. incr should be 0.1 for 360.1<=deg<=316.0. But, I am getting a drift in values e.g. 16.1 for first revolution and 366.1 for next revolution and so on.
It was hard to explain but I tried to make it as comprehendible as possible. Waiting for your kind suggestions, Thx.
close all
clear all
clc
phi=90;
angle=16;
ddeg_min=0.1;
ddeg_max=10;
deg(1)=0;
d_deg(1)=ddeg_min;
i=1;
while (1)
i=i+1;
revol_n=floor(deg(i-1)/360);
kkk=deg(i-1)-revol_n*360;
if (kkk >= 0 && kkk < angle)
d_deg(i)=ddeg_min;
deg(i)=deg(i-1)+d_deg(i);
else
d_deg(i)=ddeg_max;
deg(i)=deg(i-1)+d_deg(i);
end
if deg(i)>=1070
break
end
end
0 Commenti
Risposta accettata
Jan
il 24 Nov 2021
Modificato: Jan
il 24 Nov 2021
Let's start with a simplification of the code:
phi = 90;
angle = 16;
ddeg_min = 0.1;
ddeg_max = 10;
deg(1) = 0;
d_deg(1) = ddeg_min;
i = 1;
while deg(i) < 1070
kkk = rem(deg(i), 360);
if kkk < angle
deg(i + 1) = deg(i) + ddeg_min;
else
deg(i + 1) = deg(i) + ddeg_max;
end
i = i + 1;
end
Or:
phi = 90;
angle = 16;
ddeg = [0.1, 10];
deg = 0;
i = 1;
while deg(i) < 1070
deg(i + 1) = deg(i) + ddeg(1 + rem(deg(i), 360) < angle);
i = i + 1;
end
What is the problem now?
"the increment should be 0.1 for 0<=deg<=16.0"
"I am getting a drift in values e.g. 16.1"
Yes, of course. Welcome to the world of numerical maths.
0.1 + 0.1 + 0.1 == 0.3 % FALSE !!!
0.1 + 0.1 + 0.1 - 0.3 % This is 5.551115123125783e-17
Most floating point values cannot be represented accurately as binary numbers. Therefore you have to consider a rounding effekt. Your loop does not stop at the wanted value, because it does not reach 16, but 15.99999999999996. This must be considered either by using a specific range:
if abs(kkk - angle) < 100 * eps(angle)
But in ypour case it is easier to create the output by a constructive apporach:
deg = [0:0.1:16, 26:10:360];
deg = [deg, deg + 360, deg + 720];
deg = deg(deg < 1070);
Più risposte (1)
Mathieu NOE
il 24 Nov 2021
hello
why make things so complicated ?
this way you can generate the pattern for the first revolution . if you want to have it for multiple revolutions simply add 360 for each rev
close all
clear all
clc
phi=90;
angle=16;
ddeg_min=0.1;
ddeg_max=10;
% one revolution pattern
deg1 = (0:ddeg_min:angle);
deg2 = (angle+ddeg_max:ddeg_max:360);
deg = [deg1 deg2];
6 Commenti
Vedere anche
Categorie
Scopri di più su Number Theory in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!