2、urn;endfork=i+1:nm=0;forq=1:i-1m=m+L(k,q)*U(q,i);endL(k,i)=(A(k,i)-m)/U(i,i);endendLUend1.结果>>LU_decom(A)m=4n=4L=1000210012103321U=24260123003600015.拓展在编写程序过程中由于角标较多因此在运行过程中出现了不少角标不对的错误题目;给出函数f(x)=1/(1+25x^2),求f(x在[-1,1]上取5个和9个等距节点,做最小二乘拟合,得出均方误差方误差。五个节点时,matlab编码为:首先建立M文
3、件,并保存functiony=f(x)y=1/(1+25*x^2);endx=[-1-0.500.51];fori=1:5y(i)=f(x(i));enda=polyfit(x,y,3)symsxf1=a(1)*x^3+a(2)*x^2+a(3)*x+a(4)x=[-1-0.500.51];fori=1:5E(i)=(f(x(i))-(a(1)*x(i)^3+a(2)*x(i)^2+a(3)*x(i)+a(4)))^2;endsum(E)输出结果为a=-0.0000-0.6063-0.00000.5737f1=-47781/4*x^3-1
4、600/2639*x^2-30703/*x+1514/2639(拟合的多项式)ans=0.3534(均方误差)九个点的时候,matlab编码为:x=[-1-0.75-0.5-0.2500.250.50.751];fori=1:9y(i)=f(x(i));enda=polyfit(x,y,3)symsxf2=a(1)*x^3+a(2)*x^2+a(3)*x+a(4)x=[-1-0.75-0.5-0.2500.250.50.751];fori=1:5E1(i)=(f(x(i))-(a(1)*x(i)^3+a(2)*x(i)^2+a(3)*x(
5、i)+a(4)))^2;endsum(E1)输出结果为:a=-0.0000-0.56090.00000.4855f2=-6039/*x^3-12921/85248*x^2+54361/*x+64671/85248(最小二乘拟合多项式)ans=0.3350(均方误差)用复合梯形公式求积分的值。functioni=combinetraprl(f,a,b,eps)%复化梯形公式求函数f在区间[a,b]上的定积分%函数名:f%积分下限:a%积分上限:b%积分精度:eps%积分值:i%积分划分的子区间个数:stepn=1;h=(b-a)/2;i1=
6、0;i2=(subs(sym(f),findsym(sym(f)),a)+subs(sym(f),findsym(sym(f)),b))/h;whileabs(i2-i1)>epsn=n+1;h=(b-a)/n;i1=i2;i2=0;fori=0:n-1x=a+h*i;x1=x+h;i2=i2+(h/2)*(subs(sym(f),findsym(sym(f)),x)+...subs(sym(f),findsym(sym(f)),x1));endend四阶龙格-库塔法分别求解下列初值问题;function[x,y]=runge_kutta
7、(fun,x0,xt,y0,pointnum,varargin)ifnargin<3y0=0;endy(1,:)=y0(:)';h=(xt-x0)/(pointnum-1);x=x0+[0:pointnum]'*h;fork=1:pointnumf1=h*feval(fun,x(k),y(k,:),varargin{:});f1=f1(:)';f2=h*feval(fun,x(k)+h/2,y(k,:)+f1/2,varargin{:});f2=f2(:)';f3=h*feval(fun,x(k)+h/2,y(k,:)+f2/2,vara
8、rgin{:});f3=f3(:)';f4=h*feval(fun,x(k)+h,y(k,:)+f3,varargin{:});f4=f4(:)';y(k+1,:)=y(k,:)+(f1+2*