Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. %Problem 2 - Computer Problem section 8.3
  2. clear;
  3. clc;
  4. x0 = 0;
  5.  
  6. a = 0;
  7. b = 5;
  8.  
  9. f1 = @(x,t)5*x + cos(t) -5*sin(t);
  10. f2 = @(y,t)-5*y + cos(t) +5*sin(t);
  11. f3 = @(z,t)-10*z + cos(t) +10*sin(t);
  12.  
  13. %
  14. t(1) = [0];
  15. x(1) = [x0];
  16. y(1) = [x0];
  17. z(1) = [x0];
  18.  
  19. h = 0.01;
  20. n = round((b-a)/h);
  21. for i=1:n
  22. t(i+1) = t(i) + h;
  23. k1x = f1(x(i),t(i));
  24. k2x = f1(t(i) + h/2,x(i) + h/2*k1x);
  25. k3x = f1(t(i) + h/2,x(i) + h/2*k2x);
  26. k4x = f1(t(i) + h/2,x(i) + h/2*k3x);
  27.  
  28. j1x = f2(y(i),t(i));
  29. j2x = f2(t(i) + h/2,x(i) + h/2*j1x);
  30. j3x = f2(t(i) + h/2,x(i) + h/2*j2x);
  31. j4x = f2(t(i) + h/2,x(i) + h/2*j3x);
  32.  
  33. p1x = f3(z(i),t(i));
  34. p2x = f3(t(i) + h/2,x(i) + h/2*p1x);
  35. p3x = f3(t(i) + h/2,x(i) + h/2*p2x);
  36. p4x = f3(t(i) + h/2,x(i) + h/2*p3x);
  37.  
  38. x(i+1) = x(i) - h/6*(k1x+2*k2x+k2x*k3x+k4x);
  39. y(i+1) = y(i) - h/6*(j1x+2*j2x+j2x*j3x+j4x);
  40. z(i+1) = z(i) - h/6*(p1x+2*p2x+p2x*p3x+p4x);
  41.  
  42.  
  43. end
  44. plot(t,x,y,z)
  45. hold on
  46. xlabel('Time')
  47. ylabel('Solution')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement