1
2
3
4
5
6
7
8
9 clear all;
10 close all;
11 warning('off','all');
12 format long;
13 pkg load symbolic;
14 echo off;
15
16
17 function w=TheoreticalMaxError(n,b,a)
18
19
20 f = @(x) sin(x);
21
22 syms x;
23 ff = f(x);
24
25 ffdnp1 = diff(ff, x, n+1);
26 if (isequal(abs(ffdnp1),abs(cos(x))))
27 maxfunction=cos(0);
28 else
29 maxfunction=sin(pi/2);
30 endif
31 factor1=maxfunction;
32 factor2=1/factorial(n+1);
33 factor3=1/2^n;
34 factor4=((b-a)/2)^(n+1);
35 w=factor1*factor2*factor3*factor4;
36 return;
37 endfunction
38
39
40 function PlotFunction(n,x,signeddifference,absmaxerror,p)
41
42
43
44
45
46 figure (p);
47 plot(x, signeddifference, 'b');
48 hold on
49 plot(x, absmaxerror,'r');
50 hold on
51 plot(x, -absmaxerror,'r');
52 handle = legend (["n=" num2str(n) "approximation error"], ["n=" num2str(n) "theoretical max error"]);
53 legend (handle, "location", "northeastoutside");
54 legend boxoff;
55 set(gcf,'InvertHardCopy','off');
56 filename=["cheb_plot" int2str(p) ".png"];
57 axis([0,pi/2]);
58 print (gcf, "-S500,500", filename, '-dpng');
59 return
60 endfunction
61
62
63 function result = NestedPolyEval(a, x, nplus1)
64
65
66
67
68
69
70
71 result = a(1);
72 for i = 2:nplus1
73 result = result*x + a(i);
74 endfor
75 endfunction
76
77
78 function to_poly=calculatepolynomials(n)
79
80
81
82 syms x;
83 syms to;
84 to(1)=1;
85 to(2)=x;
86 for i=3:(n+1)
87 to(i)=2*x*to(i-1)-to(i-2);
88 to(i)=expand(to(i));
89 endfor
90 to_poly=zeros(n+1,n+1);
91 to_poly(1,n+1)=1;
92 for i=2:(n+1)
93 to_poly(i,n+2-i:n+1)=sym2poly(to(i));
94 endfor
95 to_poly;
96 return
97 endfunction
98
99
100 function c=getcoefficients(f,n,a,b)
101
102
103
104
105
106
107
108 k=linspace(0,n,n+1);
109
110 c=zeros(1,n+1);
111
112 x=(b+a)/2+((b-a)/2)*cos((2*k+1)*pi/(2*n+2));
113
114 c(1)=(1/(n+1))*sum(f(x));
115
116 factor=2/(n+1);
117 for j=1:n
118 summation=(0);
119 for k=0:n
120 term = f(x(k+1))*cos(j*(2*k+1)*pi/(2*n+2));
121 summation = summation + term;
122 endfor
123 summation=factor*summation;
124 c(j+1)=summation;
125 endfor
126 c;
127 return;
128 endfunction
129
130
131 function chebpoly = GetChebPoly(c,to_poly,n)
132
133
134
135
136
137 chebpoly = 0.0;
138 for i=1:(n+1)
139 chebpoly = chebpoly + (c(i) * to_poly(i,:));
140 endfor
141 return;
142 endfunction
143
144
145
146 function test()
147
148 f=@sin;
149
150
151
152
153
154
155 narray=[7,11];
156
157 a=(0);
158
159 b=(pi/2);
160
161 numtests=129;
162
163 x=(pi*[0:(numtests-1)]/256);
164 for p=1:length(narray)
165 n=narray(p);
166 nplus1=n+1;
167
168 to_poly=calculatepolynomials(n);
169
170 c=getcoefficients(f,n,a,b);
171 chebpoly=GetChebPoly(c,to_poly,n);
172 signeddifference=zeros(1,numtests);
173 t0=clock ();
174 for i=1:numtests
175
176 y2=sin(x(i));
177 x2=x(i);
178
179 parameter=(2*(x2-a)/(b-a))-1;
180
181 w=NestedPolyEval(chebpoly, parameter, nplus1);
182 difference(i)=abs((w-y2));
183 signeddifference(i)=w-y2;
184 endfor
185 elapsed_time=etime(clock (),t0);
186 maxdifference=max(difference);
187 TheoreticalAbsMaxError=TheoreticalMaxError(n,b,a);
188 fprintf('\n n=%d Maximum difference: %1.15e \n',n,maxdifference);
189 fprintf('\n n=%d Theoretical absolute maximum error: %1.15e \n',n,TheoreticalAbsMaxError);
190 PlotFunction(n,x,signeddifference,TheoreticalAbsMaxError,p);
191 endfor
192 endfunction
193
194 test()