Mass spring system: given code unable to run and returning errors
Show older comments
I am starting a project that covers modeling a mass spring system in MatLab. In this lab, we've been provided with some initial code to run in a .m file. According to the video instructions for the project, the code should run without any modification or edits. However, when I run the code, numerous errors are returned.
The code:
function LAB08ex1
m = 1; % mass [kg]
k = 4; % spring constant [N/m]
omega0 = sqrt(k/m);
y0 = 0.1; v0 = 0; % initial conditions
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega0); % solve for 0<t<10
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'b+-',t,v,'ro-'); % time series for y and v
grid on
%legend('y(t)','v(t)=y''(t)'); % note the use of '' for '
%figure(2); plot(y,v); % phase plot
%xlabel('y'); ylabel('v=y'''); grid on
%-----------------------------------------
function dYdt = f(t,Y,omega0)
y = Y(1); v = Y(2);
dYdt = [ v ; -omega0^2*y ];
Running this code returns the following errors:
>> LAB08ex1
Index exceeds array bounds.
Error in LAB08ex1>f (line 15)
y = Y(1); v = Y(2);
Error in ode45 (line 273)
k_(:,1)=feval(FUN,x,t);
Error in LAB08ex1 (line 6)
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega0); % solve for 0<t<10
What is going wrong and how should I fix it?
Accepted Answer
More Answers (0)
Categories
Find more on Assembly 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!