Plot Error (2-D Graph)

4 views (last 30 days)
Vishwak Tejasri Turaga
Vishwak Tejasri Turaga on 11 Apr 2019
Answered: Riya on 6 Feb 2025
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;

Answers (1)

Riya
Riya on 6 Feb 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:

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!