- #1
- 2,810
- 605
I'm trying to plot the evolution of a simple harmonic oscillator using MATLAB but I'm getting non-sense result and I have no idea what's wrong!
Here's my code:
And this is what I get:
http://www.cvberry.com/octave/graphs/graph1395294146456.jpg
What's wrong?
Thanks
Here's my code:
Code:
clear
clc
x(1)=0;
v(1)=10;
h=.001;
k=100;
m=.1;
t=[0:h:10];
n=length(t);
for i=2:n
F(i-1)=-k*x(i-1);
v(i)=(h/m)*F(i-1)+v(i-1);
x(i)=h*v(i-1)+x(i-1);
end
plot(t(1:n),x);
http://www.cvberry.com/octave/graphs/graph1395294146456.jpg
What's wrong?
Thanks
Last edited by a moderator: