Vectors must be the same lengths; plotting

6 visualizzazioni (ultimi 30 giorni)
alexr
alexr il 2 Mag 2011
Commentato: Komal Kumawat il 2 Ott 2020
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 Commenti
alexr
alexr il 3 Mag 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 il 16 Gen 2018
can you please tell me how did you solve the problem of number of elements and how you made it equal?

Accedi per commentare.

Risposte (1)

Smith
Smith il 30 Ott 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)
  3 Commenti
Jan
Jan il 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.
Komal Kumawat
Komal Kumawat il 2 Ott 2020
%*********************************************************% %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

Accedi per commentare.

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by