MATLAB Answers

alexr
2

Vectors must be the same lengths; plotting

Asked by alexr
on 2 May 2011
Latest activity Commented on by waqas muhammad on 16 Jan 2018
I have googled this to death, the common error was people looping arrays. I just have 2 simple sets and cant see where the error is.
simulated=[0.0000000 4.3830000 4.9470000 2.0180000 0.1761000 0.0162800 4.3650000 4.9540000 2.0170000 0.1760000 0.0162700 ]
actual= [0.0750000 4.0750000 4.7630000 3.5130000 0.5125000 0.1375000 2.6380000 4.5750000 4.8880000 1.5130000 0.3250000 ]
x = 0:0.0002:0.01;
plot(x, simulated, x, actual);
legend('Simulated', 'Actual')
ylabel('Current (mA)')
xlabel('Time ( mS )')
title('Current across R2 as a function of VS','FontSize',12)

  4 Comments

Show 1 older comment
To create an array x with the same span of values but the right size, you could do this:
x = linspace(0,0.01,length(simulated));
Great thanks, I thought only the number of elements in the simulated length had to match the actual. It works now
can you please tell me how did you solve the problem of number of elements and how you made it equal?

Sign in to comment.

Tags

Products

1 Answer

Answer by Smith
on 30 Oct 2016

You just need to modify variable x to match the size of variable simulated or variable actual.
simulated=[0.0000000 4.3830000 4.9470000 2.0180000 0.1761000 0.0162800 4.3650000 4.9540000 2.0170000 0.1760000 0.0162700 ]
actual= [0.0750000 4.0750000 4.7630000 3.5130000 0.5125000 0.1375000 2.6380000 4.5750000 4.8880000 1.5130000 0.3250000 ]
x = linspace(0,0.01,length(actual));
plot(x, simulated, x, actual);
legend('Simulated', 'Actual')
ylabel('Current (mA)')
xlabel('Time ( mS )')
title('Current across R2 as a function of VS','FontSize',12)

  2 Comments

i am getting the same error i used linspace command still i coundnt remove the error my code contains
y =1.3:h:-1.3; % Calculates upto y(1)
u = zeros(1,length(y));
v = zeros(1,length(y));
w = zeros(1,length(y));
z = zeros(1,length(y));
after this i calculated u , v , w, z by Runge Kutta 4 method finally when i plotted using
plot(y,z,'y--',y,w,'g--',y,v,'r',y,u,'b--');
i got the same error
@assiya malik: Please do not post a new question as a comment to an answer, but open a new thread. Note that the problem is hidding inside "calculated by Runge Kutta 4 method" and not shown here. It does not matter how you define the variables before you redefine them by any computations.

Sign in to comment.