function Lab2_ODE_solver % This file applies to question 2 function dy=myODE(t,y) % k - spring constant % m - mass % In order to solve a 2nd order ODE in Matlab, we need to turn it into % a system of 2 1st order ODEs. dy1=y(2); %y_1' = y_2 dy2=-(k/m)*y(1); % y_2' = the right hand side in y_1'' dy=[dy1;dy2]; end % initial conditions y(0)=y0_1 and y'(0)=y0_2 y0=[5; 0]; % time interval t=0:0.1:4; % Solving the ODE for different values of the parameters %%%% Try different values for k and m to answer the questions in your lab k=1; m=1; [t,y1]=ode45(@myODE, t, y0); %%%% Try different values for k and m to answer the questions in your lab k=1; m=10; [t,y2]=ode45(@myODE, t, y0); % y1(:,1) is the displacement for the first choice of parameters % y1(:,2) is the velocity for the first choice of parameters plot(t,y1(:,1),'-o', t,y2(:,1),'-*') legend('displacement, k=1,m=1', 'displacement, k=1,m=10') end