Advertisement
Catalin_Mema

Untitled

Apr 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. //GaussSeidel
  2.  
  3.  
  4. function GaussSeidel(A,b,E)
  5. [n,m]=size(A);
  6. if n~=m
  7. disp('Matricea sistemului nu este patratica')
  8. return
  9. end
  10. contor=0;
  11. % verificarea diagonal dominantei:
  12. for i=1:n
  13. suma=0;
  14. for j=1:n
  15. suma=suma+abs(A(i,j));
  16. end
  17. if(A(i,i)==0)
  18. disp('Metoda nu se aplica');
  19. return;
  20. end
  21. if suma<abs(A(i,i))
  22. contor=contor+1;
  23. end
  24. end
  25.  
  26. if contor==n
  27. disp('Matricea e diagonal dominanta=> Se poate alpica metoda')
  28. else
  29. disp('Matricea NU este diagonal dominanta => NU se poate aplica metoda')
  30. return;
  31. end
  32.  
  33. P=0;Q=0;
  34. %metoda iterativa
  35. X=zeros(n,1);
  36. error=ones(n,1);
  37. iteratie=0;
  38. while max(error)>E %conditia de terminare a procesului
  39. iteratie=iteratie+1
  40. Z=X; %salvarea valorii curente pentru a cacula eroarea mai tarziu
  41. for i=1:n
  42. P=0;Q=0; % cele 2 sume din formula
  43. X1=X; %salvam solutiile precedente
  44. for j=1:(i-1)
  45. P=P+A(i,j)*X(j);% prima suma din formula
  46. end
  47. for k=(i+1):n % a doua suma din formula
  48. Q=Q+A(i,k)*X1(k);
  49. end
  50. X(i)=(b(i)-P-Q)/A(i,i); %noile valori ale lui X
  51. end
  52. Xsol(:,iteratie)=X;
  53. error=sqrt((X-Z).^2) %calculul erorilor
  54. end
  55. disp('Solutia sistemului:')
  56. X=Xsol(:,iteratie)
  57.  
  58. disp('precizia solutiilor calculate:')
  59. X_exact=inv(A)*b
  60. n=norm(X-X_exact);
  61. end
  62. /////////////////////////////////////////
  63. A=[4 3 -1; -1 1 1;1 0 3];
  64. if det(A)~=0
  65. b=[2;0;-1];
  66. X=inv(A)*b
  67. else
  68. disp('Sistemul nu este compatibil determinat.');
  69. end
  70. //////////////////////////////////////////
  71.  
  72. //Runge Kutta
  73.  
  74. Metoda Runge-Kutta de ordin 2 pentru ecuații diferențiale de ordin I
  75. function[x,y]=rk2(f1,y0,n,a,b)
  76. clc;
  77. I=[a b];
  78. h=(I(2)-I(1))/n;
  79. x=zeros(n,1);
  80. y=zeros(n,1);
  81. pas=1;
  82. x(pas)=I(1);
  83. y(pas)=y0(1);
  84.  
  85. while x(pas)<=I(2)
  86. k1=feval('f1',x(pas),y(pas));
  87. k2=feval('f1',x(pas)+h,y(pas)+h*k1);
  88. y(pas+1)=y(pas)+((k1+k2)/2)*h;
  89. x(pas+1)=x(pas)+h;
  90. pas=pas+1;
  91. end;
  92.  
  93. plot(x,y,'y-');
  94.  
  95.  
  96.  
  97. Metoda Runge-Kutta de ordin 2 pentru sistem de ecuații diferențiale
  98.  
  99. function [x,y,z]=rk2(f,g,y0,n,a,b)
  100. I=[a b];
  101. h=(I(2)-I(1))/n;
  102. x=I(1):h:I(2);
  103. y=[];
  104. z=[];
  105. x(1)=I(1);
  106. y(1)=y0(1);
  107. z(1)=y0(2);
  108. pas=2;
  109.  
  110. while pas<=length(x)
  111. k1=feval('f',x(pas-1),y(pas-1),z(pas-1));
  112. l1=feval('g',x(pas-1),y(pas-1),z(pas-1));
  113. k2=feval('f',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
  114. l2=feval('g',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
  115. y(pas)=y(pas-1)+((k1+k2)/2)*h;
  116. z(pas)=z(pas-1)+((l1+l2)/2)*h;
  117. pas=pas+1;
  118. end;
  119. x
  120. y
  121. z
  122.  
  123. plot(x,y,'b-',x,z,'r--');
  124.  
  125.  
  126.  
  127. Metoda Runge-Kutta de ordin 3 pentru ecuații diferențiale de ordin I
  128. function [x,y]=rk3(f,y0,n,a,b)
  129. clc;
  130. I=[a b];
  131. h=(I(2)-I(1))/n;
  132. x = zeros(n,1);
  133. y = zeros(n,1);
  134. pas=1;
  135. x(I)=I(1);
  136. y(pas)=y0(1);
  137.  
  138. while x(pas)<=I(2)
  139. k1=feval('f',x(pas),y(pas));
  140. k2=feval('f',x(pas)+0.5*h,y(pas)+0.5*h*k1);
  141. k3=feval('f',x(pas)+h,y(pas)+2*h*k2-h*k1);
  142. y(pas+1)=y(pas)+(h*(k1+4*k2+k3))/6;
  143. x(pas+1)=x(pas)+h;
  144. pas=pas+1;
  145. end;
  146.  
  147. plot(x,y,'r-');
  148. Metoda Runge-Kutta de ordin 3 pentru sistem de ecuații diferenţiale
  149.  
  150. function [x,y,z]=rk3s(g,h,y0,n,a,b)
  151. clc
  152. I=[a b]
  153. h=(I(2)-I(1))/n;
  154. x=I(1):h:I(2);
  155. y=[];
  156. z=[];
  157. x(1)=I(1);
  158. y(1)=y0(1);
  159. z(1)=y0(2);
  160. pas=2;
  161. while pas<=length(x)
  162. k1=feval('g',x(pas-1),y(pas-1),z(pas-1));
  163. l1=feval('h',x(pas-1),y(pas-1),z(pas-1));
  164. k2=feval('g',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
  165. l2=feval('h',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
  166. k3=feval('g',x(pas-1)+h,y(pas-1)+2*h*k2-(h*k1),z(pas-1)+2*h*l2-(h*l1));
  167. l3=feval('h',x(pas-1)+h,y(pas-1)+2*h*k2-(h*k1),z(pas-1)+2*h*l2-(h*l1));
  168. y(pas)=y(pas-1)+(h*(k1+4*k2+k3))/6;
  169. z(pas)=y(pas-1)+(h*(11+4*l2+l3))/6;
  170. pas=pas+1;
  171. end;
  172. x
  173. y
  174. z
  175. plot(x,y,'b-',x,z,'r--');
  176.  
  177.  
  178.  
  179. Metoda Runge-Kutta de ordin 4 pentru ecuații diferențiale de ordin I
  180. function [x,y]=rk4(f,y0,n,a,b)
  181. clc;
  182. I=[a b]
  183. h=(I(2)-I(1))/n;
  184. x =I(1):h:I(2);
  185. y = [];
  186. x(1)=I(1);
  187. y(1)=y0(1);
  188.  
  189.  
  190. for i=1:(length(x)-1)
  191. k1=feval('f',x(i),y(i));
  192. k2=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k1));
  193. k3=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k2));
  194. k4=feval('f',x(i)+h,y(i)+h*k3);
  195. y(i+1)=y(i)+(h*(k1+2*k2+2*k3+k4))/6;
  196. x(i+1)=x(i)+h;
  197. end;
  198.  
  199. plot(x,y,'r-')
  200. Metoda Runge-Kutta de ordin 4 pentru sistem de ecuații diferențiale
  201.  
  202. function [x,y,z]=rk2(g,h,y0,n,a,b)
  203. clc
  204. I=[a b]
  205. %calculam pasul de integrare
  206. h=(I(2)-I(1))/n;
  207. x=I(1):h:I(2);
  208. y=[];
  209. z=[];
  210. x(1)=I(1);
  211. y(1)=cond(1);
  212. z(1)=cond(2);
  213. pas=2;
  214. while pas<=length(x)
  215. k1=feval('g',x(pas-1),y(pas-1),z(pas-1));
  216. l1=feval('h',x(pas-1),y(pas-1),z(pas-1));
  217. k2=feval('g',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k1,z(pas-1)+0.5*h*l1);
  218. l2=feval('h',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k1,z(pas-1)+0.5*h*l1);
  219. k3=feval('g',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k2,z(pas-1)+0.5*h*l2);
  220. l3=feval('h',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k2,z(pas-1)+0.5*h*l2);
  221. k4=feval('g',x(pas-1)+h,y(pas-1)+h*k3,z(pas-1)+h*l3);
  222. l4=feval('h',x(pas-1)+h,y(pas-1)+h*k3,z(pas-1)+h*l3);
  223. y(pas)=y(pas-1)+(h*(k1+2*k2+2*k3+k4))/6;
  224. z(pas)=y(pas-1)+(h*(l1+2*l2+2*l3+l4))/6;
  225. pas=pas+1;
  226. end;
  227. x
  228. y
  229. z
  230. plot(x,y,'b-',x,z,'r--');
  231.  
  232.  
  233.  
  234.  
  235. Rezolvarea ecuațiilor diferențiale folosind funcțiile „ode23” și „ode45”
  236. ode23
  237. % conditia initiala
  238. tic23=tic;
  239. y0=1;
  240. % domeniul (intervalul)
  241. I=[1,2];
  242. % rezolvarea ecuatiei diferentiale
  243. [xval,yval]=ode23('f1',I,y0)
  244. t23=toc(tic23)
  245. % reprezentarea grafica a solutiei
  246. plot(xval,yval)
  247.  
  248. ode45
  249. % conditiile initiale
  250. tic45=tic;
  251. y0=[0.1; 1];
  252. % domeniul (intervalul)
  253. I=[0,1];
  254. % rezolvarea ecuatiei diferentiale
  255. [xval,yval]=ode45('f1',I,y0)
  256. t45=toc(tic45)
  257. % reprezentarea grafica a solutiei
  258. plot(xval,yval(:,1),'b',xval,yval(:,2),'r--')
  259. legend('y1','y2')
  260.  
  261.  
  262.  
  263.  
  264.  
  265. MetodaRunge-Kutta de ordin 2 pentru ecuații diferențiale de ordin I
  266. function[x,y]=rk2(f1,y0,n,a,b)
  267. clc;
  268. I=[a b];
  269. %calculăm pasul de integrare
  270. h=(I(2)-I(1))/n;
  271.  
  272. x=zeros(n,1);
  273. y=zeros(n,1);
  274.  
  275. pas=1;
  276. %începutul invervalului
  277. x(pas)=I(1);
  278. %setăm condiţia iniţială
  279. y(pas)=y0(1);
  280.  
  281. %folosim metoda Runge-Kutta de ordinul 2
  282. while x(pas)<=I(2)
  283. k1=feval('f1',x(pas),y(pas));
  284. k2=feval('f1',x(pas)+h,y(pas)+h*k1);
  285. y(pas+1)=y(pas)+((k1+k2)/2)*h;
  286. x(pas+1)=x(pas)+h;
  287. pas=pas+1;
  288. end;
  289.  
  290. %reprezentarea grafică a soluţiei
  291. plot(x,y,'y-');
  292.  
  293. Metod aRunge-Kutta de ordin 3 pentru ecuații diferențiale de ordin I
  294. function [x,y]=rk3(f,y0,n,a,b)
  295. clc;
  296. I=[a b];
  297. %calculăm pasul de integrare
  298. h=(I(2)-I(1))/n;
  299.  
  300. %alocam spaţiu pentru îmbunătăţirea timpului de execuţie
  301. x = zeros(n,1);
  302. y = zeros(n,1);
  303. pas=1;
  304. %începutul intervalului
  305. x(I)=I(1);
  306. %setarea condiţiei iniţiale
  307. y(pas)=y0(1);
  308.  
  309. %folosim Runge-Kutta de ordin 3
  310. while x(pas)<=I(2)
  311. k1=feval('f',x(pas),y(pas));
  312. k2=feval('f',x(pas)+0.5*h,y(pas)+0.5*h*k1);
  313. k3=feval('f',x(pas)+h,y(pas)+2*h*k2-h*k1);
  314. y(pas+1)=y(pas)+(h*(k1+4*k2+k3))/6;
  315. x(pas+1)=x(pas)+h;
  316. pas=pas+1;
  317. end;
  318.  
  319. %reprezentarea grafică
  320. plot(x,y,'r-');
  321.  
  322. Metoda Runge-Kutta de ordin 4 pentru ecuații diferențiale de ordin I
  323. function [x,y]=rk4(f,y0,n,a,b)
  324. clc;
  325. I=[a b]
  326. %calculăm pasul de integrare
  327. h=(I(2)-I(1))/n;
  328.  
  329. %alocare spaţiu pentru îmbunătăţirea timpului de execuţie
  330. x =I(1):h:I(2);
  331. y = [];
  332.  
  333. %începutul intervalului
  334. x(1)=I(1);
  335. %setarea condiţiei iniţiale
  336. y(1)=y0(1);
  337.  
  338. %folosim Runge-Kutta de ordin 4
  339. for i=1:(length(x)-1)
  340. k1=feval('f',x(i),y(i));
  341. k2=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k1));
  342. k3=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k2));
  343. k4=feval('f',x(i)+h,y(i)+h*k3);
  344. y(i+1)=y(i)+(h*(k1+2*k2+2*k3+k4))/6;
  345. x(i+1)=x(i)+h;
  346. end;
  347.  
  348. %reprezentare grafică
  349. plot(x,y,'r-')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement