首页 文章

拉格朗日多项式与Matlab

提问于
浏览
1

这是我必须解决的问题:

编写一个程序来评估和绘制拉格朗日插值Iu(x)的u(x)= 1 /(1 x ^ 2),x在-5和5之间 . 为5,7,9,11,13执行此操作, 15点插值(5,7,9等数据点之间,包括-5和5) . 您的结果应显示函数和插值 .

这是我到目前为止提出的代码:

int_pts = [5,7,9,11,13,15]; %various values for no. of point interpolants
n = length(int_pts);

u = @(x) 1./(1+x.^2); %the function we are interested in

numer = 1; 
denom = 1; %set initial values of numerator and denominator 

for k = 1 : n

    L = zeros( 1,int_pts(k) ); %create an array to store Lagrange polynomials 

    x = [-5 : 10/(int_pts(k)-1) : 5]; %create a list of x values

    udata = u(x); %create a list of corresponding values of u(x)

    for i = 1 : int_pts(k)

        if (i ~= k)

            denom = x(k) - x(i);

            syms z;
            numer = symfun( z - x(i) , z);        

            break

        end

        numer = numer * numer;
        denom = denom * denom;

        L(i) = numer / denom;

    end

end

我想我已走上正轨,但我真的开始陷入如何提取数据并很好地绘制所有内容的过程中 . 我到处搜索,但大多数代码似乎只想输出拉格朗日多项式的值为某些数,即P(2)= ...

1 回答

  • 0

    在大多数情况下,您开发的内容可以引导您找到解决问题的正确途径 .

    您遇到的一些小问题是:

    1) You are missing a for loop.

    在拉格朗日插值中,您应该有n个项的总和,其中n是您拥有的数据点的数量 . 这些术语由乘积乘以数据点的y分量,即yj *Π{(x-xi)/(xj-xi)},其中如果i = j,则跳过该项(这样做是为了确保分母永远不为零)

    2) Your inner for loop and if statement don't play well together.

    一旦脚本到达 if 语句的末尾, for 循环就会中断并继续到下一个 k 值 .

    这应该替换为 else 后跟 continue .

    3) When you generate your numer and denom variables you are unintentionally just squaring the new value and storing it to the variable.

    你有 numer = numer * numer 这应该 numer = numer * numer_temp

    这是解决方案的快速伪代码:

    for n equals 'number of data points'
    
        Generate the n data points, P(x,y)
    
        for i equals 1 to n
    
            for j equals 1 to n
    
                if i is not equal to j
                    Calculate Product of Numerators <- Function of x
                    Calculate Product of Denominators
                else
                    Continue to next iteration of loop
                endIf
    
            endFor
    
            Calculate Lagrange Polynomial < Function of x
        endFor
    
        Plot Lagrange
        Plot Original Function
    
    endFor
    

    从中您可以更新代码以正确确定拉格朗日多项式 .

    u = @(x) 1./(1+x.^2); % The function we are interested in
    number_of_data_Points = [5,7,9,11,13,15];
    
    
    syms x; % Initializes x to be symbolic
    
    for n = number_of_data_Points
    
        xn = linspace(-5,5,n);      % Creates x values
        yn = u(xn);                 % Creates y values
    
        L=0; % Re/Initializes Langrange Polynomial
        for j = 1:n
            numer = 1; % Re/Initializes Numerator
            denom = 1; % Re/Initializes Denominator
            for i = 1:n
                if j~=i
                    denom_temp = xn(j) - xn(i);
    
                    numer_temp = x - xn(i);    
    
                    numer =  numer * numer_temp;
                    denom = denom * denom_temp;
                else
                    continue
                end
            end
            L = L + yn(j) * numer / denom;
        end
        L = matlabFunction(L);
    end
    

    我将保留更改代码,以便输出您需要的结果 .

相关问题