数值分析五个题目的C语言及Matlab程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

本文档包含上一个文档中的五个数值分析实验题C语言程序及Matlab程序实验一C程序#includestdio.h#includemath.hvoidmain(){inti=0;floata=0.1,b=1.9,t=0.0,e=1.9;if((pow(a,7)-28*pow(a,4)+14)*(pow(b,7)-28*pow(b,4)+14)0)if((7*pow(x,6)-112*pow(x,3)))printf(x=%f,i=%d,e=%f\n,x,i,e);for(i=1;i7&&e0.00001;i++){t=x;x=x-(pow(x,7)-28*pow(x,4)+14)/(7*pow(x,6)-112*pow(x,3));e=fabs(t-x);printf(x=%f,i=%d,e=%f\n,x,i,e);}}Matable程序i=0;x=1.9;t=0.0;e=1.9;disp(['i=',num2str(i),'','x=',num2str(x),'','e=',num2str(e)]);fori=1:7t=x;x=x-(x^7-28*x^4+14)/(7*x^6-112*x^3);e=abs(t-x);disp(['i=',num2str(i),'','x=',num2str(x),'','e=',num2str(e)]);ife0.00001break;endend实验二C程序#includestdio.h#includemath.h//已知量doublex[10]={1,2,3,4,5,6,7,8,9,10};doublefd1=1,fd2=0.1;doublefx[10]={0,0.69314718,1.0986123,1.3862944,1.6094378,1.7917595,1.9459101,2.079445,2.1972246,2.3025851};//函数声明voidcanshu(double*h,double*r,double*u,double*d);voidwanju(double*u,double*r,double*d,double*m);doubleyths(double*m,double*h,doublep);doubleytdhs(double*m,double*h,doublep);//主函数voidmain(){doublep,q1,q2;doubleh[9],u[9],r[9],m[10],d[10];//矩阵Am=d参数数据u[8]=1,r[0]=1;canshu(h,r,u,d);wanju(u,r,d,m);printf(请输入待求数值:x=);scanf(%lf,&p);q1=yths(m,h,p);q2=ytdhs(m,h,p);printf(输出结果为:\nf(%lf)=%lf\nf'(%lf)=%lf\n,p,q1,p,q2);}//求参函数voidcanshu(double*h,double*r,double*u,double*d){inti,j;doublea[10];for(i=0;i9;i++){h[i]=*(x+i+1)-*(x+i);a[i]=(fx[i]-fx[i+1])/(x[i]-x[i+1]);j=i-1;if(j-1){r[i]=h[j+1]/(h[j+1]+h[j]);u[j]=h[j]/(h[j]+h[j+1]);d[i]=(6/(h[j]+h[i]))*(a[i]-a[j]);}}d[0]=(6/h[0])*(a[i-9]-fd1);d[9]=(6/h[i-1])*(fd2-a[i-1]);}//追赶法求弯矩向量mvoidwanju(double*u,double*r,double*d,double*m){inti,j;doublel[9],n[10],z[10];n[0]=2;z[0]=d[0];for(i=0;i9;i++){l[i]=u[i]/n[i];n[i+1]=2-l[i]*r[i];z[i+1]=d[i+1]-l[i]*z[i];}m[9]=z[9]/n[9];for(j=8;j-1;j--){m[j]=(z[j]-r[j]*m[j+1])/n[j];}}//三次样条插值函数doubleyths(double*m,double*h,doublep){inti,j=0;doubleq;for(i=0;i10;i++){if(x[i]=(p-1)&&x[i+1]=(p-1))break;}q=pow(x[i+1]-p,3)/6/h[i+1]*m[i]+pow(p-x[i],3)/6/h[i+1]*m[i+1]+(fx[i]-pow(h[i+1],2)*m[i]/6)*(x[i+1]-p)/h[i+1]+(fx[i+1]-pow(h[i+1],2)*m[i+1]/6)*(p-x[i])/h[i+1];returnq;}//三次样条插值导函数doubleytdhs(double*m,double*h,doublep){inti,j=0;doubleq;for(i=0;i10;i++){if(x[i]=(p-1)&&x[i+1]=(p-1))break;}q=-pow(x[i+1]-p,2)/2/h[i+1]*m[i]+pow(p-x[i],2)/2/h[i+1]*m[i+1]-(fx[i]-pow(h[i+1],2)*m[i]/6)/h[i+1]+(fx[i+1]-pow(h[i+1],2)*m[i+1]/6)/h[i+1];returnq;}Matlab程序function[h,r,u,d]=canshu(x,fx,h,r,u,d,fd1,fd2)%UNTITLED10Summaryofthisfunctiongoeshere%Detailedexplanationgoesherefori=1:9h(i)=x(i+1)-x(i);a(i)=(fx(i)-fx(i+1))/(x(i)-x(i+1));j=i-1;ifj0r(i)=h(j+1)/(h(j+1)+h(j));u(j)=h(j)/(h(j)+h(j+1));d(i)=(6/(h(j)+h(i)))*(a(i)-a(j));endendd(1)=(6/h(1))*(a(i-8)-fd1);d(10)=(6/h(i))*(fd2-a(i));endglobalxfd1fd2fxhdmru;x=[1,2,3,4,5,6,7,8,9,10];fd1=1;fd2=0.1;fx=[0,0.69314718,1.0986123,1.3862944,1.6094378,1.7917595,1.9459101,2.079445,2.1972246,2.3025851];u(9)=1;r(1)=1;[h,r,u,d]=canshu(x,fx,h,r,u,d,fd1,fd2);[m]=wanju(u,r,d,m);p=input('请输入待求数值:x=');q1=yths(m,h,p,x,fx);q2=ytdhs(m,h,p,fx,x);disp(['输出结果为:','f(',num2str(p),')=',num2str(q1),',','f(',num2str(p),')=',num2str(q2)]);function[m]=wanju(u,r,d,m)%UNTITLED12Summaryofthisfunctiongoeshere%Detailedexplanationgoesheren(1)=2;z(1)=d(1);fori=1:9l(i)=u(i)/n(i);n(i+1)=2-l(i)*r(i);z(i+1)=d(i+1)-l(i)*z(i);endm(10)=z(10)/n(10);forj=9:-1:1m(j)=(z(j)-r(j)*m(j+1))/n(j);endendfunction[q]=ytdhs(m,h,p,fx,x)%UNTITLED14Summaryofthisfunctiongoeshere%Detailedexplanationgoesherefori=1:10ifx(i)=(p)&&x(i+1)=(p)break;endq=-(x(i+1)-p)^2/2/h(i+1)*m(i)+(p-x(i))^2/2/h(i+1)*m(i+1)-(fx(i)-(h(i+1))^2*m(i)/6)/h(i+1)+(fx(i+1)-(h(i+1))^2*m(i+1)/6)/h(i+1);endendfunction[q]=yths(m,h,p,x,fx)%UNTITLED13Summaryofthisfunctiongoeshere%Detailedexplanationgoesherefori=1:10ifx(i)=(p)&&x(i+1)=(p)break;endq=(x(i+1)-p)^3/6/h(i+1)*m(i)+(p-x(i))^3/6/h(i+1)*m(i+1)+(fx(i)-(h(i+1))^2*m(i)/6)*(x(i+1)-p)/h(i+1)+(fx(i+1)-(h(i+1))^2*m(i+1)/6)*(p-x(i))/h(i+1);endend实验三C程序#includestdio.h#includemath.h//已知量doubleb=3,a=1;//函数声明doubleromberg();doublefhs(doublex);//主函数voidmain(){doubleq;q=romberg();printf(输出结果为:r=%lf\n,q);}//Romberg算法doubleromberg(){intn,j,k=0,m=0,z=0,i;doublee=1,qh=0,q,h,x0,r1=0,t[50],s[50],c[50],r[50];t[0]=(b-a)/2*(fhs(a)+fhs(b));for(i=1;1;i++){n=pow(2,i-1);h=(b-a)/n;x0=a+0.5*h;for(j=0;jn;j++){qh+=fhs(x0+j*h);}t[i]=0.5*(t[i-1]+h*qh);qh=0;if(i=3){for(k;ki;k++)s[k]=(4*t[k+1]-t[k])/3;for(m;mk-1;m++)c[m]=(16*s[m+1]-s[m])/15;for(z;zm-1;z++)r[z]=(64*c[z+1]-c[z])/63;e=fabs(r1-r[z-1]);r1=r[z-1];}if(e0.00001)break;}q=r[z-1];returnq;}//f函数doublefhs(doublex){doubley;y=pow(3,x)*pow(x,14)*(5*x+7)*sin(pow(x,2));returny;}Matlab程序function[y]=fhs(x)%UNTITLED4Summaryofthisfunctiongoeshere%Detailedexplanationgoesherey=3^x*x^14*(5*x+7)*sin(x^2);endfunction[q]=romberg()%UNTITLED3Summaryofthisfunctiongoeshere%Detailedexplanationgoeshereglobalbanjkmzieqhhx0;t=zeros(1,20);s=zeros(1,20);c=zeros(1,20);r=zeros(1,20);k=1;m=1;z=1;e=1;r1=0;t(1)=(b-a)/2*(fhs(a)+fhs(b));qh=0;fori=1:20n=2^(i-1);h=(b-a)/n;x0=a+0.5*h;forj=0:n-1qh=qh+fhs(x0+j*h);endt(i+1)=0.5*(t(i)+h*qh);qh=0;ifi=4fork=k:is(k)=(4*t(k+1)-t(k))

1 / 25
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功