本文档包含上一个文档中的五个数值分析实验题C语言程序及Matlab程序实验一C程序#include "stdio.h"#include "math.h"void main(){inti=0;float a=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;i<7&&e>0.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)]); for i=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)]);if e<0.00001break;endend实验二C程序#include"stdio.h"#include"math.h"//已知量double x[10]={1,2,3,4,5,6,7,8,9,10};double fd1=1,fd2=0.1;double fx[10]={0,0.69314718,1.0986123,1.3862944,1.6094378,1.7917595,1.9459101,2.079445,2.1972246,2.3025851}; //函数声明void canshu(double *h,double *r,double *u,double *d);void wanju(double *u,double *r,double *d,double *m);double yths(double *m,double *h,double p);double ytdhs(double *m,double *h,double p);//主函数void main(){double p,q1,q2;double h[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);}//求参函数void canshu(double *h,double *r,double *u,double *d) {inti,j;double a[10];for(i=0;i<9;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]);}//追赶法求弯矩向量mvoid wanju(double *u,double *r,double *d,double *m){inti,j;double l[9],n[10],z[10];n[0]=2;z[0]=d[0];for(i=0;i<9;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];}}//三次样条插值函数double yths(double *m,double *h,double p) {inti,j=0;double q;for(i=0;i<10;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];return q;}//三次样条插值导函数double ytdhs(double *m,double *h,double p){inti,j=0;double q;for(i=0;i<10;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];return q;}Matlab程序function[h,r,u,d]=canshu(x,fx,h,r,u,d,fd1,fd2)%UNTITLED10 Summary of this function goes here % Detailed explanation goes herefor i=1:9h(i)=x(i+1)-x(i);a(i)=(fx(i)-fx(i+1))/(x(i)-x(i+1));j=i-1;if j>0r(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));endglobal x fd1 fd2 fx h d m r u;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.19 72246,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)%UNTITLED12 Summary of this function goes here% Detailed explanation goes heren(1)=2;z(1)=d(1);for i=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);for j=9:-1:1m(j)=(z(j)-r(j)*m(j+1))/n(j);endendfunction [q] =ytdhs(m,h,p,fx,x)%UNTITLED14 Summary of this function goes here% Detailed explanation goes herefor i=1:10if x(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)%UNTITLED13 Summary of this function goes here% Detailed explanation goes herefor i=1:10if x(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程序#include"stdio.h"#include"math.h"//已知量double b=3,a=1;//函数声明double romberg();double fhs(double x);//主函数void main(){ double q;q=romberg();printf("输出结果为:r=%lf\n",q);}//Romberg算法double romberg(){intn,j,k=0,m=0,z=0,i;double e=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;j<n;j++){qh+=fhs(x0+j*h);}t[i]=0.5*(t[i-1]+h*qh);qh=0;if(i>=3){for(k;k<i;k++)s[k]=(4*t[k+1]-t[k])/3;for(m;m<k-1;m++)c[m]=(16*s[m+1]-s[m])/15;for(z;z<m-1;z++)r[z]=(64*c[z+1]-c[z])/63;e=fabs(r1-r[z-1]);r1=r[z-1];}if(e<0.00001)break;}q=r[z-1];return q;}//f函数double fhs(double x){double y;y=pow(3,x)*pow(x,14)*(5*x+7)*sin(pow(x,2));return y;}Matlab程序function [y] =fhs(x)%UNTITLED4 Summary of this function goes here % Detailed explanation goes herey=3^x*x^14*(5*x+7)*sin(x^2);endfunction[q]=romberg()%UNTITLED3 Summary of this function goes here % Detailed explanation goes hereglobal b a n j k m z i e qh h x0;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;for i=1:20n=2^(i-1);h=(b-a)/n;x0=a+0.5*h;for j=0:n-1qh=qh+fhs(x0+j*h);endt(i+1)=0.5*(t(i)+h*qh);qh=0;if i>=4for k=k:is(k)=(4*t(k+1)-t(k))/3;endfor m=m:k-1c(m)=(16*s(m+1)-s(m))/15;endfor z=z:m-1r(z)=(64*c(z+1)-c(z))/63;ende=abs(r1-r(z-1));r1=r(z-1);endif(e<0.00001)break;endendq=r(z-1);endglobal b a k m z e qh r1;k=0;m=0;z=0;e=1;qh=0;r1=0;b=3;a=1;[q]=romberg();disp(['输出结果为:r=',num2str(q)]);实验四C程序#include"stdio.h"#include"math.h"//已知量double x=0,h=0.0005,y[3]={0},p[4]={0.025,0.045,0.085,0.1}; double fx(double x,double y[3],int t);void main(){inti,t;double k[4],m[3];for(i=0;i<4;i++){for(x=0;x<=p[i];x=x+h){for(t=0;t<3;t++)m[t]=y[t];for(t=0;t<3;t++){k[0]=fx(x,m,t);m[t]=y[t]+0.5*h*k[0];k[1]=fx(x+0.5*h,m,t);m[t]=y[t]+0.5*h*k[1];k[2]=fx(x+0.5*h,m,t);m[t]=y[t]+h*k[2];k[3]=fx(x+h,m,t);//m[t]=y[t];y[t]=y[t]+h*(k[0]+2*k[1]+2*k[2]+k[3])/6;}}x=0;for(t=0;t<3;t++)printf("y%d(%lf)=%lf\t",t+1,p[i],y[t]);printf("\n");for(t=0;t<3;t++)y[t]=0;}}double fx(double x,double y[3],int t){double f;if(t==0)f=0*x+1;else if(t==1)f=0*x+y[2];elsef=0*x+1000-1000*y[1]-100*y[2];return f;}Matlab程序function[f]=fx(x,y)%UNTITLED7 Summary of this function goes here % Detailed explanation goes heref(1)=0*x+1;f(2)=0*x+y(3);f(3)=0*x+1000-1000*y(2)-100*y(3);endy=zeros(1,3);h=0.0005;x=0;p=[0.025,0.045,0.085,0.1];k=zeros(4,3);for i=1:4for j=1:fix(p(i)/h)k(1,:)=fx(x,y);k(2,:)=fx(x+0.5*h,y+0.5*h*k(1,:));k(3,:)=fx(x+0.5*h,y+0.5*h*k(2,:));k(4,:)=fx(x+h,y+h*k(3,:));y=y+h*(k(1,:)+2*k(2,:)+2*k(3,:)+k(4,:))/6;x=x+h;enddisp(['x=',num2str(p(i)),' ','y=',num2str(y)]);y=zeros(1,3);end实验五C程序#include"stdio.h"#include"math.h"#define N 9//已知量doubleA[N][N]={12.38412,2.115237,-1.061074,1.112336,-0.113584,0.718719,1.742382,3.0678 13,-2.031743,2.115237,19.141823,-3.125432,-1.012345,2.189736,1.563849,-0.784165,1.112348,3.123124,-1.061074,-3.125432,15.567914,3.123848,2.031454,1.836742,-1.056781,0.336993,-1.010103,1.112336,-1.012345,3.123848,27.108437,4.101011,-3.741856,2.101023,-0.71828,-0 .037585,-0.113584,2.189736,2.031454,4.101011,19.897918,0.431637,-3.111223,2.121314,1 .784317,0.718719,1.563849,1.836742,-3.741856,0.431637,9.789356,-0.103458,-1.103456,0.238 417,1.742382,-0.784165,-1.056781,2.101023,-3.111223,-0.103458,14.7138465,3.123789,-2. 213474,3.067813,1.112348,0.336993,-0.71828,2.121314,-1.103456,3.123789,30.719334,4.446782,-2.031743,3.123124,-1.010103,-0.037585,1.784317,0.238417,-2.213474,4.446782,40.00 001};doubleb[9]={2.1874369,33.992318,-25.173417,-0.84671695,1.784317,-86.612343,1.1101230,4. 719345,-5.6784392};//函数声明void change(inta,int b);//主函数void main(){ inti,j,q1,n,m,t;double tmp1,tmp2=0,x[9],tmp3;for(i=0;i<9;i++){tmp1=A[i][i];q1=i;for(j=i;j<9;j++)//{if(fabs(tmp1)<fabs(A[j][i])){ tmp1=A[j][i];q1=j;}}if(q1!=i)change(i,q1);for(n=i+1;n<9;n++){tmp3=A[i][i]/A[n][i];for(m=i+1;m<9;m++)A[n][m]=A[n][m]*tmp3-A[i][m];//需检查,为何主元用公式置零不行。