实验30 Mathcad编程举例(2) 1鸡兔tong笼问题的 Mathcad程序:头36,脚100,求鸡、兔各若干 henrabit; for kE 1.. 36 break if k*(100-2.k) =36∧mod(100-2k,4)=0 Chickens""Rabbits" Chickens""Rabbits henrabit henrabitI:=chikens 1 while chikens≤36 100-2.chikens break if mod(100-2. chikens, 4 )=0A chikens chikens← chikens+1 Chickens" 100-2. chikens chikens henrabitl Given x+y=36 2x+4y=100 14 2利用求积分的梯形公式编写程序计算函数在一个有限区间上的定积分: (b-a) fori∈1.n sl←[(i-1)h+fil f(x):=xsin(x) Integ0,,50,f=1.0008250762
Integ 0 p 2 , , 50,f æ ç è ö ÷ ø f(x) := x×sin(x) = 1.000082250762 Integ(a,b, n,f) h (b - a) n ¬ s ¬ 0 s1 [f[(i - 1)×h] + f(i×h)] h 2 ¬ × s ¬ s + s1 for i Î 1.. n := 2 利用求积分的梯形公式编写程序计算函数在一个有限区间上的定积分: Find(x, y) 22 14 æ ç è ö ÷ ø ® 2x + 4y = 100 x + y = 36 Given henrabit1 "Chickens" 22 "Rabbits" 14 æ ç è ö ÷ ø = henrabit1 chikens ¬ 1 break mod(100 - 2×chikens, 4) = 0 chikens 100 - 2×chikens 4 æ ç è ö ÷ ø if Ù + = 36 chikens ¬ chikens + 1 while chikens £ 36 "Chickens" chikens "Rabbits" 100 - 2×chikens 4 æ ç ç è ö ÷ ÷ ø := henrabit "Chickens" 22 "Rabbits" 14 æ ç è ö ÷ ø = henrabit break k (100 - 2×k) 4 if + = 36 Ù mod(100 - 2×k, 4) = 0 for kÎ 1.. 36 "Chickens" k "Rabbits" 36 - k æ ç è ö ÷ ø := 1 鸡兔tong笼问题的Mathcad程序: 头36, 脚100, 求鸡、兔各若干. 实验30 Mathcad 编程举例(2)
hg(n=|h←①-a s←(fa)+fb) fori∈l l←f(a+ih;h Integ10,=,50,f|=1000250762 f(x)dx→1=1 3编写程序统计一个班级考试成绩的分组统计结果,输出次数分布,总成绩, 平均成绩以及标准差 stat(Data): =n<length( Data)-1 x.←0 fori∈0.n xo←xo+ 1 if data;<60 x←x+1if70≤Data;<80 x←x+1i80sDn<90 44+ I otherwise 0.4 Pj x100 cum·100 n+/,cum, n+1 72 534.615 4.615 414215381726154各列顺次为:成绩等级 stat( score)=|323353854061538数,百分比,累积频数 22030.769 92.308 累积百分比 15769265100 mean(score)=75969 平均成绩 sum:= mean( score ) length(score)
Integ1(a,b, n,f) h (b - a) n ¬ s (f(a) + f(b)) h 2 ¬ × s1 ¬ f(a + i×h)×h s ¬ s + s1 for i Î 1.. n - 1 := Integ1 0 p 2 , , 50,f æ ç è ö ÷ ø = 1.000082250762 0 p 2 f(x) x ó ô ô õ d ® 1 = 1 3 编写程序统计一个班级考试成绩的分组统计结果, 输出次数分布, 总成绩, 平均成绩以及标准差. stat(Data) n ¬ length(Data) - 1 x i ¬ 0 for i Î 0.. 4 x 0 x 0 ¬ + 1 Datai if < 60 x 1 x 1 ¬ + 1 60 Datai if £ < 70 x 2 x 2 ¬ + 1 70 Datai if £ < 80 x 3 x 3 ¬ + 1 80 Datai if £ < 90 x 4 x 4 ¬ + 1 otherwise for i Î 0.. n p j ¬ 5 - j for j Î 0.. 4 cum0 x 0 ¬ cumj cumj-1 x j ¬ + for j Î 1.. 4 augment p,x x×100 n + 1 , , cum cum×100 n + 1 , æ ç è ö ÷ ø := score 71 73 78 := 各列顺次为: 成绩等级 , 人数, 百分比 , 累积频数 累积百分比 stat(score) 5 4 3 2 1 3 14 23 20 5 4.615 21.538 35.385 30.769 7.692 3 17 40 60 65 4.615 26.154 61.538 92.308 100 æ ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ø = mean(score) = 75.969 平均成绩 sum := mean(score)×length(score)
总成绩 stdev(score)=9.702 标准差 17 6589 成绩频数分布图 成绩累积分布图 hist(int, score)=23 5 4编写程序将一个十进制的整数转换成二进制数 Decimal_bin(x): =floor 0.n
sum = 4938 总成绩 stdev(score) = 9.702 标准差 int 55 65 75 85 95 æ ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ø := h 3 14 23 20 5 æ ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ø := cum 3 17 40 60 65 æ ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ø := 50 100 20 成绩频数分布图 50 100 50 成绩累积分布图 int 50 60 70 80 90 100 æ ç ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ÷ ø := hist(int,score) 3 14 23 20 5 æ ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ø = 40 60 80 100 10 20 30 4 编写程序将一个十进制的整数转换成二进制数. Decimal_bin(x) n floor ln(x) ln(2) æ ç è ö ÷ ø ¬ reduck ¬ x reduck+1 floor reduck 2 æ ç è ö ÷ ø ¬ x reduck+1 ¬ for kÎ 0.. n digitj reducj 2 reducj+1 ¬ - × for j Î 0.. n binary 0 n i digiti 10i å × = ¬ :=
Decimal bin(451)=111000011 Decimal bin(127)=1111111 5如下的两个程序用于判断一个整数是不是素数 Prime(n): for ke 2.flo Pk←modn,k) or(同 pk← I if PK+2≠0 The number is prime!"if I"The number is not prime!"otherwise x:=299 Prime(x)="The number is not prime!" Prime(9013)="The number is prime! FPrime(n): for ke 1..2 Pk fork∈2.floo if n> 2 Pk m←nor(√同 s←0 ←1 s←s+1 No Prime otherwise x: 299 FPrime(x)="No Prime" FPrime(9013)="Prime 6六种银行存款方案比较,r为利率向量,其各分量分别是1,2,3,5年定期存款, 款一律不计复利程序中x为本金.输出结果为按照各种方案将本金x存 银行,五年后,本利以及利息额 0245
Decimal_bin(451) = 111000011 Decimal_bin(127) = 1111111 5 如下的两个程序用于判断一个整数是不是素数. Prime(n) p k ¬ mod(n, k) for kÎ 2.. floor( n) m ¬ floor( n) - 2 prk ¬ 1 p k+2 if ¹ 0 prk p k+2 ¬ otherwise for kÎ 0.. m "The number is prime!" 0 m k pr Õ k = if ¹ 0 "The number is not prime!" otherwise := x := 299 Prime(x) = "The number is not prime!" Prime(9013) = "The number is prime!" FPrime(n) p k ¬ k for kÎ 1.. 2 p k ¬ mod(n, k) for kÎ 2.. floor( n) if n > 2 m ¬ floor( n) s ¬ 0 prk ¬ 1 s ¬ s + 1 p k if ¹ 0 for kÎ 0.. m "Prime" if m = s "No Prime" otherwise := x := 299 FPrime(x) = "No Prime" FPrime(9013) = "Prime" 6 六种银行存款方案比较, r 为利率向量, 其各分量分别是1, 2, 3, 5年定期存款, 存款一律不计复利. 程序中 x 为本金. 输出结果为按照各种方案将本金x存入 银行, 五年后, 本利以及利息额. r0 .024 .0245 .025 .0258 æ ç ç ç ç è ö ÷ ÷ ÷ ÷ ø :=
it(x, r): = for ke0.5 corpus,←X 中+3(+2) ← accrual←p- corpus s← augment( scheme,p) acc 225802580 222553.52553.5 322544.382544.38 deposit(20000, r0) 42251825 522536.212536.21
deposit(x,r) schemek ¬ k + 1 corpusk ¬ x for kÎ 0.. 5 p x 1 5 r 3 + × ( ) × x 1 3 r 2 + × ( ) × 1 2 r 1 + × ( ) × x 1 3 r 2 + × ( ) × 1 r 0 + ( ) 2 × x 1 r 0 + ( ) 5 × x 1 2 r 1 + × ( ) 2 × 1 r 0 + ( ) × x 1 2 r 1 + × ( ) × 1 r 0 + ( ) 3 × é ê ê ê ê ê ê ê ê ê ê ê ë ù ú ú ú ú ú ú ú ú ú ú ú û ¬ accrual ¬ p - corpus s ¬ augment(scheme ,p) augment(s, accrual) := deposit(20000 ,r0) 1 2 3 4 5 6 22580 22553.5 22544.38 22518 22536.21 22527.1 2580 2553.5 2544.38 2518 2536.21 2527.1 æ ç ç ç ç ç ç è ö ÷ ÷ ÷ ÷ ÷ ÷ ø =