
Plot Error (2-D Graph)
    4 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
So I have a piece of code attached below, when running the code I get an error at the line 
plot(c_t1(1:59),c_acc);
so I'm unsure as to why there is an error. Additionally, the circular velocity graphs are screwed up too. (A figure pops up but not lines to indicate the actual thing that is being graphed).
Would it be that my for loop is incorrect?
%circular position intizalization
c_t = linspace(0,2*pi,61);
c_x = 1.2*cos(c_t); 
c_y = 1.2*sin(c_t) + 1.2;
%circular position graph
figure;
plot(c_x,c_y)
title('circular x vs. y-position'); xlabel('circular x-position(m)');
ylabel('circular y-position(m)');
grid on; hold on;
c_t1 = linspace(0,60,61);
%loop for circular velocity
for i = 1:(length(c_t1) - 1)
    c_vel_x(i) = ((c_x(i+1) - c_x(i)));
    c_vel_y(i) = ((c_y(i+1) - c_y(i)));
    c_vel = sqrt(c_vel_x(i)^2 + c_vel_y(i)^2);
end
%loop for circular acceleration
for i = 1:(length(c_vel) - 1)
    c_acc_x(i) = ((c_vel_x(i+1) - c_vel_x(i)));
    c_acc_y(i) = ((c_vel_y(i+1) - c_vel_y(i)));
    c_acc= sqrt(c_acc_x(i)^2 + c_acc_y(i)^2);
end
%circular velocity graphs
figure;
plot(c_t1(1:length(c_vel)),c_vel);hold on;
plot(c_t1(1:length(c_vel)),c_vel_x); plot(c_t1(1:length(c_vel)),c_vel_y);
title("circ. velocity vs. time"); xlabel('time(s)'); 
ylabel('velocity(m/s)');
legend('velocity','x-velocity','y-velocity');
grid on;
%circular acceleration graphs
figure;
plot(c_t1(1:59),c_acc) %this where the error occurs
hold on;
plot(c_t1(1:59),c_acc_x);plot(c_t1(1:59),c_acc_y);
title("circ. acc, vs. time"); xlabel('time(s)'); ylabel('acc.(m^2/s)');
legend('acc.','x-acc','y-acc');
grid on;
0 commentaires
Réponses (1)
  Riya
 le 6 Fév 2025
        Hi Vishwak,
I see that you are facing some errors in your code. It seems the problem comes from this line:
plot(c_t1(1:59), c_acc);
It seems you are trying to access “c_acc” up to index 59, which could be out of bounds. According to your code, “c_acc” is computed within this loop:
for i = 1:(length(c_vel) - 1) 
    c_acc_x(i) = ((c_vel_x(i+1) - c_vel_x(i))); 
    c_acc_y(i) = ((c_vel_y(i+1) - c_vel_y(i))); 
    c_acc = sqrt(c_acc_x(i)^2 + c_acc_y(i)^2); 
end
The issue is that “c_acc” is being reassigned during each iteration, which means it only retains the last computed value instead of storing an array. To resolve this, modify your loop like this:
c_acc(i) = sqrt(c_acc_x(i)^2 + c_acc_y(i)^2);
This ensures that “c_acc” retains all computed values instead of only the most recent one.
For improved performance, it is advisable to preallocate memory for the arrays before entering the loop:
c_vel_x = zeros(1, length(c_t1) - 1); 
c_vel_y = zeros(1, length(c_t1) - 1); 
c_acc_x = zeros(1, length(c_t1) - 2); 
c_acc_y = zeros(1, length(c_t1) - 2); 
c_acc = zeros(1, length(c_t1) - 2);
Here is the output of the updated code:

0 commentaires
Voir également
Catégories
				En savoir plus sur Graphics Performance dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

