Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.00 KB | None | 0 0
  1. dt=1e-4; %10^-3
  2. m=1;
  3. g=9.81;
  4.  
  5. A=m; %inertia, resistance to a change in velocity
  6. B=4; %resistive force that pushes against motion
  7. C=1; %restoring force, like a spring pushing back from being moved from rest
  8. F=@(t) 0*sin(1.5*t); %external force
  9.  
  10. %calculates the next height/velocity/acceleration
  11. nh=@(h,v) h+v*dt;
  12. nv=@(v,a) v+a*dt;
  13. na=@(h,v,t) (1/A)*(F(t)-B*v-C*h);
  14.  
  15. %initial conditions
  16. h0=1;
  17. v0=0;
  18. a0=0;
  19. Nt=500000;
  20.  
  21. %coding shit to prepare our 'table' of values
  22. t=[0:dt:dt*Nt];
  23. h=zeros(Nt+1,1);
  24. v=zeros(Nt+1,1);
  25. a=zeros(Nt+1,1);
  26.  
  27. %putting the initial conditions into the table
  28. h(1)=h0;
  29. v(1)=v0;
  30. a(1)=a0;
  31.  
  32. %calculating the next height/velocity/acceleration at each time
  33. for time=2:Nt+1
  34.     h(time)=nh(h(time-1),v(time-1));
  35.     v(time)=nv(v(time-1),a(time-1));
  36.     a(time)=na(h(time),v(time),t(time));
  37. end
  38.  
  39. %plot the result!
  40. figure(1)
  41. clf
  42. plot(t,h,'k')
  43. %the real solution we calced to compare (for A=m, B=0, C=0, F=-mg)
  44. %hreal=(-g/2)*(t.^2)+v0*t+h0;
  45. %hold on
  46. %plot(t,hreal,'r')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement