【正文】
扣5000≤price 14%折扣輸入所售商品的價格,求其實際銷售價格。 程序如下:price=input(39。請輸入商品價格39。)。switch fix(price/100) case {0,1} %價格小于200 rate=0。 case {2,3,4} %價格大于等于200但小于500 rate=3/100。 case num2cell(5:9) %價格大于等于500但小于1000 rate=5/100。 case num2cell(10:24) %價格大于等于1000但小于2500 rate=8/100。 case num2cell(25:49) %價格大于等于2500但小于5000 rate=10/100。 otherwise %價格大于等于5000 rate=14/100。endprice=price*(1rate) %輸出商品實際銷售價格trycatch結(jié)構(gòu)【例】trycatch結(jié)構(gòu)應(yīng)用實例。clear,N=4。A=magic(3)。 %設(shè)置3行3列矩陣A。try A_N=A(N,:), %取A的第N行元素catch A_end=A(end,:), %如果取A(N,:)出錯,則改取A的最后一行。endlasterr %顯示出錯原因 A_end = 4 9 2ans = Index exceeds matrix dimensions. 例:[]a=2。b=2。 x=a::a。y=b::b。for i=1:length(y) for j=1:length(x) if x(j)+y(i)1 z(i,j)=*exp(*y(i)^*x(j)^*x(j))。 elseif x(j)+y(i)=1 z(i,j)=*exp(*y(i)^*x(j)^2+*x(j))。 else z(i,j)=*exp(y(i)^26.*x(j)^2)。 end endendaxis([a,a,b,b,min(min(z)),max(max(z))])。colormap(flipud(winter))。surf(x,y,z)。MATLAB的輸入與輸出語句 輸入語句 o 輸入數(shù)值 x=input(39。please input a number:39。)please input a number:22x = 22o 輸入字符串 x=input(39。please input a string:39。,39。s39。)please input a string:this is a string x = this is a string 輸出語句 o 輸出顯示命令 167。 自由格式 (disp) disp(23+45429*4) 361disp([11 22 33。 44 55 66。 77 88 99]) 11 22 3344 55 66 77 88 99disp(39。this is a string39。) this is a string167。 格式化輸出 (fprintf)。 fprintf(39。The area is %\n39。, area) % 注意輸出格式前須有%符號,%跳行符號須有\(zhòng)符號 The area is % 輸出值為8位數(shù)含5位小數(shù) 在這里你如果學(xué)過c語言就能很好的理解了。o 錯誤消息顯示命令 error(39。this is an error39。) ?? this is an error關(guān)系操作符說明小于 =小于或等于大于 =大于或等于= =等于~ =不等于邏輯操作符說明amp。與|或~非16