title: quarter car model matlab code ode45 plotting
body: <p>I have a quarter car model which is a 2-dof system with damper and spring
  with following codes with a diagram;</p>
  
  <p><img src="https://i.stack.imgur.com/rZBv1.png" alt="quarter car model"></p>
  
  <pre><code>function dx = ode_2dof(t,x)
  m1=500;
  c1=1200;
  k1=25000;
  k2=15000;
  m2=50;
  dx=zeros(4,1);
  dx(1)=x(2)
  dx(2)=-(c1/m1)*(x(2)-x(4))-(k1/m1)*(x(1)-x(3))
  dx(3)=x(4)
  dx(4)=(c1/m2)*(x(2)-x(4))+(k1/m2)*(x(1)-x(3))-k2*x(3)
  end
  </code></pre>
  
  <p>Here, i tried to put an input as a condition of the road. First, i set the step input as a height of 0.01 metre after 0.5 second so i changed the function dx(4) as changing the function file as following;</p>
  
  <pre><code>dx(4)=(c1/m2)*(x(2)-x(4))+(k1/m2)*(x(1)-x(3))-k2*(x(3)-y)
  </code></pre>
  
  <p>with adding;</p>
  
  <pre><code>if y&gt;0.5
  y=0.01
  elseif
  y=0
  </code></pre>
  
  <p>between the code dx=zeros(4,1) and dx(1)=x(2).</p>
  
  <p>recalling this function, I could plot the graphs for x1 and x2 (i.e car and tire) displacements as following codes;</p>
  
  <pre><code>[t x] = ode45(@ode_2dof,[0 3],[0.7 0 0.35 0]) %initial position of the car and the tyre
  x1_position=x(:,1);
  x2_position=x(:,3);
  figure
  plot(t,x1_position,t,x2_position);
  grid on
  legend('x1 position','x2 position');
  </code></pre>
  
  <p>Question 1, i've got a graph as </p>
  
  <p><img src="https://i.stack.imgur.com/ZEiPp.png" alt="graph 1"></p>
  
  <p>but this is not the graph I want, how can I get the graph with x1 settling at 0.7 and x2 at 0.35 respectively as the initial condition, not at 0?</p>
  
  <p>Also, I tried to change the x1, k1 and k2 values to see how it affects on the displacements with changing the x1, k1 and k2 values respectively in the function file and saved separately. </p>
  
  <pre><code>function dx = ode_2dof2700(t,x)
  c1=2700
  
  function dx = ode_2dof40000(t,x)
  k1=40000
  
  function dx = ode_2dof30000(t,x)
  k2=30000
  
  function dx = ode_2dofmix(t,x)
  c1=2700
  k1=40000
  k2=30000
  </code></pre>
  
  <p>Question 2, i tried to plot the graph all x1 and x2 at the same time in different graphs as following;</p>
  
  <pre><code>[t x] = ode45(@ode_2dof,[0 5],[0.7 0 0.35 0])
  x1_position=x(:,1);
  x2_position=x(:,3);
  [t x] = ode45(@ode_2dof2700,[0 5],[0.7 0 0.35 0])
  x1_position_2700=x(:,1);
  x2_position_2700=x(:,3);
  [t x] = ode45(@ode_2dof40000,[0 5],[0.7 0 0.35 0])
  x1_position_40000=x(:,1);
  x2_position_40000=x(:,3);
  [t x] = ode45(@ode_2dof30000,[0 5],[0.7 0 0.35 0])
  x1_position_30000=x(:,1);
  x2_position_30000=x(:,3);
  [t x] = ode45(@ode_2dofmix,[0 5],[0.7 0 0.35 0])
  x1_position_mix=x(:,1);
  x2_position_mix=x(:,3);
  
  figure
  plot(t,x1_position,t,x1_position_2700,t,x1_position_40000,t,x1_position_30000,t,x1_position_mix);
  grid on
  </code></pre>
  
  <p>but i've got an error that the vector array have to be matched. It is actually different as x1_position is 1361x1 and x2_position_2700 is 1041x1 etc. how can i change the code to match the array?</p>
  
  <p>Question 3, if i want to set a sinusoidal input with an amplitude of 0.01 metre, how should i change the input?</p>
  
  <p>Thanks</p>