Vectors must be the same lengths; plotting

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 Kommentare

Paulo Silva
Paulo Silva am 3 Mai 2011
For the plot function to work x and y must have the same number of elements,your x vector got 51 elements and the simulated and actual vectors just 11.
You can solve the problem by making the x vector with 11 elements instead of the 51 or by adding NaN values to the simulated vector like this
simulated(end:end+numel(x)-numel(simulated))=nan;
plot(x,simulated)
(I couldn't post an answer to this question for some unknown reason, it gives me always one error)
Andrew Newell
Andrew Newell am 3 Mai 2011
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));
alexr
alexr am 3 Mai 2011
Great thanks, I thought only the number of elements in the simulated length had to match the actual. It works now
waqas muhammad
waqas muhammad am 16 Jan. 2018
can you please tell me how did you solve the problem of number of elements and how you made it equal?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Smith
Smith am 30 Okt. 2016

1 Stimme

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)

3 Kommentare

assiya malik
assiya malik am 14 Nov. 2016
Bearbeitet: Jan am 14 Nov. 2016
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
Jan
Jan am 14 Nov. 2016
@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.
%*********************************************************% %Defining the frequency vector and the mass matrix, %damping matrix, the stiffness matrix and the amplitude of %the excitation force. %*********************************************************% f=linspace(0,0.7,50); m=[1 0;0 2]; k=[2 -1;-1 1]; c=[0.002 -0.001;-0.001 0.001]; fi=[1;1]; %*********************************************************% %Calculating the amplitude and the phase for each frequency %defined by the frequency vector. %*********************************************************% for i = 1:50 omega(i)=2*pi*f(i); %omega in terms of frequency omega2(i)=omega(i)*omega(i); % squaring omega a11=-omega2(i)*m+k; % representing the left hand… a12=omega(i)*c; % matrix of the single matrix… a21=-omega(i)*c; % equation a22=-omega2(i)*m+k; a=[a11 a12;a21 a22]; b=inv(a); c1=[0;0;fi]; d(1,i)=b(1,:)*c1; d(2,i)=b(2,:)*c1; d(3,i)=b(3,:)*c1; d(4,i)=b(4,:)*c1; x(1,i)=sqrt(abs(d(1,i))^2+abs(d(3,i))^2); x(2,i)=sqrt(abs(d(2,i))^2+abs(d(4,i))^2); p(1,i)=atan(d(1,i)/d(3,i))*180/pi; if p(1,i)<0 % to check whether the angle is negative or not. p(1,i)=180+p(1,i); else
p(1,i)=p(1,i);
end
p(2,i)=atan(d(2,i)/d(4,i))*180/pi;
if p(2,i)<0
if d(4,i)<0
p(2,i) = -180 + p(2,i)
else
p(2,i)=180+p(2,i);
end
else
p(2,i)=p(2,i);
end
end
figure(1)
plot(f,x(1,:));grid
xlabel(Frequency)
ylabel(Amplitude of Mass 1)
figure(2)
plot(f,x(2,:));grid
xlabel(Frequency)
ylabel(Amplitude of Mass 2)
figure(3)
plot(f,p(1,:));grid
xlabel(Frequency)
ylabel(Phase of Mass 1)
figure(4)
plot(f,p(2,:));grid
xlabel(Frequency)
ylabel(Phase of Mass 2)
Why this shows error, please tell me the solution, actually this is my project

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Gefragt:

am 2 Mai 2011

Kommentiert:

am 2 Okt. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by