1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| n = input('插值等分为几块?'); X = -10:(10/n):10;
m = length(X); Y = zeros(1,m); syms x; f(x) = x^3+3^x-2*x; for i = 1:m Y(i) = f(X(i)); end
x0 = input('\n请输入你想要计算的插值'); ylagr=lagrange(X,Y,x0); fprintf('你所想要得到的拉格朗日插值结果为:'); disp(ylagr); ynewt=newton(X,Y,x0); fprintf('你所想要得到的牛顿插值结果为:'); disp(ynewt); plot(X,Y,'-',x0,ylagr,'o',x0,ynewt,'x'); ylim([-10,10]); fprintf('下面是线性拟合内容:'); x = [2,4,4.6,5,5.2,5.6,6,6.6,7]; y = [5,3.5,2.7,2.4,2.5,2,1.5,1.2,1.2]; plot(x,y,'.'); hold on; ab = linearfitting(x,y); a = ab(1); b = ab(2); linex(1) = -10; linex(2) = 10; liney(1) = a+b*linex(1); liney(2) = a+b*linex(2); plot(linex,liney,'-');
|