Newton法及改进的Newton法源程序:clear%%%%输入函数f=input('请输入需要求解函数','s')%%%求解f(x)的导数df=diff(f);%%%改进常数或重根数miu=2;%%%初始值x0x0=input('inputinitialvaluex0');k=0;%迭代次数max=100;%最大迭代次数R=eval(subs(f,'x0','x'));%求解f(x0),以确定初值x0时否就是解while(abs(R)1e-8)x1=x0-miu*eval(subs(f,'x0','x'))/eval(subs(df,'x0','x'));R=x1-x0;x0=x1;k=k+1;if(eval(subs(f,'x0','x'))1e-10);breakendifkmax;%如果迭代次数大于给定值,认为迭代不收敛,重新输入初值ss=input('mayberesultiserror,chooseanewx0,y/n?','s');ifstrcmp(ss,'y')x0=input('inputinitialvaluex0');k=0;elsebreakendendendk;%给出迭代次数x=x0;%给出解Gauss消元法源程序:cleara=input('输入系数阵:\n')b=input('输入列阵b:\n')n=length(b);A=[ab]x=zeros(n,1);%%%函数主体Yipusilong-1;fork=1:n-1;%%%是否进行主元选取ifabs(A(k,k))yipusilong;%事先给定的认为有必要选主元的小数yzhuyuan=1;elseyzhuyuan=0;endifyzhuyuan;%%%%选主元t=A(k,k);forr=k+1:n;ifabs(A(r,k))abs(t)p=r;elsep=k;endend%%%交换元素ifp~=k;forq=k:n+1;s=A(k,q);A(k,q)=A(p,q);A(p,q)=s;endendend%%%判断系数矩阵是否奇异或病态非常严重ifabs(A(k,k))yipusilongdisp(‘矩阵奇异,解可能不正确’)end%%%%计算消元,得三角阵forr=k+1:n;m=A(r,k)/A(k,k);forq=k:n+1;A(r,q)=A(r,q)-A(k,q)*m;endendend%%%%求解xx(n)=A(n,n+1)/A(n,n);fork=n-1:-1:1;s=0;forr=k+1:n;s=s+A(k,r)*x(r);endt=(A(k,n+1)-s)x(k)=(A(k,n+1)-s)/A(k,k)endLagrange插值源程序:n=input('将区间分为的等份数输入:\n');s=[-1+2/n*[0:n]];%%%给定的定点,Rf为给定的函数x=-1:0.01:1;f=0;forq=1:n+1;l=1;%求插值基函数fork=1:n+1;ifk~=q;l=l.*(x-s(k))./(s(q)-s(k));elsel=l;endendf=f+Rf(s(q))*l;%求插值函数endplot(x,f,'r')%作出插值函数曲线gridonholdon分段线性插值源程序clearn=input('将区间分为的等份数输入:\n');s=[-1+2/n*[0:n]];%%%给定的定点,Rf为给定的函数m=0;hh=0.001;forx=-1:hh:1;ff=0;fork=1:n+1;%%%求插值基函数switchkcase1ifx=s(2);l=(x-s(2))./(s(1)-s(2));elsel=0;endcasen+1ifxs(n);l=(x-s(n))./(s(n+1)-s(n));elsel=0;endotherwiseifx=s(k-1)&x=s(k);l=(x-s(k-1))./(s(k)-s(k-1));elseifx=s(k)&x=s(k+1);l=(x-s(k+1))./(s(k)-s(k+1));elsel=0;endendendff=ff+Rf(s(k))*l;%%求插值函数值endm=m+1;f(m)=ff;end%%%作出曲线x=-1:hh:1;plot(x,f,'r');gridonholdon三次样条插值源程序:(采用第一边界条件)clearn=input('将区间分为的等份数输入:\n');%%%插值区间a=-1;b=1;hh=0.001;%画图的步长s=[a+(b-a)/n*[0:n]];%%%给定的定点,Rf为给定的函数%%%%第一边界条件Rf(-1),Rf(1)v=5000*1/(1+25*a*a)^3-50/(1+25*a*a)^4;fork=1:n;%取出节点间距h(k)=s(k+1)-s(k);endfork=1:n-1;%求出系数向量lamuda,miula(k)=h(k+1)/(h(k+1)+h(k));miu(k)=1-la(k);end%%%%赋值系数矩阵Afork=1:n-1;forp=1:n-1;switchpcasekA(k,p)=2;casek-1A(k,p)=miu(p+1);casek+1A(k,p)=la(p-1);otherwiseA(k,p)=0;endendend%%%%求出d阵fork=1:n-1;switchkcase1d(k)=6*f2c([s(k)s(k+1)s(k+2)])-miu(k)*v;casen-1d(k)=6*f2c([s(k)s(k+1)s(k+2)])-la(k)*v;otherwised(k)=6*f2c([s(k)s(k+1)s(k+2)]);endend%%%%求解M阵M=A\d';M=[v;M;v];%%%%m=0;f=0;forx=a:hh:b;ifx==a;p=1;elsep=ceil((x-s(1))/((b-a)/n));endff1=0;ff2=0;ff3=0;ff4=0;m=m+1;ff1=1/h(p)*(s(p+1)-x)^3*M(p)/6;ff2=1/h(p)*(x-s(p))^3*M(p+1)/6;ff3=((Rf(s(p+1))-Rf(s(p)))/h(p)-h(p)*(M(p+1)-M(p))/6)*(x-s(p));ff4=Rf(s(p))-M(p)*h(p)*h(p)/6;f(m)=ff1+ff2+ff3+ff4;end%%%作出插值图形x=a:hh:b;plot(x,f,'k')holdongridon多项式最小二乘法源程序clear%%%给定测量数据点(s,f)s=[3456789];f=[2.012.983.505.025.476.027.05];%%%计算给定的数据点的数目n=length(f);%%%给定需要拟合的数据的最高次多项式的次数m=10;%%%程序主体fork=0:m;g=zeros(1,m+1);forj=0:m;t=0;fori=1:n;%计算内积(fai(si),fai(si))t=t+fai(s(i),j)*fai(s(i),k);endg(j+1)=t;endA(k+1,:)=g;%法方程的系数矩阵t=0;fori=1:n;%计算内积(f(si),fai(si))t=t+f(i)*fai(s(i),k);endb(k+1,1)=t;enda=A\b%求出多项式系数x=[s(1):0.01:s(n)]';y=0;fori=0:m;y=y+a(i+1)*fai(x,i);endplot(x,y)%作出拟合成的多项式的曲线gridonholdonplot(s,f,'rx')%在上图中标记给定的点表中,L10(x)为Lagrange插值的10次多项式,S10(x),S40(x)分别代表n=10,40的三次样条插值函数,X10(x),X40(x)分别代表n=10,40的线性分段插值函数。xf(x)L10(x)S10(x)S40(x)X10(x)X40(x)-1.000000000000000.038461538461540.038461538461540.038461538461540.038461538461540.038461538461540.03846153846154-0.950000000000000.042440318302391.923631149719200.042408331510400.042440318302390.043552036199100.04244031830239-0.900000000000000.047058823529411.578720990349260.047096975854580.047058823529410.048642533936650.04705882352941-0.850000000000000.052459016393440.719459128379820.052558399239790.052459016393440.053733031674210.05245901639344-0.800000000000000.058823529411760.058823529411760.058823529411760.058823529411760.058823529411760.05882352941176-0.750000000000000.06639004149378-0.231461749896740.066039861727440.066390041493780.069117647058820.06639004149378-0.700000000000000.07547169811321-0.226196289062500.074821161988660.075471698113210.079411764705880.07547169811321-0.650000000000000.08648648648649-0.072604203224180.085897763608490.086486486486490.089705882352940.08648648648649-0.600000000000000.100000000000000.100000000000000.100000000000000.100000000000000.100000000000000.10000000000000-0.550000000000000.116788321167880.215591878912570.117838330177130.116788321167880.125000000000000.11678832116788-0.500000000000000.137931034482760.253755457261030.140043715557300.137931034482760.150000000000000.13793103448276-0.450000000000000.164948453608250.234968543052670.167227243158830.164948453608250.175000000000000.16494845360825-0.400000000000000.200000000000000.200000000000000.200000000000000.200000000000000.200000000000000.20000000000000-0.350000000000000.246153846153850.190580466753760.240547994034640.246153846153850.275000000000000.24615384615385-0.300000000000000.307692307692310.235346591310800.297356916958600.3076923076