Daha

Mar 10th, 2022 (edited)
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. %main
2. %задание 1
3. a=-pi; b=pi; ns=[5 10 15 20 25 30 35 50 70 100 200 500 1000]; lambda=3/(10*pi);
4. K=@(x,s) 1./(0.64*cos((x+s)/2).*cos((x+s)/2)-1);
5. f=@(x) 25-16*sin(x).*sin(x);
6. u=@(x) 17/2+(128/17)*cos(2*x);
7. for i=1:length(ns)
8.     x=linspace(a,b,ns(i));
9.     h(i)=x(2)-x(1);
10.     [y,x]=fredII_trapez(K,f,lambda,a,b,ns(i));
11.     u_=u(x)';
12.     err(i)=norm(y-u_,inf)/norm(u_,inf);
13. end
14. loglog(h,err)
15. title('loglog(h,err)');
16. xlabel('h');
17. ylabel('err');
18.
19. %задание 2
20. %main
21. % a=0; b=1; lambda = 1/2 ;n=1000;
22. % f=@(x) (5/6)*x;
23. % K=@(x,s) x*s;
24. % x=linspace(a,b,n);
25. % u=@(x) x;
26. % tic;
27. % [y,x]=fredII_trapez(K,f,lambda,a,b,n);
28. % toc;
29. % % tic;
30. % % [y,x]=fredII_trapez_upd(K,f,lambda,a,b,n);
31. % % toc;
32. % plot(x,y,'ro',x,u(x),'g');
33. % xlabel('x');
34. % ylabel('y');
35. % u_=u(x)';
36. % err=norm(y-u_,inf)/norm(u_,inf);
37.
38. для задания 3
39.
40. function [y,x] = fredII_trapez_upd(K,f,lambda,a,b,n)
41. x=linspace(a,b,n);
42. h=x(2)-x(1);
43. w=[1/2,ones(1,n-2), 1/2]*h*lambda;
44. %A=-K(x,x')*diag(w')+eye(n);
45. A=-K(x,x')*spdiags(w',0,n,n)+speye(n);
46. F=f(x);
47. y=A\F'
48. end
49.
50. % %задание 4
51. % %main
52. % a=0; b=1; ns=[5 10 15 20 25 30 50 100]; lambda=-1;
53. % K=@(x,s) x.*exp(x.*s);
54. % f=@(x) exp(x);
55. % for i=1:length(ns)
56. %     x=linspace(a,b,ns(i));
57. %     h(i)=x(2)-x(1);
58. %     [y1,x]=fredII_trapez(K,f,lambda,a,b,ns(i));
59. %     [y2,x]=fredII_simpson(K,f,lambda,a,b,ns(i));
60. %     u=@(x) ones(1,numel(x));
61. %     u_=u(x)';
62. %     err1(i)=norm(y1-u_,inf)/norm(u_,inf);
63. %     err2(i)=norm(y2-u_,inf)/norm(u_,inf);
64. % end
65. % %plot(h,err1,'r');
66. % loglog(h,err1,'r')
67. % hold on
68. % %plot(h,err2,'g');
69. % loglog(h,err2,'g')
70. % title('red - trapez, green - simpson');
71. % xlabel('h');
72. % ylabel('err');
73.
74. function [y,x] = fredII_simpson(K,f,lambda,a,b,n)
75. x=linspace(a,b,n)';
76. h=x(2)-x(1);
77. if mod(n,2)==1
78.     w=[1/3,2/3*(1+mod(1:(n-2),2)),1/3]*h*lambda;
79. else
80.     w=[1/2,5/6,2/3*(1+mod(1:(n-3),2)),1/3]*h*lambda;
81. end
82. A=zeros(n);
83. for i=1:n
84.     A(:,i)=-w(i)*K(x,x(i));
85.     A(i,i)=A(i,i)+1;
86. end
87. F=f(x);
88. y=A\F;
89. end