数值分析上机题1设2211NNjSj,其精确值为1311221NN。(1)编制按从大到小的顺序22211121311NSN,计算NS的通用程序。(2)编制按从小到大的顺序2221111(1)121NSNN,计算NS的通用程序。(3)按两种顺序分别计算210S,410S,610S,并指出有效位数。(编制程序时用单精度)(4)通过本上机题,你明白了什么?程序代码(matlab编程):clccleara=single(1./([2:10^7].^2-1));S1(1)=single(0);S1(2)=1/(2^2-1);forN=3:10^2S1(N)=a(1);fori=2:N-1S1(N)=S1(N)+a(i);endendS2(1)=single(0);S2(2)=1/(2^2-1);forN=3:10^2S2(N)=a(N-1);fori=linspace(N-2,1,N-2)S2(N)=S2(N)+a(i);endendS1表示按从大到小的顺序的SNS2表示按从小到大的顺序的SN计算结果从大到小的顺序的值从小到大的顺序的值精确值有效位数从大到小从小到大210S0.7400490.740050.74004965410S0.7498520.74990.749944610S0.7498520.7499990.74999936通过本上机题,看出按两种不同的顺序计算的结果是不相同的,按从大到小的顺序计算的值与精确值有较大的误差,而按从小到大的顺序计算的值与精确值吻合。从大到小的顺序计算得到的结果的有效位数少。计算机在进行数值计算时会出现“大数吃小数”的现象,导致计算结果的精度有所降低,我们在计算机中进行同号数的加法时,采用绝对值较小者先加的算法,其结果的相对误差较小。数值分析上机题220.(上机题)Newton迭代法(1)给定初值0x及容许误差,编制Newton法解方程()0fx根的通用程序。(2)给定方程3()/30fxxx,易知其有三个根13x,20x,33x。1.由Newton方法的局部收敛性可知存在0,当0(,)x时,Newton迭代序列收敛于根2x。试确定尽可能大的。2.试取若干初始值,观察当0(,1)x,(1,),(,),(,1),(1,)时Newton序列是否收敛以及收敛于哪一个根。MATLAB程序问题1clccleardx=0.5;x(1)=0.5;while(dx1e-6)i=1;error=1;while(error1e-8)x(i+1)=x(i)-(1/3*x(i)^3-x(i))/(x(i)^2-1);error=abs(x(i+1)-x(i));i=i+1;endif(x(i)==0)x(1)=x(1)+dx;elsedx=dx/2;x(1)=x(1)-dx;endend经计算,最大的为0.774596问题2clcclearx2(1)=1e14;i=1;error=1;while(error1e-8)x2(i+1)=x2(i)-(1/3*x2(i)^3-x2(i))/(x2(i)^2-1);error=abs(x2(i+1)-x2(i));i=i+1;if(i1e4)breakendend对于不同得初始值收敛于不同的根,0x在(-∞,-1)内收敛于3,在(-0.774,0.774)内收敛于0,在(1,+∞)内收敛于3,但在内(0.774,1)和(-1,0.774)均可能收敛于3和3。分析:对于不同的初值,迭代序列会收敛于不同的根,所以在某个区间内求根对于初值的选取有很大的关系。产生上述结果的原因是区间不满足大范围收敛的条件。数值分析上机题339.(上机题)列主元三角分解法对于某电路的分析,归结为求解线性方程组RI=V。(1)编制解n阶线性方程组Ax=b的列主元三角分解法的通用程序;(2)用所编制的程序解线性方程组RI=V,并打印出解向量,保留五位有效数;(3)本编程之中,你提高了哪些编程能力?程序:clcclearA=[31,-13,0,0,0,-10,0,0,0-13,35,-9,0,-11,0,0,0,00,-9,31,-10,0,0,0,0,00,0,-10,79,-30,0,0,0,-90,0,0,-30,57,-7,0,-5,00,0,0,0,-7,47,-30,0,00,0,0,0,0,-30,41,0,00,0,0,0,-5,0,0,27,-20,0,0,-9,0,0,0,-2,29];b=[-15,27,-23,0,-20,12,-7,7,10]';[m,n]=size(A);Ap=[A,b];x=zeros(n,1);fori=1:m-1j=i;[maxa,maxi]=max(abs(Ap(i:end,j)));maxi=maxi+i-1;if(maxa~=0)mid=Ap(maxi,:);Ap(maxi,:)=Ap(i,:);Ap(i,:)=mid;fork=i:mAp(i+1:m,:)=Ap(i+1:m,:)-Ap(i+1:m,j)*(Ap(i,:)./maxa);endendendfori=linspace(m,1,m)x(i)=(Ap(i,end)-Ap(i,1:end-1)*x)/Ap(i,i);end结果:方程的解为(保留5位有效数字):x1=-0.28923,x2=0.34544,x3=-0.71281,x4=-0.22061,x5=-0.43040,x6=0.15431,x7=-0.057823,x8=0.20105,x9=0.29023。习题437.(上机题)3次样条插值函数(1)编制求第一型3次样条插值函数的通用程序;(2)已知汽车曲线型值点的数据如下:ix012345678910iy2.513.304.044.705.225.545.785.405.575.705.80端点条件为'0y=0.8,'10y=0.2。用所编制程序求车门的3次样条插值函数S(x),并打印出S(i+0.5)(i=0,1,…9)。程序:(1)clcclear%%x=[0,1,2,3,4,5,6,7,8,9,10];y=[2.51,3.30,4.04,4.7,5.22,5.54,5.78,5.40,5.57,5.70,5.80];y1=0.8;yend=0.2;%%___________________________________________n=size(x,2)-1;h=x(2:end)-x(1:end-1);miu=h(1:end-1)./(h(1:end-1)+h(2:end));lamda=1-miu;f1=[y1,(y(2:end)-y(1:end-1))./h,yend];%f[xn-1,xn]f2=[f1(2:end)-f1(1:end-1)]./[h(1),h(1:end-1)+h(2:end),h(end)];%f[xn-1,xn,xn+1]A=2.*eye(n+1);A(2:end,1:end-1)=A(2:end,1:end-1)+diag([miu,1]');A(1:end-1,2:end)=A(1:end-1,2:end)+diag([1,lamda]');M=A\(6*f2');Sx=[y(1:end-1)',((y(2:end)-y(1:end-1))./h)'-((1/3*M(1:end-1)+1/6*M(2:end)).*h'),1/2*M(1:end-1),1/6*(M(2:end)-M(1:end-1))./h'];%%xx=input(’x=’);forj=2:n+1ifxxx(j)S=Sx(j-1,:)*[1,xx-x(j-1),(xx-x(j-1))^2,(xx-x(j-1))^3]';breakendend(2)clcclear%%x=[0,1,2,3,4,5,6,7,8,9,10];y=[2.51,3.30,4.04,4.7,5.22,5.54,5.78,5.40,5.57,5.70,5.80];y1=0.8;yend=0.2;%%___________________________________________n=size(x,2)-1;h=x(2:end)-x(1:end-1);miu=h(1:end-1)./(h(1:end-1)+h(2:end));lamda=1-miu;f1=[y1,(y(2:end)-y(1:end-1))./h,yend];%f[xn-1,xn]f2=[f1(2:end)-f1(1:end-1)]./[h(1),h(1:end-1)+h(2:end),h(end)];%f[xn-1,xn,xn+1]A=2.*eye(n+1);A(2:end,1:end-1)=A(2:end,1:end-1)+diag([miu,1]');A(1:end-1,2:end)=A(1:end-1,2:end)+diag([1,lamda]');M=A\(6*f2');Sx=[y(1:end-1)',((y(2:end)-y(1:end-1))./h)'-((1/3*M(1:end-1)+1/6*M(2:end)).*h'),1/2*M(1:end-1),1/6*(M(2:end)-M(1:end-1))./h'];%%fori=0:9xx=i+0.5;forj=2:n+1ifxxx(j)S(i+1)=Sx(j-1,:)*[1,xx-x(j-1),(xx-x(j-1))^2,(xx-x(j-1))^3]';breakendendendx∈[0,1]时;S(x)=2.51+0.8x-0.0014861x2-0.00851395x3x∈[1,2]时;S(x)=3.3+0.771486(x-1)-0.027028(x-1)2-0.00445799(x-1)3x∈[2,3]时;S(x)=4.04+0.704056(x-2)-0.0404019(x-2)2-0.0036543(x-2)3x∈[3,4]时;S(x)=4.7+0.612289(x-3)-0.0513648(x-3)2-0.0409245(x-3)3x∈[4,5]时;S(x)=5.22+0.386786(x-4)-0.174138(x-4)2+0.107352(x-4)3x∈[5,6]时;S(x)=5.54+0.360567(x-5)+0.147919(x-5)2-0.268485(x-5)3x∈[6,7]时;S(x)=5.78-0.149051(x-6)-0.657537(x-6)2+0.426588(x-6)3x∈[7,8]时;S(x)=5.4-0.184361(x-7)+0.622227(x-7)2-0.267865(x-7)3x∈[8,9]时;S(x)=5.57+0.256496(x-8)-0.181369(x-8)2+0.0548728(x-8)3x∈[9,10]时;S(x)=5.7+0.058376(x-9)-0.0167508(x-9)2+0.0583752(x-9)3S(0.5)=2.90856S(1.5)=3.67843S(2.5)=4.38147S(3.5)=4.98819S(4.5)=5.38328S(5.5)=5.7237S(6.5)=5.59441S(7.5)=5.42989S(8.5)=5.65976S(9.5)=5.7323习题五重积分的计算23(上机题)重积分的计算题目:给定积分dcbadydxyxffI)),(()(。取初始步长h和k,及精度。应用复化梯形公式,采用逐次二分步长的方法,编制计算I(f)的通用程序。计算至相邻两次近似值之差的绝对值不超过为止。1)用所编程序计算积分603022))(()(dydxyxtgfI,取510*5.0。程序:clcclear%%examplef=inline('tan(x.^2+y.^2)','x','y');a=0;b=pi/