Advertisement
SonicDesu

Runge-Kutt RR

Jan 13th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.91 KB | None | 0 0
  1. clear;clc;close all;
  2.  
  3. h = 0.1; %krok początkowy
  4. %precision = @(x) 0.2*exp(sin(x)+2*cos(x))/exp(x*x); %Rozwiązanie równania
  5. f = @(x,y) 2*x*y; %Fukcja dana w zadaniu
  6. w = zeros(1,4);
  7. k = zeros(1,4); %współczynniki s
  8.  
  9.  
  10. w(1) = 1/6;
  11. w(4) = 1/6;
  12. w(2) = 1/3;
  13. w(3) = 1/3;
  14.  
  15. y=1;
  16. x=0;
  17.  
  18. display(['y(' num2str(x) ')=' num2str(y) '  h=' num2str(h)]);
  19.  
  20. while 1
  21.    h = h*2;
  22.    while 1
  23.        h=h/2;
  24.        k(1) = h*f(x,y);
  25.        k(2) = h*f(x+h/2, y+k(1)/2);
  26.        k(3) = h*f(x+h/2, y+k(2)/2);
  27.        k(4) = h*f(x+h,y+k(3));
  28.        y1=y;
  29.        
  30.        for i=1:4
  31.            y1=y1+w(i)*k(i);
  32.        end
  33.        T = (k(2)-k(3))/(k(1)-k(2));
  34.        if(T<0)
  35.            T=-T;
  36.        end
  37.        if(T<0.05)
  38.            break;
  39.        end
  40.    end
  41.    
  42.     y=y1;
  43.     x=x+h;
  44.     display(['y(' num2str(x) ')=' num2str(y) '  h=' num2str(h) ' T=' num2str(T)]);
  45.     if(x>=1)
  46.         break;
  47.     end
  48. end
  49. %by MJ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement