当前位置:首页>开发>正文

matlab中switch函数用法 在matlab中怎么用switch编写函数

2023-04-11 10:06:28 互联网 未知 开发

matlab中switch函数用法 在matlab中怎么用switch编写函数

在matlab中怎么用switch编写函数

switch switch_expression % a scalar or string
case case_expression1 % a scalar, a string, or a cell array of scalars or strings.
statements
case case_expression statements
...
otherwise
statements
end

Matlab switch 的应用举例。

我自己编的对任意底求对数的函数如下:
function y=logn(n,x)
switch(n)
case errordlg(不好意思,出错了!!!)
case y=log2(x)
case exp(1)
y=log(x)
case y=log10(x)
otherwise
y=log10(x)/log10(n)
end
其中n为底数,x为真数,当我们把此函数文件保存在work里,在命令窗口中输入:
>>logn(3,27)
得到结果如下:
ans=
3.0000
你仔细体会体会switch 是怎么使用的吧

matlab中如何用switch语句来解答

switch ch
case x = 20 * rand - 10
case x = 10 * rand - 5
case x = 2 * rand - 1
case x = randn
end

matlab用if或switch实现以下函数

嗯就是你f(x,y)右边 的这个等号不正确啊,这样的定义是无效的。

if y == 1,
z = sin(x)
elseif y == 2,
z = cos(x)
else
z = cos(x).*sin(x)
end

matlab分段函数用switch编程(不要if语句的)

不用if,也用不到switch请参阅下面代码:
clear
a=input(input a)
b=input(input b)
c=input(input c)
x=0.5:0.01:5.5
i=1:length(x)
t=fix(x(i)-0.5)
t=t(:)
y=(a*x.*x b*x c).*(t==0) ...
(a*sin(b)^c x).*(t==1) ...
(a*sin(b)^c x).*(t==2) ...
(log(abs(b c./x))).*(t==3) ...
(log(abs(b c./x))).*(t==4)

最新文章