Помогите переписать процедуру Spline с Pascal на C#
Формулировка задачи:
Помогите пожалуйста переписать процедуру Spline на C#
вот код паскаль:
Vector = Array [0..640] of Real; Vec = Array [0..30] of Real; Matr = Array [ 1..30,1..30] of Real; Ne,Ngr :Integer; Xe,Ye,Xg,Yg:Vector; M :Integer; al,bl:Real; K:Vec; Procedure Spline(Xe, Ye : Vector; Ne, Ngr : Integer; Var Xg, Yg : Vector); Var a, b, c, d : Vec; h, R : Real; i, j, L : Integer; //cubspln Procedure CubSpln(Xe,Ye:Vector;Ne:Integer;Var a,b,c,d:Vec); Var h, Y : Vec; Ma : Matr; i, j : Integer; R, Q : Real; //progonka Procedure Progonka (A:Matr; b:Vec; N:Integer; Var X:Vec); Var Alp, Bet : Vec; R : Real; i : Integer; Begin Alp[1] := - A[1, 2] / A[1, 1]; Bet[1] := b[1] / A[1, 1]; For i := 2 to N-1 do Begin R := A[i,i] + Alp[i - 1] * A[i, i - 1]; Alp[i] := - A[i, i + 1] / R; Bet [i] := (b[i] - A[i, i - 1] * Bet[i - 1]) / R End; X[N] := (B[N] - A[N, N - 1] * Bet[N - 1]) / (A[N, N] + Alp[N - 1] * A[N, N - 1]); For i := N-1 downto 1 do X[i] := Alp [i] * X [i + 1] + Bet [i] End; //progonka Begin Ne := Ne - 1; For i := 1 to Ne do Begin H[i] := Xe[i] - Xe[i - 1]; a[i] := Ye[i] End; For i := 1 to Ne - 1 do For j := 1 to Ne - 1 do If i=j then Begin R := H[i]; Q :=H[i + 1]; Ma[i, i] := 2 * (R + Q); Y[i] := 6*((Ye[i+1]-Ye[i])/Q-(Ye[i]-Ye[i-1])/R); End Else Begin Q := H[i + 1]; Ma[j, i] := 0; Ma[i + 1, i] := Q; Ma[i, i + 1] := Q End; Progonka (Ma,Y,Ne-1,c); c[0] := 0; c[Ne] := 0; For i:=1 to Ne do Begin R := h[i]; d[i] := (c[i] - c[i-1]) / R; b[i] := R*c[i]/2 - Sqr(R)*d[i]/6+(Ye[i] - Ye[i-1])/R End; End; //cubspln Begin CubSpln (Xe, Ye, Ne, a, b, c, d); L := 1; h := (bl - al) / Ngr; Xg[0]:= al; For i := 0 to Ngr - 1 do Begin YG[i] := a[L] + b[L] * (Xg[i] - Xe[L])+ c[L] * Sqr(Xg[i] - Xe[L])/2 + d[L]*Sqr(Xg[i] - Xe[L])*(Xg[i] - Xe[L])/6; Xg[i + 1] := Xg[i] + h; If Xg[i] > Xe [L] then L := L + 1 End END; //end spline
Решение задачи: «Помогите переписать процедуру Spline с Pascal на C#»
textual
Листинг программы
private void Progonka(Double[,] A, Double[] B, Int32 N, Double[] X) { Double[] Alp = new Double[N]; Double[] Bet = new Double[N]; Double R; Int32 i; Alp[0] = -A[0, 1] / A[0, 0]; Bet[0] = B[0] / A[0, 0]; for (i = 1; i < N; i++) { R = A[i, i] + Alp[i - 1] * A[i, i - 1]; Alp[i] = -A[i, i + 1] / R; Bet[i] = (B[i] - A[i, i - 1] * Bet[i - 1]) / R; } X[N] = (B[N] - A[N, N - 1] * Bet[N - 1]) / (A[N, N] + Alp[N - 1] * A[N, N - 1]); for (i = N - 1; i >= 0; i--) { X[i] = Alp[i] * X[i + 1] + Bet[i]; } } private void CubSpline(Double[] Xe, Double[] Ye, Int32 Ne, Double[] a, Double[] b, Double[] c, Double[] d) { Double[,] Ma = new Double[50, 50]; Double[] h = new Double[40]; Double[] Y = new Double[40]; Int32 i = 0, j = 0; Double R = 0, Q = 0; Ne--; for (i = 1; i <= Ne - 1; i++) { h[i] = Xe[i] = Xe[i - 1]; a[i] = Ye[i]; } for ( i = 1; i <= Ne - 1; i++) for ( j = 1; j <= Ne - 1; j++) { if (i == j) { R = h[i]; Q = h[i + 1]; Ma[i, i] = 2 * (R + Q); Y[i] = 6 * ((Ye[i + 1] - Ye[i]) / Q - (Ye[i] - Ye[i - 1]) / R);//Ye[i-1]??? } else { Q = h[i + 1];// Ma[j, i] = 0; Ma[i + 1, i] = Q; Ma[i, i + 1] = Q; } } Progonka(Ma, Y, Ne-1, c); c[0] = 0; c[Ne] = 0; for (i = 1; i <= Ne; i++) { R = h[i]; d[i] = (c[i] - c[i-1]) / R; b[i] = R * c[i] / 2 - R * R * d[i] / 6 + (Ye[i] - Ye[i - 1]) / R; } } private void TabSpline(Double[] Xe, Double[] Ye, Int32 Ne, Int32 Ngr ,Double[]Xg, Double[]Yg) { CubSpline(Xe, Ye, Ne, a, b, c, d); Int32 L = 1; Double h = (bl - al) / Ngr; Xg[0] = al; //Yg[0] = a[L] + b[L] * (Xg[0] - Xe[L]) + c[L] * Math.Pow(Xg[0] - Xe[L], 2) / 2 + d[L] * Math.Pow(Xg[0] - Xe[L], 2) * (Xg[0] - Xe[L]) / 6; for (int i = 0; i <= Ngr - 1 ; i++) { Yg[i] = a[L] + b[L] * (Xg[i] - Xe[L]) + c[L] * Math.Pow(Xg[i] - Xe[L], 2) / 2 + d[L] * Math.Pow(Xg[i] - Xe[L], 2) * (Xg[i] - Xe[L]) / 6; Xg[i + 1] = Xg[i] + h; if (Xg[i] > Xe[L]) L++; } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д