Aproksimacija funkcije

L_2  kontinuirana aproksimacija

f[t_]:=Exp[2t]; 
(*f[t_]:=If[t<1/2,-1,1];
f[t_]:=2t^4-t *)
n=2;  (* aproksimacija pomocu n baznih funkcija *)
a=0;b=1;
                                 (* definiranje baznih funkcija *)
(* mat=Table[Integrate[t^(i+j-2),{t,0,1}],{i,n},{j,n}];
y=Table[Integrate[t^(i-1)f[t],{t,0,1}],{i,n}];*)
u[1,t_]:=1; u[2,t_]:=t;   
mat=Table[Integrate[u[i,t]u[j,t],{t,0,1}],{i,n},{j,n}];
y=Table[Integrate[u[i,t]f[t],{t,0,1}],{i,n}];
Print["Gramm = ",MatrixForm[mat],"   y = ",MatrixForm[y]," = ", MatrixForm[N[y]]];
Print["cond[Gramm]=",Norm[N[mat]] Norm[Inverse[mat]//N]]
x=Inverse[mat].y//N;
Print["parametri funkcije phi =",x]
                                                    (* Slike *)
phi[t_]:=Sum[x[[i]] t^(i-1),{i,n}];
sl1=Plot[{f[t],phi[t]},{t,a,b},Filling->{1->{2}},PlotLabel->"L_2 pogreska"];
sl2=Plot[f[t]-phi[t],{t,a,b},PlotLabel->"L_1 pogreska"];
Show[GraphicsArray[{sl1,sl2}]]
Print["d_2(f,phi)=",Sqrt[Integrate[(f[t]-phi[t])^2,{t,a,b}]//N]]

Ortogonalni sustavi

Plot[Sin[2x] Cos[5x],{x,-Pi,Pi}]

Integrate[Sin[13t]^2,{t,-Pi,Pi}]

Fourierov polinom

f[x_]:=x
(* f[x_]:=If[x<0,-1,1]
f[x_]:=If[x<Pi,x/Pi,1] *)
slf=Plot[f[x],{x,-Pi,Pi}];

n=2; (* stupanj Fourirerovog polinoma *)
Clear[x]
a0=(1/Pi)Integrate[f[x],{x,-Pi,Pi}];
a=Table[(1/Pi)Integrate[f[x] Cos[k x],{x,-Pi,Pi}],{k,n}];
b=Table[(1/Pi)Integrate[f[x] Sin[k x],{x,-Pi,Pi}],{k,n}];
Print["a0=",a0,"  a=",MatrixForm[a],"   b=",MatrixForm[b]]
F[x_]:=a0/2+Sum[a[[k]] Cos[k x]+b[[k]] Sin[k x],{k,n}]
slF=Plot[F[x],{x,-Pi,Pi}];
sl1=Show[slf,slF];
sl2=Plot[{f[x],F[x]},{x,-Pi,Pi},Filling->{1->{2}}];
Show[GraphicsArray[{sl1,sl2}]]
Print["d_2(f,phi)=",Sqrt[Integrate[(f[t]-F[t])^2,{t,0,1}]//N]]

Drugi nacin Mathematica-definiranja Fourierovih koeficijenata

Clear[x,a,b,fn]
(* f[x_]=1-Abs[x-1]; *)
f[x_]:=Abs[x]; n=2;
a[k_]:=(1/Pi)Integrate[f[x]Cos[k x],{x,-Pi,Pi}];
b[k_]:=(1/Pi)Integrate[f[x]Sin[k x],{x,-Pi,Pi}];

koefa=Table[a[k],{k,n}]; 
Print["a=",koefa];
koefb=Table[b[k],{k,n}]; 
Print["b=",koefb];
fn[x_]:=a[0]/2+koefa[[1]]Cos[x]+koefb[[1]]Sin[x]
Plot[{f[x],fn[x]},{x,-Pi,Pi},Filling->{1->{2}}]

Gramm-Schmidtov postupak ortogonalizacije polinoma

fi[i_,x_]:=x^(i-1);
om[t_]:=1; a=-1;     b=1;    (* Legendre-ovi polinomi *)
(*
om[t_]:=1/Sqrt[1-t^2]; a=-1;     b=1;    (* Chebyshev-ljevi polinomi *)
om[t_]:=Exp[-t]; a=0;  b=Infinity;     (* Laguerre-ovi polinomi *)
om[t_]:=Exp[-t^2]; a=-Infinity;  b=Infinity;     (* Hermite-ovi polinomi *)
*)
n=6;
Do[
	psi[i,t]=fi[i,t]-
			Sum[Integrate[om[t]fi[i,t] psi[j,t],{t,a,b}]psi[j,t]/Integrate[om[t] psi[j,t]^2,{t,a,b}],{j,i-1}];
		,{i,n}]
ortpol=Table[{i-1,Simplify[psi[i,t]]},{i,n}];
Print[TableForm[ortpol]]

- Po cemu se razlikuju polinomi dobiveni naredbom LegendreP[n,t], odnosno LaguerreL[n,t], odnosno HermiteH[n,t], od polinoma psi[n,t]dobivenih prthodnim programom 
- Nactajte grafove ovih polinoma

Chebyshevljevi polinomi

Do[Print["T",k,"=",ChebyshevT[k,t]],{k,n}]

Plot[{ChebyshevT[6,t]},{t,-1,1}];

Najbolja L_\infty aproksimacija

a=1; b=1;
f[x_]:=x^2; fi[x_]:=a+b x
sl1=Plot[{f[x],fi[x]},{x,-1,1},DisplayFunction->Identity];
sl2=ListPlot[Table[{xk,Abs[f[xk]-fi[xk]]},{xk,-1,1,.05}],PlotStyle->AbsolutePointSize[4]];
Show[GraphicsArray[{sl1,sl2}]]
Max[Table[Abs[f[xk]-fi[xk]],{xk,-1,1,.01}]]

n=5;
Plot[{x^n,x^n-2^(1-n)ChebyshevT[n,x]},{x,-1,1},PlotRange->{-1,1}]
x^n-2^(1-n)ChebyshevT[n,x]//Simplify