Advertisement
codisinmyvines

task3summerpractice

May 22nd, 2022
1,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.40 KB | None | 0 0
  1. function [S_N,N] = int_left_rectangles(phi,c,d,eps)
  2. N=1;
  3. while 1
  4.     h_N1=(d-c)/N;
  5.     h_N2=(d-c)/(2*N);
  6.     z1=c:h_N1:d;
  7.     z2=c:h_N2:d;
  8.     S_N=0;
  9.     S_2N=0;
  10.     for i=1:N
  11.         S_N=S_N+phi(z1(i))*h_N1;
  12.     end
  13.     for i=1:2*N
  14.         S_2N=S_2N+phi(z2(i))*h_N2;
  15.     end
  16.     if(abs(S_N-S_2N)<=eps)
  17.         break;
  18.     end
  19.     N=N*2;
  20. end
  21. end
  22.  
  23. function [S_N,N] = int_center_rectangles(phi,c,d,eps)
  24. N=1;
  25. while 1
  26.     h_N1=(d-c)/N;
  27.     h_N2=(d-c)/(2*N);
  28.     z1=c:h_N1:d;
  29.     z2=c:h_N2:d;
  30.     S_N=0;
  31.     S_2N=0;
  32.     for i=1:N
  33.         S_N=S_N+phi((z1(i)+z1(i+1))/2)*h_N1;
  34.     end
  35.     for i=1:2*N
  36.         S_2N=S_2N+phi((z2(i)+z2(i+1))/2)*h_N2;
  37.     end
  38.     if(abs(S_N-S_2N)<=eps)
  39.         break;
  40.     end
  41.     N=N+1;
  42. end
  43. end
  44.  
  45. function [S_N,N] = int_trapez_(phi,c,d,eps)
  46. N=1;
  47. while 1
  48.     h_N1=(d-c)/N;
  49.     h_N2=(d-c)/(2*N);
  50.     z1=c:h_N1:d;
  51.     z2=c:h_N2:d;
  52.     S_N=0;
  53.     S_2N=0;
  54.     for i=1:N
  55.         S_N=S_N+h_N1/2*(phi(z1(i))+phi(z1(i+1)));
  56.     end
  57.     for i=1:2*N
  58.         S_2N=S_2N+h_N2/2*(phi(z2(i))+phi(z2(i+1)));
  59.     end
  60.     if(abs(S_N-S_2N)<=eps)
  61.         break;
  62.     end
  63.     N=N+1;
  64. end
  65. end
  66.  
  67.  
  68. function [S_N,N] = int_simpson(phi,c,d,eps)
  69. N=1;
  70. while 1
  71.     h_N1=(d-c)/N;
  72.     h_N2=(d-c)/(2*N);
  73.     z1=c:h_N1:d;
  74.     z2=c:h_N2:d;
  75.     S_N=0;
  76.     S_2N=0;
  77.     for i=1:N
  78.         S_N=S_N+(h_N1/6)*(phi(z1(i))+4*phi((z1(i+1)+z1(i))/2)+phi(z1(i+1)));
  79.     end
  80.     for i=1:2*N
  81.         S_2N=S_2N+(h_N2/6)*(phi(z2(i))+4*phi((z2(i+1)+z2(i))/2)+phi(z2(i+1)));
  82.     end
  83.     if(abs(S_N-S_2N)<=eps)
  84.         break;
  85.     end
  86.     N=N+1;
  87. end
  88. end
  89.  
  90.  
  91. function [S_N,N] = int_gauss(phi,c,d,eps)
  92. N=1;
  93. while 1
  94.     h_N1=(d-c)/N;
  95.     h_N2=(d-c)/(2*N);
  96.     z1=c:h_N1:d;
  97.     z2=c:h_N2:d;
  98.     S_N=0;
  99.     S_2N=0;
  100.     for i=1:N
  101.         S_N=S_N+(h_N1/2)*(phi(z1(i)+(h_N1/2)*(1-1/sqrt(3)))+phi(z1(i)+(h_N1/2)*(1+1/sqrt(3))));
  102.     end
  103.     for i=1:2*N
  104.         S_2N=S_2N+(h_N2/2)*(phi(z2(i)+(h_N2/2)*(1-1/sqrt(3)))+phi(z2(i)+(h_N2/2)*(1+1/sqrt(3))));
  105.     end
  106.     if(abs(S_N-S_2N)<=eps)
  107.         break;
  108.     end
  109.     N=N+1;
  110. end
  111. end
  112.  
  113. %main
  114. % phi = @(t) sin((pi*t^2)/2);
  115. % eps1=10^(-8);
  116. % a=0; b=1.2;
  117. % x=0:0.12:1.2;
  118. % [S,k] = taylor_series_task1(x,eps1);
  119. % S_N(1)=0;
  120. % N(1)=0;
  121. % for i=2:length(x)
  122. %     [S_N(i),N(i)] = int_left_rectangles(phi,0,x(i),eps1);
  123. % end
  124. % error=abs(S-S_N);
  125. % array2table([x;S;S_N;N;error]',...
  126. %     'VariableNames',{'x','S(x)','S_N(x)','N','error'})
  127. % format long
  128.  
  129. % phi = @(t) sin((pi*t^2)/2);
  130. % eps1=10^(-8);
  131. % a=0; b=1.2;
  132. % x=0:0.12:1.2;
  133. % [S,k] = taylor_series_task1(x,eps1);
  134. % S_N(1)=0;
  135. % N(1)=0;
  136. % for i=2:length(x)
  137. %     [S_N(i),N(i)] = int_center_rectangles(phi,0,x(i),eps1);
  138. % end
  139. % error=abs(S-S_N);
  140. % array2table([x;S;S_N;N;error]',...
  141. %     'VariableNames',{'x','S(x)','S_N(x)','N','error'})
  142. % format long
  143.  
  144. % phi = @(t) sin((pi*t^2)/2);
  145. % eps1=10^(-8);
  146. % a=0; b=1.2;
  147. % x=0:0.12:1.2;
  148. % [S,k] = taylor_series_task1(x,eps1);
  149. % S_N(1)=0;
  150. % N(1)=0;
  151. % for i=2:length(x)
  152. %     [S_N(i),N(i)] = int_trapez_(phi,0,x(i),eps1);
  153. % end
  154. % error=abs(S-S_N);
  155. % array2table([x;S;S_N;N;error]',...
  156. %     'VariableNames',{'x','S(x)','S_N(x)','N','error'})
  157. % format long
  158.  
  159. % phi = @(t) sin((pi*t^2)/2);
  160. % eps1=10^(-8);
  161. % a=0; b=1.2;
  162. % x=0:0.12:1.2;
  163. % [S,k] = taylor_series_task1(x,eps1);
  164. % S_N(1)=0;
  165. % N(1)=0;
  166. % for i=2:length(x)
  167. %     [S_N(i),N(i)] = int_simpson(phi,0,x(i),eps1);
  168. % end
  169. % error=abs(S-S_N);
  170. % array2table([x;S;S_N;N;error]',...
  171. %     'VariableNames',{'x','S(x)','S_N(x)','N','error'})
  172. % format long
  173.  
  174. % phi = @(t) sin((pi*t^2)/2);
  175. % eps1=10^(-6);
  176. % a=0; b=1.2;
  177. % x=0:0.12:1.2;
  178. % [S,k] = taylor_series_task1(x,eps1);
  179. % S_N(1)=0;
  180. % N(1)=0;
  181. % for i=2:length(x)
  182. %     [S_N(i),N(i)] = int_gauss(phi,0,x(i),eps1);
  183. % end
  184. % error=abs(S-S_N);
  185. % array2table([x;S;S_N;N;error]',...
  186. %     'VariableNames',{'x','S(x)','S_N(x)','N','error'})
  187. % format long
  188.  
  189. phi = @(t) sin((pi*t^2)/2);
  190. eps1=10^(-8);
  191. a=0; b=1.2;
  192. x=0:0.12:1.2;
  193. [S,k] = taylor_series_task1(x,eps1);
  194. S_N_trapez(1)=0;
  195. S_N_simpson(1)=0;
  196. S_N_gauss(1)=0;
  197. for i=2:length(x)
  198.     S_N_trapez(i) = int_trapez_(phi,0,x(i),eps1);
  199.     S_N_simpson(i)= int_simpson(phi,0,x(i),eps1);
  200.     S_N_gauss(i)= int_gauss(phi,0,x(i),eps1);
  201. end
  202. error_trapez=abs(S-S_N_trapez);
  203. error_simpson=abs(S-S_N_simpson);
  204. error_gauss=abs(S-S_N_gauss);
  205. array2table([x;error_trapez;error_simpson;error_gauss]',...
  206.     'VariableNames',{'x','Трапеции','Симпсона','Гаусса'})
  207. format long
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement