Advertisement
Randomsurpriseguy

Blatt4_13

May 14th, 2020
3,308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.44 KB | None | 0 0
  1. % B4A13 geschrieben für Octave, Adams Moulton, Zeitvariable x
  2. %Exakte Lösung ist y(x)= e^(x-x^2) (trivial), damit erhalten wir y(1,5)=0.47237
  3.  
  4.  
  5. function Blatt3_13 %B4A13
  6.     Moulton()
  7.     exakt()
  8.     BDF()
  9. endfunction
  10.  
  11. %--------------------------Moulton-----------------------------------
  12. function Moulton
  13.  
  14. f = inline('(1-2*x)*y','x','y');
  15. h=0.1;
  16. x=  1 : h : 1.5  ;  
  17. y = x;
  18. y(1)=1;
  19. % Startwerte suchen (Runge-Kutta)
  20.  
  21.     k1 = f(x(1), y(1));
  22.     k2 = f(x(1) + h/2, y(1) + h/2*k1);
  23.    
  24.     y(2) = y(1) + h*k2;
  25.  
  26. % Iterieren:
  27. for i = 3:length(y)
  28.     % Adams-Moulton
  29.   % Löse y(i) - (y(i-1) + h/12*(5*f(x(i),y(i)) + 8*f(x(i-1),y(i-1))- f(x(i-2),y(i-2))))=0;
  30.   % Löse y(i) + h/12*5*f(x(i),y(i)) - (y(i-1)  + h/12 (8*f(x(i-1),y(i-1))- f(x(i-2),y(i-2))))=0;
  31.   %Löse nach y(i) mit Sekantenverfahren, Startwerte aus Runge Kutta
  32.     %Vereinfachung des Terms
  33.   c= h/12*(8*f(x(i-1),y(i-1))- f(x(i-2),y(i-2)))+y(i-1); %Konstanter Wert
  34.  
  35.   y(i)=approx(y(i-2),y(i-1),@(t) t - h/12 * 5* f(x(i),t)-c);
  36.  
  37. endfor
  38. y
  39. disp('Damit ist der Wert an der Stelle 1.5:')
  40. y(length(y))
  41.  
  42. figure('Name','Moulton-Plot','NumberTitle','off')
  43. plot(x,y)
  44. title ("Ergebnis mit Moulton");
  45.  
  46.  
  47. endfunction
  48.  
  49. %------------------------------Exakte Lösung----------------------
  50. function exakt
  51. h=0.1;
  52. x=1:h:1.5;
  53. y=arrayfun(@(t) e^(t-t^2), x);
  54. figure('Name','exakter Plot','NumberTitle','off')
  55. disp('Exakte Lösung ist y(x)= e^(x-x^2) (trivial), damit erhalten wir y(1,5)=0.47237')
  56. plot(x,y)
  57. title("Exakte Lösung");
  58.  
  59. endfunction
  60.  
  61. %-------------------------BDF-Formel---------------------------------------
  62. function BDF
  63.  
  64.  
  65. f = inline('(1-2*x)*y','x','y');
  66. h=0.1;
  67. x=  1 : h : 1.5  ;  
  68. y = x;
  69. y(1) = 1;
  70. % Startwerte suchen (Runge-Kutta)
  71.  
  72.     k1 = f(x(1), y(1));
  73.     k2 = f(x(1) + h/2, y(1) + h/2*k1);
  74.    
  75.     y(2) = y(1) + h*k2;
  76.  
  77. % Löse auf: 0= 2/3*h*f(x(i),y(i)) - y(i) -(-4/3 y(i-1)+1/3*y(i-2)). Setze:
  78. for i=3:length(y)
  79.     c= (-4/3*y(i-1)+1/3*y(i-2))
  80.     y(i)=approx(y(i-2),y(i-1),@(t) 2/3*h*f(x(i),t)-t-c)
  81.    
  82.  
  83.  
  84.  
  85. endfor
  86. y
  87. disp('Damit ist der Wert an der Stelle 1.5:')
  88. y(length(y))
  89.  
  90. figure('Name','BDF-Plot','NumberTitle','off')
  91. plot(x,y)
  92. title('BDF Lösung');
  93.  
  94. endfunction
  95.  
  96. %-----------------------------------
  97. function r=approx(in1,in2,g)
  98.   %test=1
  99.   while abs(g(in2))>0.00001
  100.     in1=in2-(in2-in1)/(g(in2)-g(in1)) * g(in2);
  101.     in2=in1-(in1-in2)/(g(in1)-g(in2)) * g(in1);
  102.     %test=test+1
  103.   endwhile
  104.   r=in2;
  105. endfunction
  106.  
  107. %erstellt mit Tobias Post/Philipp Heering
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement