1、所有题目设计题1: 根据下表所列的数据点求出其拉格朗日插值多项式及牛顿插值多项式,并计算当x=2.0时的值。11.21.82.5411.443.246.2516 x=1 1.2 1.8 2.5 4; y=1 1.44 3.24 6.25 16; p1=polyfit(x,y,1)p1 = 5.0224 -4.9611 y1=polyval(p1,2.0)y1 = 5.0838设计题2: 根据下面实验数据求解拟合曲线12346782367532在MATLAB中,用polyfit函数来求得最小二乘拟合多项式的系数,再用polyval函数按所得的多项式计算所给出的点上的函数近似值。polyfit函数
2、的调用格式为:P,S=polyfit(X,Y,m)函数根据采样点X和采样点函数值Y,产生一个m次多项式P及其在采样点的误差向量S。其中X,Y是两个等长的向量,P是一个长度为m+1的向量,P的元素为多项式系数。function p,S,mu = polyfit(x,y,n)%POLYFIT Fit polynomial to data.% POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of% degree N that fits the data, P(X(I)=Y(I), in a least-squares sense.
3、% P,S = POLYFIT(X,Y,N) returns the polynomial coefficients P and a% structure S for use with POLYVAL to obtain error estimates on predictions.% P is a row vector of length N+1 containing the polynomial coefficients% in descending powers, P(1)*XN + P(2)*X(N-1) +.+ P(N)*X + P(N+1).% If the errors in t
4、he data, Y, are independent normal with constant % variance, POLYVAL will produce error bounds which contain at least 50% of % the predictions.% The structure S contains the Cholesky factor of the Vandermonde% matrix (R), the degrees of freedom (df), and the norm of the% residuals (normr) as fields.
5、 % P,S,MU = POLYFIT(X,Y,N) finds the coefficients of a polynomial% in XHAT = (X-MU(1)/MU(2) where MU(1) = mean(X) and MU(2) = std(X).% This centering and scaling transformation improves the numerical% properties of both the polynomial and the fitting algorithm.% Warning messages result if N is = len
6、gth(X), if X has repeated, or% nearly repeated, points, or if X might need centering and scaling.% Class support for inputs x,y:% float: double, single% See also POLY, POLYVAL, ROOTS.% Copyright 1984-2004 The MathWorks, Inc.% $Revision: 5.17.4.4 $ $Date: 2004/03/02 21:47:57 $% The regression problem
7、 is formulated in matrix format as:% y = V*p or% 3 2% y = x x x 1 p3% p2% p1% p0% where the vector p contains the coefficients to be found. For a% 7th order polynomial, matrix V would be:% V = x.7 x.6 x.5 x.4 x.3 x.2 x ones(size(x);if isequal(size(x),size(y) error(MATLAB:polyfit:XYSizeMismatch,. X a
8、nd Y vectors must be the same size.)endx = x(:);y = y(:);if nargout 2 mu = mean(x); std(x); x = (x - mu(1)/mu(2);end% Construct Vandermonde matrix.V(:,n+1) = ones(length(x),1,class(x);for j = n:-1:1 V(:,j) = x.*V(:,j+1);end% Solve least squares problem, and save the Cholesky factor.Q,R = qr(V,0);ws
9、= warning(off,all); p = R(Q*y); % Same as p = Vy;warning(ws);if size(R,2) size(R,1) warning(MATLAB:polyfit:PolyNotUnique, . Polynomial is not unique; degree = number of data points.)elseif condest(R) 1.0e10 if nargout 2 warning(MATLAB:polyfit:RepeatedPoints, . Polynomial is badly conditioned. Remove
10、 repeated data points.) else warning(MATLAB:polyfit:RepeatedPointsOrRescale, . Polynomial is badly conditioned. Remove repeated data pointsn . or try centering and scaling as described in HELP POLYFIT.) endendr = y - V*p;p = p.; % Polynomial coefficients are row vectors by convention.% S is a struct
11、ure containing three elements: the Cholesky factor of the% Vandermonde matrix, the degrees of freedom and the norm of the residuals.S.R = R;S.df = length(y) - (n+1);S.normr = norm(r);polyval函数的功能是按多项式的系数计算x点多项式的值 P S=polyfit(x,y,2)P = -0.3864 3.4318 -1.3182S = R: 3x3 double df: 4 normr: 1.7321 f=pol
12、yval(P,x)f = Columns 1 through 6 1.7273 4.0000 5.5000 6.2273 5.3636 3.7727 Column 7 1.4091 plot(x,y,*) table=x y f y-ftable = Columns 1 through 6 1.0000 2.0000 3.0000 4.0000 6.0000 7.0000 Columns 7 through 12 8.0000 2.0000 3.0000 6.0000 7.0000 5.0000 Columns 13 through 18 3.0000 2.0000 1.7273 4.0000
13、 5.5000 6.2273 Columns 19 through 24 5.3636 3.7727 1.4091 0.2727 -1.0000 0.5000 Columns 25 through 28 0.7727 -0.3636 -0.7727 0.5909 plot(x,y,*,x,f,-)设计题3: (3和4二选一,若选3,不能选4;若选4,不能选3)用列主元Gauss消去法解方程组设计题4: 用LU分解求方程组 A=2,2,3;4,7,7;-2,4,5; b=3,1,7; l,u=lu(A)l = 0.5000 -0.2000 1.0000 1.0000 0 0 -0.5000 1.
14、0000 0u = 4.0000 7.0000 7.0000 0 7.5000 8.5000 0 0 1.2000 L,U,P=lu(A)L = 1.0000 0 0 -0.5000 1.0000 0 0.5000 -0.2000 1.0000U = 4.0000 7.0000 7.0000 0 7.5000 8.5000 0 0 1.2000P = 0 1 0 0 0 1 1 0 0 b,c=huffman(a);设计题5: 用雅可比迭代法和高斯-塞德尔迭代法求线性方程组的解,初始迭代值,求近似解使其满足Gauss法的MATLAB函数文件gauss.m如下:function y,n=gaus
15、sl(A,b,x0,eps)if nargin=3 eps=1.0e-6;elseif nargin=eps x0=y; y=G*x0+f; n=n+1;end A=0.9889 -0.0005 -0.0002;-0.0046 0.9946 0.0077;-0.0002 0.0092 0.9941; b=1 0 1; x ,n=gauss(A,b,1;1;1,1e-5)x = 1.0114 -0.0031 1.0062n = 3设计题6:用牛顿法求方程在区间1,2内的根。初始值,求近似解使得 syms x f; f=inline(x.3-x-1); I,n=quad(f,1,2,10(-5)I
16、 = 1.250000000000000n = 13 syms x y; x=1 1.5 2; y=x.3-x-1y = -1.0000 0.8750 5.0000 x=1 1.25 1.5; y=x.3-x-1y = -1.0000 -0.2969 0.8750 x=1.25 1.275 1.5; y=x.3-x-1y = -0.2969 -0.2023 0.8750 x=1.275 1.385 1.5; y=x.3-x-1y = -0.2023 0.2717 0.8750 x=1.275 1.3312 1.3875; y=x.3-x-1y = -0.2023 0.0278 0.2837 x
17、=1.275 1.3031 1.3312; y=x.3-x-1y = -0.2023 -0.0903 0.0278 x=1.275 1.3031 1.3312; x=1.3031 1.3171 1.3312; y=x.3-x-1y = -0.0903 -0.0323 0.0278 x=1.3171 1.3241 1.3312; y=x.3-x-1y = -0.0323 -0.0026 0.0278 x=1.3241 1.3277 1.3312; y=x.3-x-1y = -0.0026 0.0128 0.0278 x=1.3241 1.3259 1.3277; y=x.3-x-1y = -0.
18、0026 0.0050 0.0128 x=1.3241 1.3250 1.3259; y=x.3-x-1y = -0.0026 0.0012 0.0050 x=1.3241 1.3245 1.3250; y=x.3-x-1y = -0.0026 -0.0009 0.0012 x=1.3245 1.3247 1.3250; y=x.3-x-1y = -0.0009 -0.0001 0.0012 x=1.3247 1.3249 1.3250; y=x.3-x-1y = -0.0001 0.0008 0.0012 x=1.3247 1.3248 1.3249; y=x.3-x-1y = 1.0e-0
19、03 * -0.0766 0.3499 0.7765 x=1.3247 1.32475 1.3248; y=x.3-x-1y = 1.0e-003 * -0.0766 0.1367 0.3499 x=1.3247 1.324725 1.32475; y=x.3-x-1y = 1.0e-003 * -0.0766 0.0300 0.1367因为|1.32475-1.3247|105所以X0=1.32475设计题7:用复合梯形求积公式和复合辛普森求积公式计算(划分的子区间至少为10个)基于变步长的辛普森求积,该函数的调用格式为: I,n=quad(fname,a,b,tol,trace)其中fna
20、me是被积函数名。a和b分别是定积分的下限和上限。tol用来控制积分精度,缺省时取tol=0.001。trace控制是否展现积分过程,若取非0则展现积分过程,取0则不展现,缺省时取trace=0。返回参数I即定积分值,n为被积函数的调用次数。function Q,fcnt = quad(funfcn,a,b,tol,trace,varargin)%QUAD Numerically evaluate integral, adaptive Simpson quadrature.% Q = QUAD(FUN,A,B) tries to approximate the integral of func
21、tion% FUN from A to B to within an error of 1.e-6 using recursive% adaptive Simpson quadrature. The function Y = FUN(X) should% accept a vector argument X and return a vector result Y, the% integrand evaluated at each element of X. % Q = QUAD(FUN,A,B,TOL) uses an absolute error tolerance of TOL % in
22、stead of the default, which is 1.e-6. Larger values of TOL% result in fewer function evaluations and faster computation,% but less accurate results. The QUAD function in MATLAB 5.3 used% a less reliable algorithm and a default tolerance of 1.e-3.% Q,FCNT = QUAD(.) returns the number of function eval
23、uations.% QUAD(FUN,A,B,TOL,TRACE) with non-zero TRACE shows the values% of fcnt a b-a Q during the recursion.% QUAD(FUN,A,B,TOL,TRACE,P1,P2,.) provides for additional % arguments P1, P2, . to be passed directly to function FUN,% FUN(X,P1,P2,.). Pass empty matrices for TOL or TRACE to% use the defaul
24、t values.% Use array operators .*, ./ and . in the definition of FUN% so that it can be evaluated with a vector argument.% Function QUADL may be more efficient with high accuracies% and smooth integrands.% Example:% FUN can be specified as:% An anonymous function:% F = (x) 1./(x.3-2*x-5);% Q = quad(
25、F,0,2);% A function handle:% Q = quad(myfun,0,2);% where myfun.m is an M-file:% function y = myfun(x)% y = 1./(x.3-2*x-5);% Class support for inputs A, B, and the output of FUN: % float: double, single% See also QUADV, QUADL, DBLQUAD, TRIPLEQUAD, .% Based on adaptsim by Walter Gander. % Ref: W. Gand
26、er and W. Gautschi, Adaptive Quadrature Revisited, 1998.% http:/www.inf.ethz.ch/personal/gander% Copyright 1984-2004 The MathWorks, Inc. % $Revision: 5.26.4.3 $ $Date: 2004/03/24 03:05:30 $f = fcnchk(funfcn);if nargin 4 | isempty(tol), tol = 1.e-6; end;if nargin 5 | isempty(trace), trace = 0; end;%
27、Initialize with three unequal subintervals.h = 0.13579*(b-a);x = a a+h a+2*h (a+b)/2 b-2*h b-h b;y = f(x, varargin:);fcnt = 7;% Fudge endpoints to avoid infinities.if isfinite(y(1) y(1) = f(a+eps(superiorfloat(a,b)*(b-a),varargin:); fcnt = fcnt+1;endif isfinite(y(7) y(7) = f(b-eps(superiorfloat(a,b)
28、*(b-a),varargin:); fcnt = fcnt+1;end% Call the recursive core integrator.hmin = eps(b-a)/1024;Q(1),fcnt,warn(1) = . quadstep(f,x(1),x(3),y(1),y(2),y(3),tol,trace,fcnt,hmin,varargin:);Q(2),fcnt,warn(2) = . quadstep(f,x(3),x(5),y(3),y(4),y(5),tol,trace,fcnt,hmin,varargin:);Q(3),fcnt,warn(3) = . quadstep(f,x(5),x(7),y(5),y(6),y(7),tol,trace,fcnt,hmin,varargin:);Q = sum(Q);warn = max(warn);switch warn case 1 warning(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1