2011-10-22 95 views
1

我有一個用帕斯卡計算三次樣條的任務。我已經完成了大部分的程序,還有一個問題。我的任務是,對於一個函數y = f(x),找到樣條S1和S2,也可以找到
delta1 = max | f(x)-S(x)|,delta2 = max | f'(x)-S1 '(x)|,delta3 = max | f(x)-S2(x)|,delta4 = max | f'(x)-S2'(x)|三次樣條插值:如何計算二次樣條S2

對於S2,哪個公式適用?

這裏是我到目前爲止的代碼:

program spline_1_2; 
uses crt; 
const 
k=1000; 
var 
input_file:text; 
out1_file:text; 
out2_file:text; 
n,i:integer; 
a_int,b_int,h:real; 
delta_1, delta_2:real; 
x, alpha, beta, y, a_coef, b_coef, c_coef, d_coef:array[0..k] of real; 
S, S1, S2:array[0..k] of real; 
A, B, C, F, z:array[0..k] of real; 

Function f_given(x:real):real; 
begin 
    f_given:=Cos((x*x)/3)-Exp(Sin(2*x)); 
end; 
Function f_prime(x:real):real; 
begin 
    f_prime:=-Cos((x*x)/3)*(1/3)*2*x-Exp(Sin(2*x))*Cos(2*x)*2; 
end; 

begin 
clrscr; 

Assign(input_file,'in.txt'); 
Reset(input_file); 

Assign(out1_file,'out1.txt'); 
Rewrite(out1_file); 

Assign(out2_file,'out2.txt'); 
Rewrite(out2_file); 

while not EOF(input_file) do 
begin 
    readln(input_file,a_int,b_int); 
    readln(input_file,h); 
    readln(input_file,n); 
end; 
for i:=0 to n do 
    begin 
    x[i]:=a_int+(i-0.5)*h; 
    f_given(x[i]); 
    y[i]:= f_given(x[i]); 
    f_prime(x[i]); 

    A[i]:= h; 
    C[i]:= 4*h; 
    B[i]:= h; 

end; 

for i:=1 to n-1 do 
begin 
a_coef[i]:=y[i]; 
F[i]:=((y[i+1]-2*y[i]+y[i-1])*3)/(h*h); 
A[1]:=0; 
    C[n-1]:=0; 
end; 
alpha[1]:=-C[1]/B[1]; 
beta[1]:=F[1]/B[1]; 

    for i:=1 to n-1 do 
begin 
    z[i]:=(A[i]*alpha[i-1] + C[i]); 
    alpha[i]:= -B[i]/z[i]; 
    beta[i]:= (F[i]-A[i]*beta[i-1])/z[i]; 
end; 

c_coef[n-1]:=(F[i] - A[i] * beta[n-1])/(C[i] + A[i] * alpha[n-1]); 

for i:=n-2 downto 1 do 
begin 
c_coef[i]:=alpha[i]*c_coef[i+1] + beta[i]; 
c_coef[0]:=0; 
c_coef[n]:=0; 

end; 



for i:=0 to n-1 do 
begin 
    d_coef[i]:=(c_coef[i+1]-c_coef[i])/(3*h); 
end; 
    b_coef[0]:=f_prime(x[0]); 
    b_coef[n-1]:=f_prime(x[n-1]); 

for i:=1 to n-2 do 
begin 
    b_coef[i]:=(h/3)*(c_coef[i+1]+2*c_coef[i]) + (y[i+1] - y[i])/h; 
end; 

//our spline 
for i:=1 to n do 
begin 
    S[i]:= a_coef[i] + b_coef[i]*(x[i-1]-x[i]) + c_coef[i]*sqr(x[i-1]-x[i]) + d_coef[i]*sqr(x[i-1]-x[i])*(x[i-1]-x[i]); 
    S1[i]:=b_coef[i]+2*c_coef[i]*(x[i-1]-x[i])+3*d_coef[i]*(x[i-1]-x[i])*(x[i-1]-x[i]); 
end; 

for i:=1 to n-1 do 
begin 
    Writeln(out1_file, x[i],' ',y[i]:3:6,' ',S[i]:3:6); 
    delta_1:=abs(y[i]-S[i]); 
    Writeln(out2_file,x[i],' ',f_prime(x[i]):3:6,' ', S1[i]:3:6); 
    delta_2:=abs(f_prime(x[i])-S1[i]); 
    end; 
    Writeln(out1_file,'"delta1" = ',delta_1); 
    Writeln(out2_file,'"delta2" = ',delta_2); 
    Readln; 
Close(input_file); 
Close(out1_file); 
Close(out2_file); 
end. 

回答

0

很可能已經過時但無論如何,一些注意事項第一

1.as我明白你F(X),並希望從中創建樣條

  • 一個三次曲線或更多聯結的樣條曲線?
  • S1,S2有什麼區別或含義?
  • 那麼f(x)是什麼?我看到f_given和f_prime是哪一個?

2.after樣條計算你必須計算偏差

  • 樣條和原始的F之間(X)
  • 及其衍生之間

3.什麼是您的輸入( A,H,N)?

  • 我假定爲x間隔的開始
  • h被點之間的x步驟
  • 且n是點的計數採樣

如何解決它:

1.get SPLINE

  • 只是從f(x)得到控制點,爲4個點
  • 並計算樣條係數(您的A,B,C,d)通過求解後此樣條方程式
  • t = < 0.0 , 1.0 > 
    x = x0 + (x1-x0)*t 
    y = a + b*t + c*t*t + d*t*t*t 
    y'=  b + c*t + d*t*t 
    
  • 其中X0,X1爲x

  • 的範圍
  • ,t是花鍵參數
  • 如果你有更多的加入了樣條段然後計算每段separatelly
  • 不要忘了適當加入他們的行列,如果您有PO整數P0,P1,P2,...
  • 然後調用樣條序列將蜜蜂

    p0,p0,p0,p1 // 3x p0 to ensure that SPLINE starts at point p0 
    p0,p0,p1,p2 
    p0,p1,p2,p3 // here continue normally 
    p1,p2,p3,p4 
    p2,p3,p4,p5 
    ... 
    p5,p6,p7,p8 
    p6,p7,p8,p9 // at the end the last point is also 3x 
    p7,p8,p9,p9 
    p8,p9,p9,p9 
    

2。計算的差異(偏差)

  • 只是做對於x = X0 X1 ..
  • 並計算差異
  • 商店最大結果