2019年2月20日 星期三

Julia語言 求Lagrange 的值 [ 1.5 , 2.5 ,3.5] 與誤差

Julia語言 求Lagrange 的值 [ 1.5 , 2.5 ,3.5] 與誤差 

x= [1.0 ,2.0 , 3.0 , 4.0] 
y= [0.0,0.693,1.099,1.386]
xa = [ 1.5 , 2.5 ,3.5]

程式
using Printf


#This function returns another function, which is the Lagrange Interpolant of the values xvals and yvals.
function LagrangeInterpolantGenerator(xvals,yvals)
    function LagrangeInterpolant(x)
        numvalstoevaluate = length(x)
        numvalstoevaluate == 1 ? output = 0 : output = zeros(numvalstoevaluate)
        for k = 1:numvalstoevaluate
            N = length(xvals)
            LagrangePolynomials = ones(N)
            for i in 1:N  
                for j in [1:i-1;i+1:N]     #Surprisingly, this works even in the i=1 and i=N cases.
                    LagrangePolynomials[i] = LagrangePolynomials[i].*(x[k]-xvals[j])./(xvals[i]-xvals[j])
                end
            end
            numvalstoevaluate == 1 ? output = sum(LagrangePolynomials.*yvals) : output[k] = sum(LagrangePolynomials.*yvals)
        end
        return output
    end
    return LagrangeInterpolant
end

#Examples
x= [1.0 ,2.0 , 3.0 , 4.0] 
y= [0.0,0.693,1.099,1.386]
xa = [ 1.5 , 2.5 ,3.5]

for i = 1:3

    interpolantfunc = LagrangeInterpolantGenerator(x,y)
    xb=xa[i]
    a=interpolantfunc(xb)   #returns  0.34650 
    s = @sprintf("Lagrange插值法 P(%0.2f) = %0.5f " , float(xb) , float(a) )
    println(s)

end


輸出結果
Lagrange插值法 P(1.50) = 0.39287 
Lagrange插值法 P(2.50) = 0.92138 
Lagrange插值法 P(3.50) = 1.24687 




using Printf


#This function returns another function, which is the Lagrange Interpolant of the values xvals and yvals.
function LagrangeInterpolantGenerator(xvals,yvals)
    function LagrangeInterpolant(x)
        numvalstoevaluate = length(x)
        numvalstoevaluate == 1 ? output = 0 : output = zeros(numvalstoevaluate)
        for k = 1:numvalstoevaluate
            N = length(xvals)
            LagrangePolynomials = ones(N)
            for i in 1:N  
                for j in [1:i-1;i+1:N]     #Surprisingly, this works even in the i=1 and i=N cases.
                    LagrangePolynomials[i] = LagrangePolynomials[i].*(x[k]-xvals[j])./(xvals[i]-xvals[j])
                end
            end
            numvalstoevaluate == 1 ? output = sum(LagrangePolynomials.*yvals) : output[k] = sum(LagrangePolynomials.*yvals)
        end
        return output
    end
    return LagrangeInterpolant
end

#Examples
x= [1.0 ,2.0 , 3.0 , 4.0] 
y= [0.0,0.693,1.099,1.386]
xa = [ 1.5 , 2.5 ,3.5]

for i = 1:3

    interpolantfunc = LagrangeInterpolantGenerator(x,y)
    xb=xa[i]
    a=interpolantfunc(xb)   #returns  0.34650 
    s = @sprintf("Lagrange插值法 P(%0.2f) = %0.5f  " , float(xb) , float(a) )
    println(s)
    xc = abs( float(a) - float(log(xb)) )
    s = @sprintf("Lagrange插值法 P(%0.2f) 與 真實值的誤差 = %0.5f " , float(xb) ,  float(xc)   )
    println(s)
   

end




Lagrange插值法 P(1.50) = 0.39287 
Lagrange插值法 P(1.50) 與 真實值的誤差 = 0.01259 
Lagrange插值法 P(2.50) = 0.92138 
Lagrange插值法 P(2.50) 與 真實值的誤差 = 0.00508 
Lagrange插值法 P(3.50) = 1.24687 
Lagrange插值法 P(3.50) 與 真實值的誤差 = 0.00589 

沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...