Why won't this plot?
显示 更早的评论
I'm new to Matlab and I'm trying to plot the velocity related to the function for a damped harmonic oscillator. I have been able to plot the function itself, but when I try to plot the velocity of said function, I receive this error:
Error using plot
Vectors must be the same length.
Error in damped_oscillator (line 102)
plot(t,v);
I'm not sure how to solve this problem. I've linked the original code. Any help is much appreciated!
采纳的回答
更多回答(1 个)
The two vectors have different sizes. Since you are using the diff function. From documentation:
Y = diff(X) calculates differences between adjacent elements of X along the first array dimension whose size does not equal 1:
- If X is a vector of length m, then Y = diff(X) returns a vector of length m-1.
Hence, the t vector has a size of 1 x1000 and the v vector has a size of 1x 999.
if you want, you can update t by removing the first element where the speed is not calculated:
t = t(2:1000)
plot(t,v);
类别
在 帮助中心 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
