"Index exceeds the number of array elements" Spring Mass Damper System
1 view (last 30 days)
Show older comments
I am getting "Index exceeds the number of array elements" error while solving a Spring Mass Damper System with unit step input using Euler Integration. Here is the code:
clc
clear all
m = 5;
c = 0.5;
k = 1;
%input
h = 1/60;
t_fin = 50;
N = t_fin/h;
%Initial Conditions
t(1)=0;
x(1)=1;
v(1)=0;
u(2:51)=2;
%loop
for i=1:N
t(i+1) = t(i)+h;
x(i+1) = x(i)+h*(v(i));
v(i+1) = v(i)+h*(u(i)-(c/m)*v(i)-(k/m)*x(i));
end
figure(1)
plot(t,x)
figure(2)
plot(t,v)
0 Comments
Accepted Answer
David Hill
on 2 Apr 2021
for i=1:N
t(i+1) = t(i)+h;
x(i+1) = x(i)+h*(v(i));
v(i+1) = v(i)+h*(u(i)-(c/m)*v(i)-(k/m)*x(i));%appears your problem is with u
end
Loop goes from 1 to 3000, yet u is only defined from 1:51. Once the loop gets to i=52 it will error.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!