You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

拉格朗日插值绘图报错求助:plot2d参数尺寸不匹配问题排查

Fixing Your Scilab Lagrange Interpolation Plot Issues

Let's break down the problems in your code and fix them step by step:

1. Critical Variable Name Conflict

You're using j both as the number of evaluation points (j=100) and as a loop variable inside your Chebyshev node calculation. This overwrites j after the first iteration, leading to unexpected behavior later.

Fix: Rename the evaluation point count to something unique (like num_eval) and use a different loop variable (like k_idx):

num_eval = 100  % Replaced j=100
...
x = linspace(a,b,num_eval)'
...
% Inside Chebyshev node loop:
for k_idx=1:n(i)
    c(k_idx) = (a+b)/2 + ((b-a)/2)*cos(((2*k_idx -1)*%pi)/(2*n(i)))
end

2. Plot2d Argument Misalignment

The error plot2d: Wrong size for input argument #7 comes from mixing named arguments (rect=, style=) in a way that confuses Scilab's parameter ordering.

Fix: Use positional arguments in the correct order for plot2d, or split your plot calls to avoid multi-column y vectors:

% Option 1: Correct positional arguments
subplot(2,m,i)
plot2d(x, [yi1, fx], [2,5], "081", "", [a,-0.4,b,0.4])
plot2d(xi, fxi, style=-6)

% Option 2: Split into separate plots (easier to read)
subplot(2,m,i)
plot2d(x, yi1, style=2, rect=[a,-0.4,b,0.4])
plot2d(x, fx, style=5, rect=[a,-0.4,b,0.4], axesflag=0)  % axesflag=0 avoids redrawing axes
plot2d(xi, fxi, style=-6)

3. Corrected Full Code

Here's the revised code with all fixes applied:

funcprot(0)
clear all
clf()
a = -5
b = 5
num_eval = 100
n = [3;9;15;36]

function fx = func(x)
    fx = -x/(2+x^6)
endfunction

function y = Lagrange(t, f, x)
    n_t = size(t,'r')
    n_x = size(x,'r')
    y = zeros(n_x,1)
    for k = 1:n_x
        POL = 0
        for i = 1:n_t
            L_k = 1
            for l = 1:n_t
                if (l ~= i) then
                    L_k = L_k * (x(k)-t(l))/(t(i)-t(l))
                end
            end
            POL = POL + f(i)*L_k
        end
        y(k) = POL
    end
endfunction

x = linspace(a,b,num_eval)'
fx = func(x)
m = size(n, 'r')

for i=1:m
    xi = linspace(a,b,n(i))'
    fxi = func(xi)
    yi1 = Lagrange(xi, fxi, x)
    
    subplot(2,m,i)
    plot2d(x, [yi1, fx], [2,5], "081", "", [a,-0.4,b,0.4])
    plot2d(xi, fxi, style=-6)
    title(strcat(['Number of grid-points = ', string(n(i))]))
    
    c = zeros(n(i),1)
    for k_idx=1:n(i)
        c(k_idx) = (a+b)/2 + ((b-a)/2)*cos(((2*k_idx -1)*%pi)/(2*n(i)))
    end
    fc = func(c)
    yi2 = Lagrange(c,fc,x)
    
    subplot(2,m,i+m)
    plot2d(x, [yi2, fx], [2,5], "081", "", [a,-0.4,b,0.4])
    plot2d(c, fc, style=-6)
    title(strcat(['Chebyshev nodes = ', string(n(i))]))
end

Why This Works

  • Variable name fix: Ensures num_eval stays at 100 for all iterations, so your evaluation points x remain consistent.
  • Plot2d fix: Positional arguments eliminate confusion about parameter ordering, and the style vector [2,5] correctly maps to the two curves in [yi1, fx].
  • Lagrange function: Your original interpolation logic was correct—once the variable conflict and plot issues are fixed, it will generate the expected curves.

You should now see proper Lagrange interpolation plots for both uniform and Chebyshev nodes, matching the original function fx closely (especially with Chebyshev nodes, which mitigate Runge's phenomenon).

内容的提问来源于stack exchange,提问作者Terza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:52:20