SonicDesu

Adams RR

Jan 13th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.90 KB | None | 0 0
  1. clc;
  2. clear all;
  3. close all;
  4.  
  5.  
  6. yk=1; % krok początkowy
  7. xk=0; %krok początkowy
  8. eps=0.01; %dokładność
  9. h=0.05; %krok
  10. f = @(x,y) 2*y*x; %podana funkcja
  11. k=0;
  12.  
  13. while(k<4) %algorytm Rungego-Kutty
  14.     s1=h*f(xk,yk);
  15.     s2=h*f(xk+(1/2*h),yk+(1/2*s1));
  16.     s3=h*f(xk+(1/2*h),yk+(1/2*s2));
  17.     s4=h*f(xk+h,yk+s3);
  18.     T=abs((s2-s3)/(s1-s2));
  19.     wyn(k+1,:)=[k, xk, yk, s1, s2 ,s3, s4, T, h];
  20.     if(T<=0.05)
  21.         wyn(k+2,3)=wyn(k+1,3)+(1/6*s1)+(1/3*s2)+(1/3*s3)+(1/6*s4);
  22.     else
  23.         h=h/2;
  24.         continue
  25.     end
  26.     yk=wyn(k+2,3);
  27.     k=k+1;
  28.     xk=xk+h;
  29. end
  30.  
  31. k=4;
  32. xk=0.2;
  33.  
  34. while(xk<=1+h) %algorytm Adamsa
  35.     k=k+1;
  36.     wyn(k,1)=k-1;
  37.     wyn(k,2)=xk;
  38.     wyn(k,3)=wyn(k-1,3)+(h/24)*((55*f(wyn(k-1,2),wyn(k-1,3)))-(59*f(wyn(k-2,2),wyn(k-2,3)))+(37*f(wyn(k-3,2),wyn(k-3,3)))-(9*f(wyn(k-4,2),wyn(k-4,3))));
  39.     xk=xk+h;
  40. end
  41.  
  42. fprintf('Kolejno: k, x, y, s1...s4, T, h');
  43. wyn
Advertisement
Add Comment
Please, Sign In to add comment