1、1 %计算空心球体积r0 = 3;ri = 2;volume = 4*pi/3*(r03-ri3)2%计算过氧化氢分子量H = 15.9994;O = 1.0079;H2O2 = 2*H + 2*O3%计算字符创长度string = input(enter string: ,s);stringlength = length(string)%4 输入实数,fprintf 输出这个变量,格式为两位小数num = input(enter a num: );fprintf(The num is %.2fn,num)6fprintf(不指定宽度: %fn,12345.6789)不指定宽度: 12345.
2、678900fprintf(10 个字符宽度, 4 个小数位: %10.4fn,12345.6789)10 个字符宽度,4 个小数位: 12345.6789fprintf(10 个字符宽度, 2 个小数位: %10.2fn,12345.6789)10 个字符宽度,2 个小数位: 12345.68fprintf(6 个字符宽度, 4 个小数位 : %6.4fn,12345.6789)6 个字符宽度,4 个小数位: 12345.6789fprintf(2 个字符宽度, 4 个小数位 : %2.4fn,12345.6789)2 个字符宽度,4 个小数位: 12345.67897fprintf(不指定
3、宽度:%int16n,12345)不指定宽度:12345nt16fprintf(不指定宽度:%dn,12345)不指定宽度:12345fprintf(5 个字符宽度: %5dn,12345)5 个字符宽度:12345fprintf(8 个字符宽度: %8dn,12345)8 个字符宽度: 12345fprintf(3 个字符宽度: %3dn,12345)3 个字符宽度:123458x = 12.34;y = 4.56;fprintf(x is %.3fn,x)x is 12.340fprintf(x is %2.0fn,x)x is 12fprintf(y is %.1fn,y)y is 4.
4、6fprintf(y is %-6.1f!n,y)y is 4.6 !9%计算矩形面积x = input(enter length:);y = input(enter borad:);area = x*y;fprintf(area is %.2fn,area)10string = input(what is your name?,s);fprintf(WoW,your name is %sn,string)11string = input(enter your string:,s);fprintf(your string was:%sn,string) %输出单引号是应采用 格式12v = i
5、nput(enter the flow in m3/s:);fprintf(a flow rate of %.3f meters per secnis equivalent to %.3f feet per secn,v,v/0.028)13incomeyear = input(enter your income per year: );fprintf(the range of food expenditure per year %f%fnthe range of food expenditure per mounth %f%fn,incomeyear*0.08,incomeyear*0.1,
6、incomeyear*0.08/12,incomeyear*0.1/12)14wight = input(wight of plant: );area = input(area of wing: );fprintf(W/A: %f kg/m2n,wight/area)15x= 10;y =22;plot(x,y,g+)16x = -2:0.1:2;plot(x,exp(x)xlabel(x)ylabel(y)title(y=ex)17x = 1:5:100;y = sqrt(x);figure(1)plot(x,y)figure(2)bar(x,y)18略19x1 = linspace(0,p
7、i,10);figure(1)plot(x1,sin(x1)x2 = linspace(0,pi,100);figure(2)plot(x2,sin(x2)20mat = 1000 2000 3000 5000 10000;288 281 269 256 223;x = mat(:,1);y = mat(:,2);plot(x,y)xlabel(high)ylabel(tempture)title(high-tempture)21mat1 = randi(50,100,3,6)save randfile.dat mat1 -ascii;mat2 = randi(50,100,2,6)save
8、randfile.dat mat2 -ascii -append;load randfile.dat;randfile22mat = rand(2,3)*4-1;save testtan.dat mat -ascii;load testtan.dat;mattan = tan(testtan)23mat = 89 42 49 55 72 63 68 77 82 76 67;90 45 50 56 59 62 68 75 77 75 66;91 44 43 60 60 60 65 69 74 70 70save hightemp.dat mat -ascii;load hightemp.dath
9、ightemp(:,1) = hightemp(:,1)+1900;hightempsave y2ktemp.dat hightemp -ascii24%24 Calculates y as a function of xfunction y = fn(x)y = x3-4*x2+sin(x);25%25 Converts from MWh to GJfunction gj = mwh_to_gj(mwh)gj = 3.6*mwh;26%26 converta from inch/h to meter/sfunction meter_sec = converta(inch_hour)meter
10、_sec = inch_hour*5280*0.3048/3600;27function Tn = fn27(P,i,n)Tn = P*(1+i)*n;28略29function V = fn29(Pt,Ps)V = 1.016*sqrt(Pt-Ps);30function THR = fn30(A)THR = (220-A)*0.6;31function outdate = fn31(n)outdate = sqrt(2*pi*n)*(n/exp(1)n;32%32 脚本n = input(enter the number of units: );Cn = costn(n);fprintf(
11、the cost for %d units will be $%.2fn,n,Cn)%32 mygcost 函数function Cn = costn(n)Cn = 5*n2-44*n+11;33%33 脚本rain = input(enter the rain: );snow = fn33(rain);fprintf(the snow is %fn,snow)%33 调用函数function snow = fn33(rain)snow = rain*6.5;34%34 脚本s = input(enter long_side: );V = fn34(s);fprintf(volume is %
12、.2fn,V)%34 函数function V = fn34(s)V = sqrt(2)*s3/12;35%35 pickone(x) returns a random element from vector xfunction outdate = pickone(x)outdate = randi(x(1) x(end),1,1);36function outdate = vecout(x)outdate = x:1:x+5;37%37b = input(enter the first side: );c = input(enter the second side: );alpha = in
13、put(enter the angle between them: );a = sqrt(b2+c2-2*b*c*cosd(alpha); %切记这里用 sindfprintf(the third side is %.3fn,a)38略39%39mat = 90.5792 27.8498 97.0593;12.6987 54.6882 95.7167;91.3376 95.7507 48.5376;63.2359 96.4889 80.02809.7540 15.7613 14.1886;save floatnums.dat mat -ascii;load floatnums.dat;floa
14、tnums = round(floatnums)save intnums.dat floatnums -ascii40%40costssales = 1100 800;1233 650;1111 1001;1222 1300;999 1221;fprintf(there were %d quarters in the filen,length(costssales)x = costssales(:,1);y = costssales(:,2);plot(x,ko)hold onplot(y,b*)mat = fliplr(costssales);save newfile.dat mat -ascii