2.11.5 对数尺度
打印数据既可以用对数尺度,也可以用线性尺度。在x,y轴上使用这两种尺度的一种或两种可以组合形成4种不同的坐标系。每一种组合者有一个特定的函数。
1.plot函数的x,y均用线性尺度
2.semilog函数x轴用对数尺度,y轴将用线性尺度
3.semiloge函数x轴用线性尺度,y轴用对数尺度
4.loglog函数两坐标轴将会都用对数尺度。
这四个函数在意义上是等价的,只是坐标轴的类型不同。每一个图象的例子如图2.8所示。
例2.3
(温度转换)设计一个MATLAB程序,读取一个华氏温度的输入,输出开尔文温度。
答案
华氏温度和开尔文温度的转换关系式可在物理学课本中找到。其关系式为:
\(\[T=(5/9T-32.0)+273.15\]\) (2.5)
在物理学参考书中举了一些例子,我们可以用来检验我们程序是否正确。例如
我们设计程序的步骤如下
1.提示用户键入华氏温度值
2.读取输入值
3.通过关系式转换为开氏温度
4.输出结果,结束
我们将会用input函数输入华氏温度,用fprintf函数输出结果。
% Scriptfile:temp_conversion.m % % Purpose: % Toconvert an input temperature from degrees Fahrenheit to % anoutput temperature in kelvins. % % Recordof revisions: % Date Programmer Description of change % ==== ========= ================ % 12/01/97 S.J.Chapman Original code % %Define variables: % temp_f ­­Temperature in degrees Fahrenheit % temp_k ­­Temperature in kelvins %Prompt the user for the inputtemperature. temp_f=input('Enter the temperature indegrees Fahrenheit:'); %Converttokelvins. temp_k=(5/9)*(temp_f-32)+273.15; %Writeouttheresult. fprintf('%6.2f degrees Fahrenheit = %6.2f kelvins.\n',... temp_f,temp_k);
我们输入上面的例子中的华氏温度值,以检测程序的正确性。注意用户的输入值已用黑体字标出。
>> temp_conversion Enter the temperature in degreesFahrenheit:212 212.00 degrees Fahrenheit = 373.15kelvins. >> temp_conversion Enter the temperature in degreesFahrenheit:-110 -110.00 degrees Fahrenheit = 194.26kelvins.
这个结果和物理教科书的结果相同。在本程序中,我们重复出带单位的输入值和输出值.只有带上单们神经质输出才有意义.
按照惯例,任何输入变量和输出变量的单位都应打印出来.
好的编程习惯
当你读取和写入数据时,使用适当的单位
|