所有题目.docx
《所有题目.docx》由会员分享,可在线阅读,更多相关《所有题目.docx(17页珍藏版)》请在冰豆网上搜索。
所有题目
设计题1:
根据下表所列的数据点求出其拉格朗日插值多项式及牛顿插值多项式,并计算当x=2.0时的
值。
1
1.2
1.8
2.5
4
1
1.44
3.24
6.25
16
>>x=[11.21.82.54];
>>y=[11.443.246.2516];
>>p1=polyfit(x,y,1)
p1=
5.0224-4.9611
>>y1=polyval(p1,2.0)
y1=
5.0838
>>
设计题2:
根据下面实验数据求解拟合曲线
1
2
3
4
6
7
8
2
3
6
7
5
3
2
在MATLAB中,用polyfit函数来求得最小二乘拟合多项式的系数,再用polyval函数按所得的多项式计算所给出的点上的函数近似值。
polyfit函数的调用格式为:
[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)
%POLYFITFitpolynomialtodata.
%POLYFIT(X,Y,N)findsthecoefficientsofapolynomialP(X)of
%degreeNthatfitsthedata,P(X(I))~=Y(I),inaleast-squaressense.
%
%[P,S]=POLYFIT(X,Y,N)returnsthepolynomialcoefficientsPanda
%structureSforusewithPOLYVALtoobtainerrorestimatesonpredictions.
%PisarowvectoroflengthN+1containingthepolynomialcoefficients
%indescendingpowers,P
(1)*X^N+P
(2)*X^(N-1)+...+P(N)*X+P(N+1).
%Iftheerrorsinthedata,Y,areindependentnormalwithconstant
%variance,POLYVALwillproduceerrorboundswhichcontainatleast50%of
%thepredictions.
%
%ThestructureScontainstheCholeskyfactoroftheVandermonde
%matrix(R),thedegreesoffreedom(df),andthenormofthe
%residuals(normr)asfields.
%
%[P,S,MU]=POLYFIT(X,Y,N)findsthecoefficientsofapolynomial
%inXHAT=(X-MU
(1))/MU
(2)whereMU
(1)=mean(X)andMU
(2)=std(X).
%Thiscenteringandscalingtransformationimprovesthenumerical
%propertiesofboththepolynomialandthefittingalgorithm.
%
%WarningmessagesresultifNis>=length(X),ifXhasrepeated,or
%nearlyrepeated,points,orifXmightneedcenteringandscaling.
%
%Classsupportforinputsx,y:
%float:
double,single
%
%SeealsoPOLY,POLYVAL,ROOTS.
%Copyright1984-2004TheMathWorks,Inc.
%$Revision:
5.17.4.4$$Date:
2004/03/0221:
47:
57$
%Theregressionproblemisformulatedinmatrixformatas:
%
%y=V*por
%
%32
%y=[xxx1][p3
%p2
%p1
%p0]
%
%wherethevectorpcontainsthecoefficientstobefound.Fora
%7thorderpolynomial,matrixVwouldbe:
%
%V=[x.^7x.^6x.^5x.^4x.^3x.^2xones(size(x))];
if~isequal(size(x),size(y))
error('MATLAB:
polyfit:
XYSizeMismatch',...
'XandYvectorsmustbethesamesize.')
end
x=x(:
);
y=y(:
);
ifnargout>2
mu=[mean(x);std(x)];
x=(x-mu
(1))/mu
(2);
end
%ConstructVandermondematrix.
V(:
n+1)=ones(length(x),1,class(x));
forj=n:
-1:
1
V(:
j)=x.*V(:
j+1);
end
%Solveleastsquaresproblem,andsavetheCholeskyfactor.
[Q,R]=qr(V,0);
ws=warning('off','all');
p=R\(Q'*y);%Sameasp=V\y;
warning(ws);
ifsize(R,2)>size(R,1)
warning('MATLAB:
polyfit:
PolyNotUnique',...
'Polynomialisnotunique;degree>=numberofdatapoints.')
elseifcondest(R)>1.0e10
ifnargout>2
warning('MATLAB:
polyfit:
RepeatedPoints',...
'Polynomialisbadlyconditioned.Removerepeateddatapoints.')
else
warning('MATLAB:
polyfit:
RepeatedPointsOrRescale',...
['Polynomialisbadlyconditioned.Removerepeateddatapoints\n'...
'ortrycenteringandscalingasdescribedinHELPPOLYFIT.'])
end
end
r=y-V*p;
p=p.';%Polynomialcoefficientsarerowvectorsbyconvention.
%Sisastructurecontainingthreeelements:
theCholeskyfactorofthe
%Vandermondematrix,thedegreesoffreedomandthenormoftheresiduals.
S.R=R;
S.df=length(y)-(n+1);
S.normr=norm(r);
polyval函数的功能是按多项式的系数计算x点多项式的值
>>[PS]=polyfit(x,y,2)
P=
-0.38643.4318-1.3182
S=
R:
[3x3double]
df:
4
normr:
1.7321
>>f=polyval(P,x)
f=
Columns1through6
1.72734.00005.50006.22735.36363.7727
Column7
1.4091
>>plot(x,y,'*')
>>table=[xyfy-f]
table=
Columns1through6
1.00002.00003.00004.00006.00007.0000
Columns7through12
8.00002.00003.00006.00007.00005.0000
Columns13through18
3.00002.00001.72734.00005.50006.2273
Columns19through24
5.36363.77271.40910.2727-1.00000.5000
Columns25through28
0.7727-0.3636-0.77270.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.20001.0000
1.000000
-0.50001.00000
u=
4.00007.00007.0000
07.50008.5000
001.2000
>>[L,U,P]=lu(A)
L=
1.000000
-0.50001.00000
0.5000-0.20001.0000
U=
4.00007.00007.0000
07.50008.5000
001.2000
P=
010
001
100
>>[b,c]=huffman(a);
设计题5:
用雅可比迭代法和高斯-塞德尔迭代法求线性方程组的解,初始迭代值
,求近似解
使其满足
Gauss法的MATLAB函数文件gauss.m如下:
function[y,n]=gaussl(A,b,x0,eps)
ifnargin==3
eps=1.0e-6;
elseifnargin<3
error
return
end
D=diag(diag(A)); %求A的对角矩阵
L=-tril(A,-1); %求A的下三角阵
U=-triu(A,1); %求A的上三角阵
G=(D-L)\U;
f=(D-L)\b;
y=G*x0+f;
n=1; %迭代次数
whilenorm(y-x0)>=eps
x0=y;
y=G*x0+f;
n=n+1;
end
--[if!
supportEmptyParas]-->
--[endif]-->
>>A=[0.9889-0.0005-0.0002;-0.00460.99460.0077;-0.00020.00920.9941];
>>b=[101]';
>>[x,n]=gauss(A,b,[1;1;1],1e-5)
x=
1.0114
-0.0031
1.0062
n=
3
>>
设计题6:
用牛顿法求方程
在区间[1,2]内的根。
初始值
,求近似解
使得
>>symsxf;
>>f=inline('x.^3-x-1');
>>[I,n]=quad(f,1,2,10^(-5))
I=
1.250000000000000
n=
13
>>
>>symsxy;
>>x=[11.52];
>>y=x.^3-x-1
y=
-1.00000.87505.0000
>>x=[11.251.5];
>>y=x.^3-x-1
y=
-1.0000-0.29690.8750
>>x=[1.251.2751.5];
>>y=x.^3-x-1
y=
-0.2969-0.20230.8750
>>x=[1.2751.3851.5];
>>y=x.^3-x-1
y=
-0.20230.27170.8750
>>x=[1.2751.33121.3875];
>>y=x.^3-x-1
y=
-0.20230.02780.2837
>>x=[1.2751.30311.3312];
>>y=x.^3-x-1
y=
-0.2023-0.09030.0278
>>x=[1.2751.30311.3312];
>>x=[1.30311.31711.3312];
>>y=x.^3-x-1
y=
-0.0903-0.03230.0278
>>x=[1.31711.32411.3312];
>>y=x.^3-x-1
y=
-0.0323-0.00260.0278
>>x=[1.32411.32771.3312];
>>y=x.^3-x-1
y=
-0.00260.01280.0278
>>x=[1.32411.32591.3277];
>>y=x.^3-x-1
y=
-0.00260.00500.0128
>>x=[1.32411.32501.3259];
>>y=x.^3-x-1
y=
-0.00260.00120.0050
>>x=[1.32411.32451.3250];
>>y=x.^3-x-1
y=
-0.0026-0.00090.0012
>>x=[1.32451.32471.3250];
>>y=x.^3-x-1
y=
-0.0009-0.00010.0012
>>x=[1.32471.32491.3250];
>>y=x.^3-x-1
y=
-0.00010.00080.0012
>>x=[1.32471.32481.3249];
>>y=x.^3-x-1
y=
1.0e-003*
-0.07660.34990.7765
>>x=[1.32471.324751.3248];
>>y=x.^3-x-1
y=
1.0e-003*
-0.07660.13670.3499
>>x=[1.32471.3247251.32475];
>>y=x.^3-x-1
y=
1.0e-003*
-0.07660.03000.1367
因为|1.32475-1.3247|<105所以X0=1.32475
设计题7:
用复合梯形求积公式和复合辛普森求积公式计算
(划分的子区间至少为10个)
基于变步长的辛普森求积,该函数的调用格式为:
[I,n]=quad('fname',a,b,tol,trace)
其中fname是被积函数名。
a和b分别是定积分的下限和上限。
tol用来控制积分精度,缺省时取tol=0.001。
trace控制是否展现积分过程,若取非0则展现积分过程,取0则不展现,缺省时取trace=0。
返回参数I即定积分值,n为被积函数的调用次数。
function[Q,fcnt]=quad(funfcn,a,b,tol,trace,varargin)
%QUADNumericallyevaluateintegral,adaptiveSimpsonquadrature.
%Q=QUAD(FUN,A,B)triestoapproximatetheintegraloffunction
%FUNfromAtoBtowithinanerrorof1.e-6usingrecursive
%adaptiveSimpsonquadrature.ThefunctionY=FUN(X)should
%acceptavectorargumentXandreturnavectorresultY,the
%integrandevaluatedateachelementofX.
%
%Q=QUAD(FUN,A,B,TOL)usesanabsoluteerrortoleranceofTOL
%insteadofthedefault,whichis1.e-6.LargervaluesofTOL
%resultinfewerfunctionevaluationsandfastercomputation,
%butlessaccurateresults.TheQUADfunctioninMATLAB5.3used
%alessreliablealgorithmandadefaulttoleranceof1.e-3.
%
%[Q,FCNT]=QUAD(...)returnsthenumberoffunctionevaluations.
%
%QUAD(FUN,A,B,TOL,TRACE)withnon-zeroTRACEshowsthevalues
%of[fcntab-aQ]duringtherecursion.
%
%QUAD(FUN,A,B,TOL,TRACE,P1,P2,...)providesforadditional
%argumentsP1,P2,...tobepasseddirectlytofunctionFUN,
%FUN(X,P1,P2,...).PassemptymatricesforTOLorTRACEto
%usethedefaultvalues.
%
%Usearrayoperators.*,./and.^inthedefinitionofFUN
%sothatitcanbeevaluatedwithavectorargument.
%
%FunctionQUADLmaybemoreefficientwithhighaccuracies
%andsmoothintegrands.
%
%Example:
%FUNcanbespecifiedas:
%
%Ananonymousfunction:
%F=@(x)1./(x.^3-2*x-5);
%Q=quad(F,0,2);
%
%Afunctionhandle:
%Q=quad(@myfun,0,2);
%wheremyfun.misanM-file:
%functiony=myfun(x)
%y=1./(x.^3-2*x-5);
%
%ClasssupportforinputsA,B,andtheoutputofFUN:
%float:
double,single
%
%SeealsoQUADV,QUADL,DBLQUAD,TRIPLEQUAD,@.
%Basedon"adaptsim"byWalterGander.
%Ref:
W.GanderandW.Gautschi,"AdaptiveQuadratureRevisited",1998.
%http:
//www.inf.ethz.ch/personal/gander
%Copyright1984-2004TheMathWorks,Inc.
%$Revision:
5.26.4.3$$Date:
2004/03/2403:
05:
30$
f=fcnchk(funfcn);
ifnargin<4||isempty(tol),tol=1.e-6;end;
ifnargin<5||isempty(trace),trace=0;end;
%Initializewiththreeunequalsubintervals.
h=0.13579*(b-a);
x=[aa+ha+2*h(a+b)/2b-2*hb-hb];
y=f(x,varargin{:
});
fcnt=7;
%Fudgeendpointstoavoidinfinities.
if~isfinite(y
(1))
y
(1)=f(a+eps(superiorfloat(a,b))*(b-a),varargin{:
});
fcnt=fcnt+1;
end
if~isfinite(y(7))
y(7)=f(b-eps(superiorfloat(a,b))*(b-a),varargin{:
});
fcnt=fcnt+1;
end
%Calltherecursivecoreintegrator.
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);
switchwarn
case1
warning('