trying to create a simple plot. Get error 'Vectors must be the same length'

I am trying to plot a function of time, and its time derivative, in a 3d plot.
This is what I typed
t = [0:0.1:4]
x = exp(-(4-sqrt(3))*t)+exp(-(4+sqrt(3))*t)
v = diff(x)./0.1
plot3(x,v,t);
and I get the error " 'Error using plot3 Vectors must be the same lengths.'"

回答 (3 件)

Sean de Wolski
Sean de Wolski 2013 年 11 月 5 日
When you take the diff of t it subtracts each element of t from the next element leaving t one element shorter
diff(1:3)
doc diff
To plot with diff typically I zero or nan pad the input to diff or to plot.
plot(x,[v 0],t)
Wayne King
Wayne King 2013 年 11 月 5 日
編集済み: Wayne King 2013 年 11 月 5 日
Because when you use diff(), you end up with vector with one less element, so that t and x are one element greater in length than v.
t = [0:0.1:4];
x = exp(-(4-sqrt(3))*t)+exp(-(4+sqrt(3))*t);
v = diff(x)./0.1;
plot3(x(2:end),v,t(2:end));
or do what Sean suggests.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 5 日
t = [0:0.1:4]
x = exp(-(4-sqrt(3))*t)+exp(-(4+sqrt(3))*t)
v = gradient(x)./0.1
plot3(x,v,t);
%or
t = [0:0.1:4]
x = exp(-(4-sqrt(3))*t)+exp(-(4+sqrt(3))*t)
v = diff(x)./0.1
plot3(x(2:end),v,t(2:end));

カテゴリ

製品

質問済み:

2013 年 11 月 5 日

編集済み:

2013 年 11 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by