写出程序运行结果和编程题

写出程序运行结果和编程题
写出程序运行结果和编程题

写出程序运行结果

1、下面程序的运行结果是:________

void swap1(int c[])

{ int t;

t=c[0];c[0]=c[1];c[1]=t;

}

void swap2(int c0,int c1)

{ int t;

t=c0;c0=c1;c1=t;

}

main( )

{ int a[2]={2,4},b[2]={3,5};

swap1(a); swap2(b[0],b[1]);

printf(“%d %d %d %d\n”,a[0],a[1],b[0],b[1]); }

2、下面程序的运行结果是:_____________

main()

{int a=2,i

for(i=0;i<3;i++)

printf("%4d",ff(a));

}

ff(int a)

{int b=0;static int c=3;

b++;

c++;

return(a+b+c);

}

3、下面程序的运行结果是:________

main()

{int i,j,k=5;

for(i=0;i<2;i++)

{k++;

{int k=0;

for(j=0;j<=3;j++)

{if(j%2)continue;

k++;}

}

k++;}

printf("k=%d\n",k);

}

4、下面程序的运行结果:_________。

main()

{int a,b,k=2,m=6,*p1=&k,*p2=&m;

a=p1==&m;

b=(-*p1)/(*p2)+7;

printf("a=%d,",a);

printf("b=%d\n",b);

}

5、以下程序运行结果:________。

funa(int a)

{int b=0;

static int c=0;

a=c++,b++;

return (a);

main()

{int a=3,i,k;

for(i=0;i<2;i++)

k=fun(a++);

printf("%d\n",k);

}

6、下面程序的运行结果:_____________

struct stu

{int num;

char name[10];

int age;

}

void fun(struct stu *p)

{printf("%s\n",(*p).name);

}

main()

{struct stu students[3]={{2010,"zhang",20},{2011,"wang",19},{2012,"zhao",18};

fun(students+2);

}

7、下面程序的运行结果是:_________。

main()

{int i,j,k=5;

for(i=0;i<2;i++)

{k++;

{int k=0;

for(j=0;j<=3;j++)

{if(j%2)continue;

k++;}

}

k++;}

printf("k=%d\n",k);

}

8、下面程序的运行结果是:_________。

main()

{int a[5]={1,3,5,7,9},x,y,*p;

p=&a[0];

x=*(p+2)

y=*(p+4);

printf("%d ,%d , %d\n",*p,x,y);

}

9、下面程序的运行结果是:_________。

#include "stdio.h"

int k=1;

main( )

{int i=6;

fun(i);

printf("\n%d,%d",i,k);

}

fun(int m)

{m+=k;k+=m;

{char k='B';

printf("%d",k-'A');

}

printf("%d,%d",m,k);

}

10、下面程序的运行结果是:_________。

#include

void fun(int *s)

{static int j=1;

do

{s[j]+=s[j+1];

}while(++j<2);

}

main()

{int k,a[10]={1,2,3,4,5};

for(k=1;k<3;k++)

fun(a);

for(k=0;k<5;k++)

printf("%d",a[k]);

}

11、下面程序的运行结果是:_________。

#include "string.h"

fun(char *w,int n)

{char t,*s1,*s2,t;

s1=w; s2=w+n-1;

while(s1

{t=*s1;

*s1++=*s2;

*s2--=t;

}

}

main()

{static char *p="123456789";

fun(p,strlen(p));

printf("%s\n",p);

}

12、下面程序的运行结果是:_________。main()

{struct cmplx

{int x;

int y;

}cnum[2]={1,3,2,5};

cnum[0].x=cnum[0].x*cnum[1].x;

cnum[1].y=cnum[0].y+cnum[1].y;

printf("%d\n",cnum[1].y/cnum[0].x*cnum[1].x);

}

14、以下程序的正确运行结果是:______。

#inclued

main()

{int k=5,m=1,p;

p=func(k,m);printf("%d,",p);

p=func(k,m);printf("%d\n",p);

}

func(int a,int b)

{static int m=0,i=2;

i+=m+1;

m=i+a+b;

return m;

}

15、以下程序的正确运行结果是:______。

int w=3;

main()

{int w=10;

printf("%d\n",fun(4)*w);

}

fun(int k)

{if(k= =0)return (w);

return(fun(k-1)*k);

}

16、下面程序的运行结果是:_________。

main()

{char *s="1312442311";

int v1=0,v2=0,v3=0,v4=0,k;

for(k=0;s[k];k++)

switch(s[k])

{case '1': v1++;

case '3': v3++;

case '2':v2++;

case '4':v4++;

}

printf("v1=%d,v2=%d,v3=%d,v4=%d\n",v1,v2,v3,v4); }

编程题

1、产生50个[10,99]间的随机整数存入一个一维数组中,计算这50个数的平均

值(保留两位小数)。

2、 输出100以内的个位数为5、且能被7整除的所有数,并统计个数。

3、用随机函数产生100个[0,99]的整数,存放到一个10×10的二维数组中,求每列数组元素的和。输出该二维数组和每列数据的和。

4、输入x 和值,按下面公式:计算前n 项和,直到x n /n! < 10-5。

5、从键盘输入20个单精度数,对输入的数据按从大到小的顺序进行排序,然后输出排序后的结果。

6、编写程序,计算下面的多项式的前30和。

)

1(*1......4*313*212*111++++++n n

7、按下面公式计算计算π的近似值,直到1/n<1e-6。

!......!3!2132n x x x x n +++++ +-+-=7151314

写出下列程序运行结果

写出下列程序运行结果: 理解PRINT A=5:B=3 PRINT A,B,A+B END X=9:Y=7 PRINT "X=";X,"Y=";Y PRINT "X+Y=";X;"+";Y;"=";X+Y END A=4:B=7 PRINT "C=";A+B*2-A/2 END 理解变量(包括字符串变量) X=6:Y=0 PRINT X-2, X=X+7 PRINT “Y=”;X+2 PRINT Y END M=12:N=6 X=M/N:Y=M+N PRINT X,Y X=X+N:Y=M+X PRINT X,Y END A=7:B=4 A=A+B:B=A-B:A=A-B PRINT “A=”;A,”B=”;B END A$=”123”:B$=”321” C$=A$+B$ PRINT A$;”+”;B$;”=”;C$ END 理解INPUT INPUT X,Y C=X*Y PRINT “C=”;C END ? 4, 8 INPUT “A,B=”;A,B D=A+A+B+B PRINT D END A,B=? 2,3 绘图语句 SCREEN 12 LINE (100,100)-(400,300),2,B LINE (100,100)-(400,300),2 END SCREEN 12 LINE (100,100)-(400,100) LINE –(100,300) LINE –(100,100) END SCREEN 12 CIRCLE (200,200),100 LINE (200,100)-(200,300) LINE (200,200),(300,200)

END 简单函数 A=12.34: B=-5.34: C=0 D=A*A: E=B*B PRINT ABS(A),ABS(B),ABS(C) PRINT FIX(A),FIX(B),FIX(C) PRINT SQR(D),SQR(E) END A=3.14: B=A^2: C=-A D=FIX(A):E=ABS(C):F=SQR(B) PRINT ABS(D),E,F END 子字符串函数 A$=”123456789” L$=LEFT$(A$,4)+MID$(A$,3,3)+RIGHT$(A$,2) PRINT L$ END A$=”I am a student.” B$=”I like sing.” C$=”I often use computer.” D$=LEFT$(A$,2)+MID$(B$,3,5)+RIGHT$(C$,9) PRINT “D$=”;D$ END 读数置数语句 READ A,B,C PRINT A,B+C DATA 3,2,5 END READ X,Y PRINT “X=”;X,”Y=”;Y READ X PRINT X,Y DATA 3,2,5 END M=0 : READ A : M=M+A READ B : M=M+B PRINT M DATA 21,34,26 END 理解循环语句 FOR I=1 TO 10 STEP 3 PRINT “I=”;I,”I^2=”;I*I NEXT I END M=0 FOR A=2 TO 10 STEP 3 M=M+A NEXT A PRINT “M=”;M, “P=”;M/3 END 双重循环 FOR A=1 TO 3 FOR B=4 TO 5 PRINT A,B NEXT B NEXT A END FOR X=6 TO 10 STEP 2 FOR Y=5 TO 3 STEP –2 PRINT X-Y NEXT Y NEXT X

程序运行结果练习题

一、程序运行结果 1、假设数据表文件成绩表中有“总分”字 段,运行下列程序段 Use 成绩 X1=0 SCAN X1=MAX(总分,X1) Endscan ?X1 RETURN 执行该程序后X1显示的值为成绩表中的__________________________ 2、clear do A return procedure A s=5 do B ?s Return Procedure B S=S+10 Return _______________________________ 3、set talk off s=0 i=5 x=11 do while s<=x s=s+1 i=i+1 enddo ?s Set talk on _____________________ 4有如下程序,函数iif( )返回值是_____ *程序 Private X,Y Stroe “男”to X Y=len(X)+2 ?iif(Y<4,”男”,”女”) Return 4、s=0 for n=100 to1 step -1 s=s+n endfor ?s Return ________________ 5、clear set talk off store 1 to i,a,b do while i<=3 do prog1 “p(“+str(I,1)+”)=”+str(A,2)+”,” i=i+1 enddo ??”b=”+str(b,2) Return Procdure prog1 a=a*2 b=b+a set talk on return ___________________ 6、dime a(6) a(1)=1 a(2)=2 for i=3 to 6 a(i)=a(i-1)+a(i-2) next ?a(6) ____________________ 7、accept to a if A=[123] s=o endif s=1 ?s _______________ 8、clear store 0 to a,b,s,i,j do while i<=5 i=i+1 if i=3 a=s j=i loop else s=s+i endif if i=4 b=s+a j=j+i exit else s=s+i endif enddo ?S ___________ 9、main.prg set talk off a=5 b=”c” c=10 do p1 with b ?a,b,c set talk off return p1.prg parameters a b=”a” &b=&a Do p2 ?a,b,c Return p2.prg if c=&b return to master else return endif 运行main.prg的结果为____________ 11、set talk off Dimension k(2,3) i=1 do while i<=2 j=1 do while j<=3 k(i,j)=i*j if int(k(i,j)/2)=k(i,j)/2 k(i,j)=k(i,j)+1 else k(i,j)=k(i,j)-1 endif ??k(i,j) ?? j=j+1 enddo ?i=i+1 Enddo

MATLAB程序运行结果

close all %关闭打开了的所有图形窗口clc %清屏命令 clear %清除工作空间中所有变量%定义时间范围 t=[0:pi/10:8*pi]; y=sin(t); plot(t,y,'b:square')

close all clc clear %定义时间范围 t=[0:pi/20:9*pi]; grid on hold on %允许在同一坐标系下绘制不同的图形 plot(t,sin(t),'r:*') plot(t,cos(t)) plot(t,-cos(t),'k') %grid on %在所画出的图形坐标中添加栅格,注意用在plot之后4-1: close all clc clear %定义时间范围

t=[0:pi/20:9*pi]; hold on %允许在同一坐标系下绘制不同的图形 plot(t,sin(t),'r:*') plot(t,cos(t)) plot(t,-cos(t),'k') grid on %在所画出的图形坐标中添加栅格,注意用在plot之后hold off %覆盖旧图,自动把栅格去掉,且若要在加入栅格就 %必须把grid on加在plot后面 plot(t,-sin(t)) grid on

%主程序exp2_10.m global a %声明变量a为全局变量 x=1:100; a=3; c=prods(x) %调用子程序prods.m %子程序prods.m % function result=prods(x) % global a % result=a*sum(x); %声明了与主程序一样的全局变量a,以便在子程序中可以%使用主程序中定义的变量 答案:15150 exmdl2_1.m clear close all clc

C#程序题运行结果题

三写出运行结果题 (1) using System; class Program { static void Main() { int x = 3; Console.Write("星期:"); Console.WriteLine((Weekday)x); Console.Write("月份:"); Console.WriteLine((Month)x); Console.Write("季节:"); Console.WriteLine((Season)x); } enum Weekday { 未知 = -1, 星期天, 星期一, 星期二, 星期三, 星期四,星期五,星期六 } enum Month { 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月, 未知 = -1 } enum Season { 春, 夏 = 2, 秋 = 4, 冬 = 8 } } (2) using System; class Program { static void Main() { class1 c1 = new class1(); class1.y = 5; c1.Output(); class1 c2 = new class1(); c2.Output(); } } 星期:星期三月份:四月季节:3

public class class1 { private static int x = 0; public static int y = x; public int z = y; public void Output() { Console .WriteLine(class1.x); Console .WriteLine(class1.y); Console .WriteLine(z); } } (3) using System; class Program { static void Main() { IA ia = new B (); Console .WriteLine("{0} {1} {2} {4}", ia is IA , ia is IB , ia is A , ia is A , ia is B , ia is C ); } } public interface IA { } public interface IB : IA { } public class A : IA { } public class B : IB { } public class C : B , IB { } (4) using System; class Program { static void Main(string [] args) { MyClass y = new MyClass (); BaseClass x = y; x.i = 100; Console .WriteLine("{0}, {1}", x.i, y.i); } } class BaseClass { public int i; 0 5 0 0 5 5 True True False True

程序注释及运行结果

第二部分程序注释及运行结果 读者须知: 为了便于读者理解,现将光盘上第一部分可直接在MATLAB6.I 下运行的MATLAB程序的编号和书本上的内容对应如下,每个程序题目括号内的file.m是对应书本上的内容在光盘上第一部分的程序编号。 第二章的随机序列产生程序 例2.1 用乘同余法产生随机数(见光盘FLch2sjxleg1.m) ①编程如下: A=6; N=100; %初始化; x0=1; M=255; for k=1:N %乘同余法递推100次; x2=A*x0; %x2和x0分别表示x i和x i-1; x1=mod (x2,M); %将x2存储器的数除以M,取余数放x1(x i)中; v1=x1/256; %将x1存储器的数除以256得到小于1的随机数放v1中; )存放在矩阵存储器v的第k列中,v(:,k) v(:,k)=v1; % 将v1中的数( i %表示行不变、列随递推循环次数变化; x0=x1; %x i-1= x i; v0=v1; end %递推100次结束; v2=v %该语句末无‘;’,实现矩阵存储器v中随机数放在v2中,% 且可直接显示在MA TLAB的window中; k1=k; %grapher %以下是绘图程序; k=1:k1; plot(k,v,k,v,'r'); xlabel('k'), ylabel('v');tktle('(0-1)均匀分布的随机序列') ②程序运行结果如图2.5所示。

图2.5 采用MA TLAB产生的(0,1)均匀分布的随机序列图 ③产生的(0-1)均匀分布的随机序列 在程序运行结束后,产生的(0,1)均匀分布的随机序列,直接从MATLAB的window 界面中copy出来如下(v2中每行存6个随机数): v2 = 0.0234 0.1406 0.8438 0.0820 0.4922 0.9609 0.7852 0.7266 0.3750 0.2578 0.5508 0.3164 0.9023 0.4336 0.6094 0.6680 0.0234 0.1406 0.8438 0.0820 0.4922 0.9609 0.7852 0.7266 0.3750 0.2578 0.5508 0.3164 0.9023 0.4336 0.6094 0.6680 0.0234 0.1406 0.8438 0.0820 0.4922 0.9609 0.7852 0.7266 0.3750 0.2578 0.5508 0.3164 0.9023 0.4336 0.6094 0.6680 0.0234 0.1406 0.8438 0.0820 0.4922 0.9609 0.7852 0.7266 0.3750 0.2578 0.5508 0.3164 0.9023 0.4336 0.6094 0.6680 0.0234 0.1406 0.8438 0.0820 0.4922 0.9609 0.7852 0.7266 0.3750 0.2578 0.5508 0.3164 0.9023 0.4336 0.6094 0.6680 0.0234 0.1406 0.8438 0.0820 0.4922 0.9609 0.7852 0.7266 0.3750 0.2578 0.5508 0.3164 0.9023 0.4336 0.6094 0.6680 0.0234 0.1406 0.8438 0.0820

C++程序与运行结果

Class #include class X { int V alue; public: X(int v=0) { V alue = v; cout<<"X::X("<

5、以下程序运行结果:________。 funa(int a) {int b=0; static int c=0; a=c++,b++; return (a); main() {int a=3,i,k; for(i=0;i<2;i++) k=fun(a++); printf("%d\n",k); } 6、下面程序的运行结果:_____________ struct stu {int num; char name[10]; int age; } void fun(struct stu *p) {printf("%s\n",(*p).name); } main() {struct stu students[3]={{2010,"zhang",20},{2011,"wang",19},{2012,"zhao",18}; fun(students+2); } 7、下面程序的运行结果是:_________。 main() {int i,j,k=5; for(i=0;i<2;i++) {k++; {int k=0; for(j=0;j<=3;j++) {if(j%2)continue; k++;} } k++;} printf("k=%d\n",k); } 8、下面程序的运行结果是:_________。 main() {int a[5]={1,3,5,7,9},x,y,*p; p=&a[0]; x=*(p+2) y=*(p+4); printf("%d ,%d , %d\n",*p,x,y); } 9、下面程序的运行结果是:_________。 #include "stdio.h" int k=1; main( )

C语言程序及其运行结果一览表

关于数组 1.利用一维数组输出0~20的偶数 程序:#include void main(void) { int x[20],i; for(i=0;i<20;i++) x[i]=i; for(i=0;i<20;i+=2) printf("%3d\n",x[i]); } 结果: 2.求Fibonacci数列的前二十项 程序:#include void main(void) { int i; int f[20]={1,1}; for(i=2;i<20;i++) { f[i]=f[i-2]+f[i-1]; } for(i=0;i<20;i++) { if (i%5==0) printf("\n"); printf("%5d",f[i]); } printf("\n"); } 结果:

3.求10个整形数中最大数,最小数及其所在的位置。(有些地方不太能搞懂。) 程序:#include void main(void) { int a[10],i,imax,imin,d,x; for(i=0;i<10;i++) scanf("%d",&a[i]); d=x=0; for(i=1;i<10;i++) { if (a[i]>imax) { imax=a[i]; d=i; } if(a[i]

程序: #include void main(void) { int x[10],i,sum=0; for(i=0;i<10;i++) scanf("%d",&x[i]); for(i=0;i<10;i++) sum=sum+x[i]; printf("sum=%d\n",sum); for(i=9;i>=0;i--) printf("%3d",x[i]); printf("\n"); } 结果: 5.将10个整形数按从小到大的顺序排列并输出。(冒泡法的基本思想:通过相邻两个数的比较和交换,使数值较小的数逐渐从底部移向顶部,数值较大的数从顶部移向底部。像水底的气泡一样逐渐向上冒,故而得名。) 程序: #include void main(void) { int a[10],i,j,temp; printf("input ten numbers:\n"); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<9;i++) { for(j=0;j<9-i;j++) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } printf("the sorted numbers:\n"); for(i=0;i<10;i++) printf("%3d",a[i]);

Java运行程序写结果

Java练习题 四、运行程序写结果 1. 当在命令提示符后输入”java SumTest 4 5”之后,下列程序的输出结果为: 4+5=9 public class SumTest { public static void main(String[] args) { int a, b, sum; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); sum=a+b; System.out.println(a + "+" + b + "=" + sum); } } 2. 当在命令提示符后输入”java PrintStar 8”之后,下列程序的输出结果为:(A12) ******** public class PrintStar { public static void main(String[] args) { int n = Integer.parseInt(args[0]); int i; for( i=1; i<=n; i++) System.out.print("*"); System.out.println(); } } 3. 当在命令提示符后输入”java CalculateTest 5.5”之后,下列程序的输出结果为: 10.0 public class CalculateTest{ public static void main(String args[]){ float x,y; x = Float.parseFloat(args[0]); if(x<1) y = x; else if(1<=x&&x<10)

写出程序的运行结果

#include void swap(int *x,int *y) { int t; t=*x;*x=*y;*y=t; } main() { int a,b; scanf("%d,%d",&a,&b); swap(&a,&b); printf("%d,%d\n",a,b); } 输入3,5,则显示__5,3___ 2.写出程序的运行结果 #include void swap(int *x,int *y) { int *t; t=x;x=y;y=t; } main() { int a,b; scanf("%d,%d",&a,&b); swap(&a,&b); printf("%d,%d\n",a,b); } 输入3,5,则显示___3,5___ 3.写出程序的运行结果 #include void swap(int *x,int *y) { int t; t=*x;*x=*y;*y=t; printf("%d,%d\n",*x,*y); } main() { int a,b; scanf("%d,%d",&a,&b); swap(&a,&b); printf("%d,%d\n",a,b); } 输入3,5,则显示__ 5,3 5,3

#include void swap(int *x,int *y) { int *t; t=x;x=y;y=t; printf("%d,%d\n",*x,*y); } main() { int a,b; scanf("%d,%d",&a,&b); swap(&a,&b); printf("%d,%d\n",a,b); } 输入3,5,则显示___5,3 3,5_

MATLAB_程序运行结果

MATLAB程序及运行结果 >>【%第1题】 [x,y]=meshgrid(-3:0.1:3) %创建网格数据 z=3.*(1-x).^2.*exp(-x.^2-(y+1).^2)... -10.*(x./5-x.^3-y.^5).*exp(-x.^2-y.^2)... -(1/3).*exp(-(x+1).^2-y.^2); %输入函数式 subplot(2,2,1); %设置2行2列的子图的第1图 mesh(x,y,z); %画出网状图 colormap(autumn) %当前图形窗的着色色图为autumn hidden off %显示遮住的部分 axis tight %把数据范围直接设置为坐标范围 title('表现图形的隐含部分') %设置标题 subplot(2,2,2) mesh(x,y,z) shading flat title('带阴影的三维图形') z=peaks(x,y); %计算函数值 subplot(2,2,3); meshc(x,y,z); %绘制带等高线的三维图形 title('带等高线的三维图形') subplot(2,2,4) contour3(x,y,z,20,'k'); %绘制三维图形的等高线 xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis') title('三维图形的等高线') 【运行结果略】 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ >>【%第2题】

%for循环: k=0;n=63; %设置初值 for i=0:1:63 %建立63次循环k=k+2.^i; sum(k) %累加 end %结束循环,并显示累加值k %WHILE循环: y=0;i=-1; while i<63; i=i+1; y=y+2^i; y=y+2.^i; end y % 避免循环: n=63; i=1:1:n; k=sum(2.^i) ans =1 ans =3 ans =7 ans =15 ans =31 ans =63 ans =127 ans =255 ans =511 ans =1023 ans =2047 ans =4095 ans =8191 ans =16383 ans =32767 ans =65535 ans =131071 ans =262143 ans =524287 ans =1048575 ans =2097151 ans =4194303 ans =8388607 ans =16777215 ans =33554431 ans =67108863 ans =134217727 ans =268435455 ans =536870911 ans =1.0737e+009 ans =2.1475e+009 ans =4.2950e+009

循环:程序阅读,写出程序的运行结果

一、程序阅读,写出程序的运行结果 1)program p431; var i,s,t:integer; begin t:=0; s:=0; for i:= -5 to 5 do begin t:=t+1; s:=s+t+i; end; writeln('s,t=',s:10,t:10); end. {结果为:} 2)program p432; var p,m:integer; begin p:=20; m:=2; repeat p:=p-m; m:=m+3; until m>p; writeln('m,p=',m:10,p:10); end. {结果为:} 3)program p433; var a,n:integer; begin n:=6; a:=0; while n>1 do begin a:=1; repeat

write('*'); a:=a+1; until a>=n; writeln; n:=n-1; end; end. {结果为:} 4)program p434; var i,j,k,s:integer; begin s:=0; for i:=3 downto 1 do begin for j:=1 to 3 do begin k:=0; repeat k:=k+1; s:=s+k; until k=j; end; s:=s-k-1; end; write('s=',s); end. {结果为:} 5)program p5305; var s,i:integer; begin s:=0; for i:=1 to 100 do if (i mod 3=0) and (i mod 4=0) then s:=s+i; writeln('s=',s); end. {结果为:}

程序和运行结果

金牌的程序 clear clc X0=[5 16 16 28 32]; X1(1)=X0(1); [m,n]=size(X0); for i=2:n Y(i-1,1)=X0(i); X1(i)=X1(i-1)+X0(i); B(i-1,1:2)=[-0.5.*(X1(i-1)+X1(i)),1]; end R=inv(B'*B)*B'*Y u=R(2); a=R(1); for i=1:n+1 X(i)=(X0(1)-u/a)*exp(-(i-1)*a)+u/a; if i>=2 X01(i)=X(i)-X(i-1); else X01(i)=X(i); end end disp('中国前五届的奥运会获得的金牌数'); X0 disp('预测中国第29届获得的金牌数为'); X01(n+1) disp('预测值'); X01 disp('相对残差'); for i=1:n D(i)=abs(X0(i)-X01(i)); W(i)=D(i)/X0(i); end W for i=1:n y(i)=(min(D)+0.5*max(D))/(D(i)+0.5*max(D)); end r=sum(y)/n

QX0=X0(1:n)-X01(1:n); QX1(1)=QX0(1); for i=2:n QX1(i)=QX0(i-1)+QX0(i); QY(i-1,1)=QX0(i); QB(i-1,1:2)=[-0.5.*(QX1(i-1)+QX1(i)),1]; end QR=(QB'*QB)\QB'*QY; for i=1:6 QX(i)=(QX0(1)-QR(2,1)/QR(1,1))*exp(-(i-1)*QR(1,1))+QR(2,1)/QR(1,1); XX(i)=X(i)+QX(i); if i>=2 XX0(i)=XX(i)-XX(i-1); else XX0(i)=XX(i); end end disp('修改后预测中国第29届获得的金牌数为'); XX0(n+1) disp('修改后的预测值为'); XX0 for i=1:n D1(i)=abs(X0(i)-XX0(i)); W1(i)=D1(i)/X0(i); end disp('利用残差模型修改后的相对残差'); W1 for i=1:n y1(i)=(min(D1)+0.5*max(D1))/(D1(i)+0.5*max(D1)); end r1=sum(y1)/n t=[24 25 26 27 28]; t1=[24 25 26 27 28 29]; [u1,m]=lsqcurvefit(@gm11,[11,-0.2],t,X0); [u2,m]=lsqcurvefit(@gm11,[11,-0.2],t1,X01); x1=linspace(24,30); y1=(5-u1(1)/u1(2)).*exp(-(x1-24)*u1(2))+u1(1)/u1(2); y2=(5-u2(1)/u2(2)).*exp(-(x1-24)*u2(2))+u2(1)/u2(2); plot(t,X0,'r*',x1,y1,'b-',t1,X01,'o',x1,y2,'-g'); legend('','',0);

相关文档
最新文档