Vectors must be the same length.

15 visualizzazioni (ultimi 30 giorni)
Ameyr Rosha
Ameyr Rosha il 30 Mag 2021
Risposto: Star Strider il 30 Mag 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 Commento
Torsten
Torsten il 30 Mag 2021
What is the t vector that corresponds to the displacement vector ? It must have 7 elements.

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 30 Mag 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')
.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by