diff options
| Commit | cc5fe29ae501e4926d810a46f2036dba272c32fc (patch) | |
|---|---|---|
| Author | 2023-12-05 17:41:59 +0200 | |
| Committer | 2023-12-05 17:41:59 +0200 | |
| Files | f72450c1532701c9a44306a79c0eabe21e16c1d3 | |
| uni-projects-cc5fe29ae501e4926d810a46f2036dba272c32fc.tar uni-projects-cc5fe29ae501e4926d810a46f2036dba272c32fc.tar.gz uni-projects-cc5fe29ae501e4926d810a46f2036dba272c32fc.zip | ||
| Parent | c2fde66ef82a6fed1f70ef3902d0932df4723fe4 (diff) | |
[NA] Reformatting and updated logic
1 files changed, 37 insertions, 33 deletions
| -rw-r--r-- | Year 3/Numerical analysis/Project.wl | 70 |
diff --git a/Year 3/Numerical analysis/Project.wl b/Year 3/Numerical analysis/Project.wl index 9948423..a3ac02a 100644 --- a/Year 3/Numerical analysis/Project.wl +++ b/Year 3/Numerical analysis/Project.wl @@ -1,33 +1,33 @@ (*---------- * 1 задача *----------*) -ApproxIntegral[M_,a_,b_,f_]:= With[ +ApproxIntegral[M_, a_, b_, f_] := With[ { h=(b-a)/(2M), - xk=Function[k,a+k*(b-a)/(2M)] + xk=Function[k, a+k*(b-a)/(2M)] }, (h/3)(f[a]+f[b]) + - (2h/3)Sum[f[xk[2k]],{k,1,M-1}]+ - (4h/3)Sum[f[xk[2k-1]],{k,1,M}] + (2h/3)Sum[f[xk[2k]], {k, 1, M-1}]+ + (4h/3)Sum[f[xk[2k-1]], {k, 1, M}] ] -Integrand[x_]:=1/((1+2x^2)^5) -m15:=N[ApproxIntegral[15,-1,1,Integrand],{Infinity,20}] -m50:=N[ApproxIntegral[50,-1,1,Integrand],{Infinity,20}] -correct := N[Integrate[Integrand[x],{x,-1,1}],{Infinity,20}] +Integrand[x_] := 1/((1+2x^2)^5) +m15 := N[ApproxIntegral[15, -1, 1, Integrand], {Infinity, 20}] +m50 := N[ApproxIntegral[50, -1, 1, Integrand], {Infinity, 20}] +correct := N[Integrate[Integrand[x], {x, -1, 1}], {Infinity, 20}] Print["1 задача)"] -Print["При M=15: ",m15] -Print["Разлика с точен: ", N[Abs[m15-correct],{Infinity,20}]] -Print["При M=50: ",m50] -Print["Разлика с точен: ", N[Abs[m50-correct],{Infinity,20}]] +Print["При M=15: ", m15] +Print["Разлика с точен: ", N[correct-m15, {Infinity, 20}]] +Print["При M=50: ", m50] +Print["Разлика с точен: ", N[correct-m50, {Infinity, 20}]] (*---------- * 2 задача *----------*) (* Намиране Pn *) -Pncoeff [xs_,f_]:= With[ +Pncoeff [xs_, f_] := With[ { k=Length[xs], x0=First[xs], @@ -39,40 +39,44 @@ Pncoeff [xs_,f_]:= With[ k==1 }, { - (Pncoeff[Drop[xs,1],f]-Pncoeff[Drop[xs,-1],f])/(xk-x0), + (Pncoeff[Drop[xs, 1], f]-Pncoeff[Drop[xs, -1], f])/(xk-x0), x0!=xk }, { - D[f,{x,k}][x0]/((k-1)!), + (D[f[x], {x, k-1}]/.x->x0)/((k-1)!), x0==xk }}] ] -Pn[f_,xsWithDup_]:=With[ - { xs = DeleteDuplicates[xsWithDup] }, - Function[x,Sum[ - Pncoeff[Take[xs,i],f]* - Product[x-xs[[j]],{j,1,i-1}], - {i,1,Length[xs]} +Pn[f_, xsWithDup_] := With[ + { xs = xsWithDup }, + Function[x, Sum[ + FullSimplify[Pncoeff[Take[xs, i], f]* + Product[x-xs[[j]], {j, 1, i-1}]], + {i, 1, Length[xs]} ]] ] -f[x_]:=Piecewise[{ - {Cos[3x],x<=Pi}, - {x-Pi-1,x>Pi} +f[x_] := Piecewise[{ + {Cos[3x], x<=Pi}, + {x-Pi-1, x>Pi} }] -values:={9Pi/10,10Pi/11,11Pi/10} -newtonpolyn:=Pn[f,values] +values := {9Pi/10, 9Pi/10, 9Pi/10, 10Pi/11, 10Pi/11, 11Pi/10} -Collect[FullSimplify[InterpolatingPolynomial[{ - {9Pi/10,f[9Pi/10]}, - {10Pi/11,f[10Pi/11]}, - {11Pi/10,f[11Pi/10]} -},x]],x] + +newtonpolyn := Pn[f, values] Print["2 задача)"] -Print["Полином: ",Collect[FullSimplify[newtonpolyn[x]],x]] +Print["Полином: ", newtonpolyn[x]] + +(* +InterpolatingPolynomial[{ + {9Pi/10, f[9Pi/10]}, + {10Pi/11, f[10Pi/11]}, + {11Pi/10, f[11Pi/10]} +}, x] +*) (* Графика разлика *) Print["Графика на разлика:"] -Plot[Abs[f[z]-newtonpolyn[z]],{z,-2Pi,2Pi}] +Plot[Abs[f[z]-newtonpolyn[z]], {z, -2Pi, 2Pi}] |
