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

 时间:2026-04-28 21:02:09

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

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

2、第二,利用switch-case-otherwise-end结构求任意底数的对数。启动MATLAB,新建脚本(Ctrl+N),在脚本编辑区输入如下代码:

close all; clear all; clc

a=input('please input a base number of logarithm ''a:''');

N=input('please input a real number of logarithm (antilogarithm) ''N:''');

switch(a)

    case 1

        errordlg('error,a can''t equal 1')

    case exp(1)

        x=log(N);

    case 10

        x=log10(N);

    otherwise

        x=log(N)/log(a);

end

disp('x=') 

disp(x)

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

3、第三,以x=log10(100)为例,即底数为10,真数为100,x等于以10为底100的对数,保存和运行上述脚本。在命令行窗口得到如下结果:

please input a base number of logarithm 'a:'10

please input a real number of logarithm (antilogarithm) 'N:'100

x=

     2

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

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

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

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:'100

x=

    4.6052

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

  • matlab符号表达式的极限如何求值
  • isprime函数的使用方法?
  • 怎么用Matlab求方程?
  • MATLAB如何进行三维曲线和三维曲面的绘制?
  • 如何在Mathematica中引用无穷大量?
  • 热门搜索
    大手牵小手手抄报 元宵节手抄报图片大全 感恩教师手抄报 三年级手抄报大全 国庆节手抄报简单又漂亮 庆国庆手抄报简单漂亮 抗击冠状病毒手抄报 中秋节英语手抄报 关于大自然的手抄报 亲子阅读手抄报