MATLAB Answers

Vectors must be the same length.

8 views (last 30 days)
Ameyr Rosha
Ameyr Rosha on 30 May 2021
Answered: Star Strider on 30 May 2021
Help!!! im trying to plot a graph. But its always tell me 'Vectors must be the same length.'
h=0.04; %step size
t=0.04:h:0.6; %time
y=[0.1560 0.0125 0.0850 0.2360 0.2450 0.0350 0.5610];%Displacement
v=diff(y)/h; %First derivative of y
a=diff(v)/h; %Second derivative of y
j=diff(a)/h; % Third derivative of y
%plot y vs t
plot(t,y,'LineWidth',2);
grid on
xlim([min(t) max(t)])
xlabel('t')
ylabel('y')
title('Plot of y vs. t')
Error using plot
Vectors must be the same length.
  1 Comment
Torsten
Torsten on 30 May 2021
What is the t vector that corresponds to the displacement vector ? It must have 7 elements.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 30 May 2021
Use the gradient function instead of diff and define ‘t’ based on ‘y’
% h=0.04; %step size
% t=0.04:h:0.6; %time
y=[0.1560 0.0125 0.0850 0.2360 0.2450 0.0350 0.5610];%Displacement
t = linspace(0.04, 0.6, numel(y));
h = t(2)-t(1)
h = 0.0933
v=gradient(y,h); %First derivative of y
a=gradient(v,h); %Second derivative of y
j=gradient(a,h); % Third derivative of y
%plot y vs t
figure
plot(t,y,'LineWidth',2);
grid on
xlim([min(t) max(t)])
xlabel('t')
ylabel('y')
title('Plot of y vs. t')
.

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 30 May 2021
h=0.04; %step size
t=0.04:h:0.6; %time
size(t) =
1 15
y=[0.1560 0.0125 0.0850 0.2360 0.2450 0.0350 0.5610];
size(y) =
1 7
How can you plot a graph with 15 x-axis points but only 7 y-axis points?
For plotting graphs with 2 inputs, you need both inputs with same dimensions, as suggested by the clear error message. Errors in MATLAB are quite simple and clear in stating what the problem is.
You can try this and plot again
t=linspace(0.04,0.6,7);

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by