Plot 拉格朗日插值没有显示绘图,因为有一条错误消息,我可以';不固定
所以,我试图编写一个程序,计算给定区间[a,b]内的拉格朗日插值。我的代码运行正常(我在程序的不同点上打印了结果),但我似乎无法得到一个好的绘图。这是我的代码:Plot 拉格朗日插值没有显示绘图,因为有一条错误消息,我可以';不固定,plot,interpolation,scilab,Plot,Interpolation,Scilab,所以,我试图编写一个程序,计算给定区间[a,b]内的拉格朗日插值。我的代码运行正常(我在程序的不同点上打印了结果),但我似乎无法得到一个好的绘图。这是我的代码: funcprot(0) clear all clf() a = -5 b = 5 j = 100 n = [3;9;15;36] function fx = func(x) fx = -x/(2+x^6) endfunction function y = Lagrange(t
funcprot(0)
clear all
clf()
a = -5
b = 5
j = 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')
disp(n_t)
disp(n_x)
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,j)'
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], rect=[a,-0.4,b, 0.4], style=[2,5])
plot2d(xi,fxi, style=[-6])
title(strcat(['Number of grid-points =: ',string(n(i))]))
c = zeros(n(i),1)
for j=1:n(i)
c(j) = (a+b)/2+((b-a)/2)*cos(((2*j-1)*%pi)/(2*n(i)))
end
fc = func(c)
yi2 = Lagrange(c,fc,x)
subplot(2,m,i+m)
plot2d(x,[yi2,fx],rect=[a,-0.4,b, 0.4], style=[2,5])
plot2d(c,fc, style=-6)
title(strcat(['Chebyshev = ',string(n(i))]))
end
我总是收到以下错误消息:
plot2d: Wrong size for input argument #7: 2 < 101 expected.
plot2d:输入参数的大小错误#应为7:2<101。
这意味着,我的绘图函数plot2d有点错误,但我不明白为什么。我试着改变“rect”和“style”,但这没有帮助,而且,它们只决定了曲线的边界和形状。我开始相信我的错误在别的地方,但我似乎找不到。当我去掉'rect'和'style'时,我得到了一些图形,但它们根本不正确,因为它们只是直线,而不是拉格朗日插值。如果有人能指出我的错误,我将不胜感激。
干杯 以下是运行该程序的一些技巧:尝试按照plot2D说明中要求的顺序和数量匹配参数:
plot2d(x,[yi1,fx], style=[2,5],,, rect=[a,-0.4,b, 0.4],)
plot2d(xi,fxi, style=[-6],,,)
...
plot2d(x,[yi2,fx], style=[2,5],,,rect=[a,-0.4,b, 0.4],)
plot2d(c,fc, style=-6,,,)
我无法理解你为什么要这样做,因为它们是可选的参数,但至少,你有你的结果(或者看起来你有stg,至少更进一步!)