多语言展示
当前在线:198今日阅读:100今日分享:18

switch-case-end结构求任意底数的对数

如果a^x=N,那么x=logaN,x叫做以a为底N的对数,a叫做对数的底数,N叫做真数。下面采用MATLAB提供的switch-case-otherwise-end结构求任意底数的对数。
工具/原料
1

MATLAB

2

switch-case-otherwise-end

3

对数

方法/步骤
1

第一,以10为底的对数叫做常用对数(common logarithm),记作log,以无理数e(e=2.71828...)为底的对数叫做自然对数(natural logarithm),记作ln,对于任意底数的对数,可以采用换底公式进行计算,换底公式见下图。

3

第三,以x=log10(100)为例,即底数为10,真数为100,x等于以10为底100的对数,保存和运行上述脚本。在命令行窗口得到如下结果:please input a base number of logarithm 'a:'10please input a real number of logarithm (antilogarithm) 'N:'100x=     2

4

第四,如果底数a=1,则会弹出错误对话框。代码中的errordlg()确保a=1时弹出错误对话框“error,a can't equal 1”。

5

第五,如果底数为e,求以e为底100的对数。运行脚本时输入a=exp(1),在命令行窗口得到如下结果:please input a base number of logarithm 'a:'exp(1)please input a real number of logarithm (antilogarithm) 'N:'100x=    4.6052

注意事项
1

对于对数logaN,底数a要大于0且不等于1。

2

0没有对数。

3

MATLAB遇到字符串有单引号要在代码中多加一个单引号,如errordlg('error,a can''t equal 1')。

推荐信息