Why won't this plot?
Show older comments
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!
Accepted Answer
More Answers (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);
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!