why my code is running so slowly in the while loop

1 visualización (últimos 30 días)
Muhammed Emin Yavuzaslan
Muhammed Emin Yavuzaslan el 20 de Ag. de 2022
Comentada: VBBV el 23 de Ag. de 2022
Hi, im new at Matlab and im trying to implement a guidance for Uav but my while loop running so slowly.
Any suggestions for fix this trouble ?
clc;
clear all;
close all;
r = 1250;
O = [2500 2500]
Px = 2500; Py = 1000;
P = [ Px Py ];
Va = 100;
To = 4;
q22 = 0.1
psi = -0.4;
delta = 10;
K =0.5;
Rmin = 75
K2 = 35;
B = 0 : 0.001 : 2*pi;
c = (r* (cos(B)))+O(1);
d = (r* (sin(B)))+O(2);
N = (r * (cos(B)))+O(1);
M = (r * (sin(B)))+O(2);
% Wa = [0 300]; %
% Wb = [0 300]; %
Vx = Va * cos( psi );
Vy = Va * sin( psi );
thetaU = atan2(( Px - O(2)),( Py - O(1)));
d = abs(sqrt (( O(1) - Py )^2 + ( O(2) - Px )^2) - r)
xt = ((r * (cos ( thetaU+delta ))) + O(1));
yt = ((r * (sin ( thetaU+delta ))) + O(2));
Vd = Va*sin(psi-thetaU);
q11 = sqrt(abs(To / (To-d)));
psiD = atan2(( yt -Py ),( xt - Px ));
u =- ((d*q11) + sqrt(2*q11+q22^2)*Vd);
while d > 0
t = 0.05;
d = abs(sqrt (( O(1) - Py )^2 + ( O(2) - Px )^2) - r)
thetaU = atan2(( Px - O(2)),( Py - O(1)));
xt = ((r * (cos ( thetaU+delta ))) + O(1));
yt = ((r * (sin ( thetaU+delta ))) + O(2));
Vd = Va*sin(psi-thetaU);
q11 = sqrt(abs(To / (To-d)));
psiD = atan2(( yt -Py ),( xt - Px ));
u =- ((d*q11) + sqrt(2*q11+q22^2)*Vd);
psi = psiD ;
if(abs(u) > (Va^2)/Rmin)
if (u > 0)
u = (Va^2)/Rmin;
else
u = -(Va^2)/Rmin;
end
end
Vy = Va * sin ( psi ) + u * t;
Vx = Va * cos(psi) + u * t;
Px = Px + Vx *t;
Py = Py + Vy *t;
t = t + 0.1;
hold on;
plot (Px ,Py ,"r--.",O(1) , O(2) ,'b-o');
plot (c,d,'r')
plot(N,M,'k')
title("2D Carrot Chase Trajectory")
xlabel("Position");
ylabel("Position"),
end

Respuestas (1)

VBBV
VBBV el 20 de Ag. de 2022
clc;
clear all;
close all;
r = 1250;
O = [2500 2500]
O = 1×2
2500 2500
Px = 2500; Py = 1000;
P = [ Px Py ];
Va = 100;
To = 4;
q22 = 0.1
q22 = 0.1000
psi = -0.4;
delta = 10;
K =0.5;
Rmin = 75
Rmin = 75
K2 = 35;
B = 0 : 0.001 : 2*pi;
c = (r* (cos(B)))+O(1);
d = (r* (sin(B)))+O(2);
N = (r * (cos(B)))+O(1);
M = (r * (sin(B)))+O(2);
% Wa = [0 300]; %
% Wb = [0 300]; %
Vx = Va * cos( psi );
Vy = Va * sin( psi );
thetaU = atan2(( Px - O(2)),( Py - O(1)));
d = abs(sqrt (( O(1) - Py )^2 + ( O(2) - Px )^2) - r)
d = 250
xt = ((r * (cos ( thetaU+delta ))) + O(1));
yt = ((r * (sin ( thetaU+delta ))) + O(2));
Vd = Va*sin(psi-thetaU);
q11 = sqrt(abs(To / (To-d)));
psiD = atan2(( yt -Py ),( xt - Px ));
u =- ((d*q11) + sqrt(2*q11+q22^2)*Vd);
i = 1
i = 1
t = 0.05;
while d > 0
thetaU = atan2(( Px - O(2)),( Py - O(1)));
xt = ((r * (cos ( thetaU+delta ))) + O(1));
yt = ((r * (sin ( thetaU+delta ))) + O(2));
Vd = Va*sin(psi-thetaU);
q11 = sqrt(abs(To / (To-d)));
psiD = atan2(( yt -Py ),( xt - Px ));
u =- ((d*q11) + sqrt(2*q11+q22^2)*Vd);
psi = psiD ;
if(abs(u) > (Va^2)/Rmin)
if (u > 0)
u = (Va^2)/Rmin;
else
u = -(Va^2)/Rmin;
end
end
Vy = Va * sin ( psi ) + u * t;
Vx = Va * cos(psi) + u * t;
Pxx(i) = Px + Vx *t;
Pyy(i) = Py + Vy *t;
t = t + 0.1;
d = d-1; % use one counter to check the while conditon
i = i+1;
end
plot (Pxx ,Pyy ,"r--.",O(1) , O(2) ,'b-o');
% subplot(211)
% plot (c,d,'or')
% subplot(212)
% plot(N,M,'+k')
title("2D Carrot Chase Trajectory")
xlabel("Position");
ylabel("Position"),
  6 comentarios
Muhammed Emin Yavuzaslan
Muhammed Emin Yavuzaslan el 23 de Ag. de 2022
@VBBV Thanks for answering my quesiton. But i have a little question more.
You changed my equation
Px = Px + Vx *t;
Py = Py + Vy *t;
to this
Pxx(i) = Px + Vx *t;
Pyy(i) = Py + Vy *t;
why we did this ?
and my another quesiton is theta is related with Px and Py variables. When we did
Px--Pxx and Py--Pyy theta doesnt change. its stuck at zero but it must be change.
So what should i do
Sorry for my English. I'm trying my best
Thanks a lot
VBBV
VBBV el 23 de Ag. de 2022
This is done to save computed values in a new array during iteration in while loop.
clc;
clear all;
close all;
r = 1250;
O = [2500 2500]
O = 1×2
2500 2500
Px = 2500; Py = 1000;
P = [ Px Py ];
Va = 200;
alpha = 1;
k = 1;
Rmin = 75;
psi = -0.4;
delta = 0.1;
Vx = Va * cos( psi );
Vy = Va * sin( psi );
K =0.5;
B = 0 : 0.001 : 2*pi;%plot of circle
c = (r* (cos(B)))+O(1);
d = (r* (sin(B)))+O(2);
N = (r * (cos(B)))+O(1);
M = (r * (sin(B)))+O(2);
d = abs(sqrt (( O(2) - Py )^2 + ( O(1) - Px )^2));
theta = atan2(Py-O(2),Px-O(1));
xt = ((r * (cos ( theta+delta ))) + O(1));
yt = ((r * (sin ( theta+delta ))) + O(2));
psiD = atan2(( yt -Py ),( xt - Px ));
if d > 2*r
psid = theta-pi+asin(r/d)
psic = psid+ (Va*sin(psi-theta) /(alpha*d))
else
psid = (theta-(pi/2) - (pi/3)*(((d-r)/r)^k) );
psic = psid-(Va*sin(psi-theta)/(alpha*d))- (k*Va*pi*(d^(k-1))) / (3*(r^k)*alpha);
end
u = K*(psic-psi)*Va;
i = 1;
t = 0.05;
d = abs(sqrt (( O(2) - Py )^2 + ( O(1) - Px )^2) - r);
while abs (d) > 0
theta = atan2(Py-O(2),Px-O(1))
xt = ((r * (cos ( theta+delta ))) + O(1));
yt = ((r * (sin ( theta+delta ))) + O(2));
psiD = atan2(( yt -Py ),( xt - Px ));
if d > 2*r
psid = theta-pi+asin(r/d)
psic = psid+ (Va*sin(psi-theta) /(alpha*d))
else
psid = (theta-(pi/2) - (pi/3)*(((d-r)/r)^k) );
psic = psid-(Va*sin(psi-theta)/(alpha*d))- (k*Va*pi*(d^(k-1))) / (3*(r^k)*alpha);
end
psi = psiD;
if(abs(u) > (Va^2)/Rmin)
if (u > 0)
u = (Va^2)/Rmin;
else
u = -(Va^2)/Rmin;
end
end
Vy = Va * sin ( psi ) + u * t;
Vx = Va * cos(psi) + u * t;
Px = Px + Vx *t; %
Py = Py + Vy *t;
t = t + 0.01;
d = d-1;
i = i+1;
hold on;
plot (Px ,Py ,"ro",O(1) , O(2) ,'b-o');
title("2D Carrot Chase Trajectory")
xlabel("Position");
ylabel("Position"),
end
theta = -1.5708
theta = -1.5684
theta = -1.5656
theta = -1.5622
theta = -1.5584
theta = -1.5540
theta = -1.5490
theta = -1.5433
theta = -1.5367
theta = -1.5293
theta = -1.5207
theta = -1.5110
theta = -1.5000
theta = -1.4877
theta = -1.4740
theta = -1.4591
theta = -1.4432
theta = -1.4264
theta = -1.4090
theta = -1.3912
theta = -1.3733
theta = -1.3553
theta = -1.3376
theta = -1.3201
theta = -1.3031
theta = -1.2866
theta = -1.2707
theta = -1.2556
theta = -1.2412
theta = -1.2277
theta = -1.2151
theta = -1.2035
theta = -1.1930
theta = -1.1836
theta = -1.1754
theta = -1.1685
theta = -1.1629
theta = -1.1586
theta = -1.1559
theta = -1.1546
theta = -1.1549
theta = -1.1569
theta = -1.1606
theta = -1.1661
theta = -1.1735
theta = -1.1828
theta = -1.1941
theta = -1.2074
theta = -1.2229
theta = -1.2406
theta = -1.2606
theta = -1.2830
theta = -1.3078
theta = -1.3350
theta = -1.3649
theta = -1.3973
theta = -1.4323
theta = -1.4698
theta = -1.5097
theta = -1.5518
theta = -1.5958
theta = -1.6412
theta = -1.6875
theta = -1.7339
theta = -1.7798
theta = -1.8245
theta = -1.8674
theta = -1.9080
theta = -1.9461
theta = -1.9814
theta = -2.0138
theta = -2.0435
theta = -2.0705
theta = -2.0949
theta = -2.1171
theta = -2.1371
theta = -2.1551
theta = -2.1714
theta = -2.1861
theta = -2.1994
theta = -2.2114
theta = -2.2223
theta = -2.2322
theta = -2.2411
theta = -2.2492
theta = -2.2566
theta = -2.2634
theta = -2.2695
theta = -2.2752
theta = -2.2803
theta = -2.2850
theta = -2.2894
theta = -2.2934
theta = -2.2971
theta = -2.3005
theta = -2.3036
theta = -2.3065
theta = -2.3092
theta = -2.3117
theta = -2.3141
theta = -2.3162
theta = -2.3182
theta = -2.3201
theta = -2.3219
theta = -2.3235
theta = -2.3251
theta = -2.3265
theta = -2.3279
theta = -2.3291
theta = -2.3303
theta = -2.3315
theta = -2.3325
theta = -2.3335
theta = -2.3345
theta = -2.3354
theta = -2.3362
theta = -2.3370
theta = -2.3377
theta = -2.3385
theta = -2.3391
theta = -2.3398
theta = -2.3404
theta = -2.3410
theta = -2.3415
theta = -2.3420
theta = -2.3425
theta = -2.3430
theta = -2.3435
theta = -2.3439
theta = -2.3443
theta = -2.3447
theta = -2.3451
theta = -2.3454
theta = -2.3458
theta = -2.3461
theta = -2.3464
theta = -2.3467
theta = -2.3470
theta = -2.3473
theta = -2.3476
theta = -2.3478
theta = -2.3481
theta = -2.3483
theta = -2.3485
theta = -2.3487
theta = -2.3489
theta = -2.3491
theta = -2.3493
theta = -2.3495
theta = -2.3497
theta = -2.3499
theta = -2.3501
theta = -2.3502
theta = -2.3504
theta = -2.3505
theta = -2.3507
theta = -2.3508
theta = -2.3509
theta = -2.3511
theta = -2.3512
theta = -2.3513
theta = -2.3514
theta = -2.3515
theta = -2.3517
theta = -2.3518
theta = -2.3519
theta = -2.3520
theta = -2.3521
theta = -2.3522
theta = -2.3523
theta = -2.3523
theta = -2.3524
theta = -2.3525
theta = -2.3526
theta = -2.3527
theta = -2.3527
theta = -2.3528
theta = -2.3529
theta = -2.3530
theta = -2.3530
theta = -2.3531
theta = -2.3532
theta = -2.3532
theta = -2.3533
theta = -2.3533
theta = -2.3534
theta = -2.3535
theta = -2.3535
theta = -2.3536
theta = -2.3536
theta = -2.3537
theta = -2.3537
theta = -2.3538
theta = -2.3538
theta = -2.3538
theta = -2.3539
theta = -2.3539
theta = -2.3540
theta = -2.3540
theta = -2.3541
theta = -2.3541
theta = -2.3541
theta = -2.3542
theta = -2.3542
theta = -2.3542
theta = -2.3543
theta = -2.3543
theta = -2.3543
theta = -2.3544
theta = -2.3544
theta = -2.3544
theta = -2.3545
theta = -2.3545
theta = -2.3545
theta = -2.3546
theta = -2.3546
theta = -2.3546
theta = -2.3546
theta = -2.3547
theta = -2.3547
theta = -2.3547
theta = -2.3547
theta = -2.3548
theta = -2.3548
theta = -2.3548
theta = -2.3548
theta = -2.3548
theta = -2.3549
theta = -2.3549
theta = -2.3549
theta = -2.3549
theta = -2.3549
theta = -2.3550
theta = -2.3550
theta = -2.3550
theta = -2.3550
theta = -2.3550
theta = -2.3551
theta = -2.3551
theta = -2.3551
theta = -2.3551
theta = -2.3551
theta = -2.3551
theta = -2.3551
theta = -2.3552
theta = -2.3552
theta = -2.3552
theta = -2.3552
theta = -2.3552
theta = -2.3552

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by