有人可以向我解释以下程序的错误消息吗?我是Mathematica的新手,无法弄清楚我给自己分配了什么错误。我该如何解决?我收到以下错误:“(1/5)[0]中的Set :: write:Tag Rational被保护”和“ General :: stop:在此计算期间,Set :: write的进一步输出将被抑制”。分配Do [delta [k] = x [k + 1]-x [k],{k,0,n-1}]可能有问题;但我找不到预先感谢您的帮助!
n = 10;
(* Nodes: *)
Do[x[k] = Sin[k * Pi/(2*n)]^2, {k, 0, n}];
(* x[0]=0, x[n]=1 *)
(* Distances between the nodes: *)
Do[delta[k] = x[k + 1] - x[k], {k, 0, n - 1}];
(* Function to interpolate with a cubic spline s(t): *)
f[t_] := Cos[2*Pi*t];
(* Divided differences divDiff[i]=f[xi,xi+1] *)
Do[divDiff[k] = (f[x[k + 1]] - f[x[k]])/delta[k], {k, 0, n - 1}];
(* We will find {Pi(t)}, which represent s(t) in (xi,xi+1):
Pi(xi)=f(xi) Pi(xi+1)=f(xi+1) i=0,...,n-1
P'i(xi)=di P'i(xi+1)=di+1 i=0,...,n-1
=> s(t) and s'(t) will be continuous on [0,1]
=> Newton's polynomial:
Pi(t)=Pi(xi)+Pi[xi,xi]*(t-xi)+Pi[xi,xi,xi+1]*(t-xi)^2+Pi[xi,xi,xi+1,xi+1]*(t-xi)^2*(t-xi+1).*)
(* Tridiagonal matrix: *)
Do[m[i] = delta[i], {i, 1, n - 1}];
Do[p[i] = 2*(delta[i - 1] + delta[i]), {i, 1, n - 1}];
Do[q[i] = delta[i - 1], {i, 1, n - 1}];
(* Right side coeff. bi *)
Do[b[i] = 3*(divDiff[i - 1]*delta[i] + divDiff[i]*delta[i - 1]),{i,1,n - 1}];
(* d0 and dn for natural cubic spline interpolation: *)
d[0] = f'[x[0]];
d[n] = f'[x[n]];
(* b1 and bn-1 differ from the right side calculation scheme: *)
b[1] = b[1] - m[1]*d[0];
b[n - 1] = b[n - 1] - q[n - 1]*d[n];
(* p1*d1 + q1*d2 =b1
m2*d1 + p2*d2 + q2*d3 =b2
m3*d2 + p3*d3 + q3*d4 =b3
....
mn-1 * dn-2 + pn-1 * dn-1 =bn-1
*)
(* Tridiagonal matrix algorithm' first sweep: *)
alpha[1] = -q[1]/p[1];
betha[1] = b[1]/p[1];
Do[alpha[k] = (-q[k])/(m[k]*alpha[k - 1] + p[k]), {k, 2, n - 2}];
Do[betha[k] = (b[k] - m[k]*betha[k - 1])/(m[k]*alpha[k - 1] + p[k]), {k, 2, n - 1}];
d[n - 1] = betha[n - 1];
(* Backward substitution: *)
Do[d[k] = alpha[k]*d[k + 1] + betha[k], {k, n - 2, 1, -1}];
(* Pi(t) with the Newton's formula: *)
P[i_, t_] := f[x[i]] + d[i]*(t - x[i]) + ((divDiff[i]- d[i])/delta[i])*(t - x[i])^2 + ((d[i + 1] - 2*divDiff[i] + d[i])/(delta[i])^2)*(t -
x[i + 1])*(t - x[i])^2 ;
(* Cubic spline function: *)
S[t_] := Sum[
If[t >= x[i] && t <= x[i + 1], P[i, t], 0], {i, 0, n - 1}];
Plot[{f[t], S[t]}, {t, 0, 1}, PlotRange -> All]
Plot[{f[t] - S[t]}, {t, 0, 1}]