diff options
| Commit | c2fde66ef82a6fed1f70ef3902d0932df4723fe4 (patch) | |
|---|---|---|
| Author | 2023-12-05 16:25:26 +0200 | |
| Committer | 2023-12-05 16:25:26 +0200 | |
| Files | 91cfc55b33d1ca8b1596e844bebc7e7a182ff20c | |
| uni-projects-c2fde66ef82a6fed1f70ef3902d0932df4723fe4.tar uni-projects-c2fde66ef82a6fed1f70ef3902d0932df4723fe4.tar.gz uni-projects-c2fde66ef82a6fed1f70ef3902d0932df4723fe4.zip | ||
| Parent | 39a4bd3d1b70e4d800487b79860f9113fb079f84 (diff) | |
[NA] Added initial solution
1 files changed, 78 insertions, 0 deletions
| -rw-r--r-- | Year 3/Numerical analysis/Project.wl | 78 |
diff --git a/Year 3/Numerical analysis/Project.wl b/Year 3/Numerical analysis/Project.wl new file mode 100644 index 0000000..9948423 --- /dev/null +++ b/Year 3/Numerical analysis/Project.wl @@ -0,0 +1,78 @@ +(*---------- +* 1 задача +*----------*) +ApproxIntegral[M_,a_,b_,f_]:= With[ + { + h=(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}] +] + +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}]] + +(*---------- +* 2 задача +*----------*) + +(* Намиране Pn *) +Pncoeff [xs_,f_]:= With[ + { + k=Length[xs], + x0=First[xs], + xk=Last[xs] + }, + Piecewise[{ + { + f[xk], + k==1 + }, + { + (Pncoeff[Drop[xs,1],f]-Pncoeff[Drop[xs,-1],f])/(xk-x0), + x0!=xk + }, + { + D[f,{x,k}][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]} + ]] +] + +f[x_]:=Piecewise[{ + {Cos[3x],x<=Pi}, + {x-Pi-1,x>Pi} +}] +values:={9Pi/10,10Pi/11,11Pi/10} +newtonpolyn:=Pn[f,values] + +Collect[FullSimplify[InterpolatingPolynomial[{ + {9Pi/10,f[9Pi/10]}, + {10Pi/11,f[10Pi/11]}, + {11Pi/10,f[11Pi/10]} +},x]],x] + +Print["2 задача)"] +Print["Полином: ",Collect[FullSimplify[newtonpolyn[x]],x]] + +(* Графика разлика *) +Print["Графика на разлика:"] +Plot[Abs[f[z]-newtonpolyn[z]],{z,-2Pi,2Pi}] |
