MATLAB的Simulink常用模块(三)(relational operator)

您所在的位置:网站首页 simulink连接示波器的方法 MATLAB的Simulink常用模块(三)(relational operator)

MATLAB的Simulink常用模块(三)(relational operator)

2023-05-03 04:19| 来源: 网络整理| 查看: 265

导读在MATLAB的Simulink中有些常用的模块,今天主要介绍延时模块、关系操作模块、逻辑运算模块。1.延时模块1.1 Delay延时模块Delay模块(延时模块)的输出等于它的一个或几个采样周期前的输入。延时模块的参数设置(1)Delay...

在MATLAB的Simulink中有些常用的模块,今天主要介绍延时模块、关系操作模块、逻辑运算模块。

MATLAB的Simulink常用模块(三)

1.延时模块

1.1 Delay延时模块

Delay模块(延时模块)的输出等于它的一个或几个采样周期前的输入。

MATLAB的Simulink常用模块(三)

MATLAB的Simulink常用模块(三)

延时模块的参数设置

(1)Delay length和Initial condition分别表示延时的采样点数和输出的初始值。(2)模块总的延时时间由延时的采样点数和模块采样时间间隔共同决定。例如: Delay length为5,Sample time设置为0.5,则模块的输出将在2.5s时开始更新,在0~2.58s之间,模块的输出值由Initial condition中指定的初始值决定。(3)Source中有两个选项 Dialog表示参数在对话框里输入,Input port表示增加一个输入端口,通过信号线传递参数。(4)External reset选择None以外的选项时为Delay增加一个输入端口,通过此端口的输入信号达到某种条件而将Delay模块的输出值复位,所谓复位即Delay模块的状态值恢复初始状态值。由External reset下拉框选择触发条件,包括None,Rising(上升沿),Falling(下降沿)、Either、Level和Level hold.Either表示上升沿和下降沿均进行复位。Level Hold表示当前采样时刻的值非零时即复位。Level则包含了Level hold的情况,另外还包括在信号从非零跳变到零采样点。(5)Input processing包括Element as channels(基于采样)和Columns as channels(基于帧)以及Inherited3个选项,基于采样和基于帧的采样方式区别在于采样数据的组织方式。(6)勾选Use circular buffer for state选项时,将使用环形缓存存储仿真或代码生成时使用的状态变量,当Delay length值比较大时,勾选此选项有助于提高执行效率。

示例1

使用Sine Wave连续正弦信号进行简单的示例仿真,将要延时模块的Delay length采样点为5,Initial condition初始值为1,采样时间为0.5,按照仿真图进行仿真。

MATLAB的Simulink常用模块(三)

延时模块参数设置

MATLAB的Simulink常用模块(三)

示波器显示结果

MATLAB的Simulink常用模块(三)

1.2 Unit delay

Unit delay模块(单位延时模块),该模块只延时一个采样周期。其参数对话框比较简单,不需要设置Delay length,不需要考虑缓存区使用环形或线性区域,没有复位功能。

MATLAB的Simulink常用模块(三)

示例2

MATLAB的Simulink常用模块(三)

MATLAB的Simulink常用模块(三)

示波器结果

MATLAB的Simulink常用模块(三)

示例3

MATLAB的Simulink常用模块(三)

单位延时模块参数

MATLAB的Simulink常用模块(三)

示波器结果

MATLAB的Simulink常用模块(三)

2.关系操作模块

Relational Operator 关系操作模块包含9种关系操作符,

MATLAB的Simulink常用模块(三)

MATLAB的Simulink常用模块(三)

Enable zero-crossing detection:启用过零检测功能

示例4

设计示例:八进制累加计数器

仿真图

MATLAB的Simulink常用模块(三)

Delay模块参数

MATLAB的Simulink常用模块(三)

Unit delay模块参数

MATLAB的Simulink常用模块(三)

示波器结果

MATLAB的Simulink常用模块(三)

3.逻辑运算模块

Logical Operator(逻辑运算模块)包括7种逻辑操作,与(AND)、或(OR)、非(NAND)、或非(NOR)、异或(XOR)、异或非(NXOR)、非(NOT)。

或非:即作一次或者多次“或”运算后再做一次“非”运算。有1出0,全0出1.异或:只有在两个比较的位不同时其结果是1,否则结果为0即“两个输入相同时为0,不同则为1”!异或非:异或运算的运算法则:相同为零,不同为一。异或非运算法则:将异或的结果取反。

MATLAB的Simulink常用模块(三)

MATLAB的Simulink常用模块(三)

Icon shape用于选择模块图标的形状,有两个选项:(1)选择rectangular时,模块形状不变,只是在框图中央显示当前所选择的逻辑操作。(2)选择distinctive时,模块会以IEEE图像符号标准所规定的形式展现出来。

MATLAB的Simulink常用模块(三)

MATLAB的Simulink常用模块(三)

对元器件的对齐,选择要对齐的元器件,右键->鼠标点击Arrange->对齐的方式。

MATLAB的Simulink常用模块(三)

参考内容

[1] 博客园作者叮叮当当sunny的文章《Simulink仿真入门到精通(二) Simulink模块 》,文章链接如下:https://www.cnblogs.com/dingdangsunny/p/12235689.html#_label0

[2] CSDN博主chhttty的文章《Simulink代码生成:延时模块及其代码》,文章链接为:

https://blog.csdn.net/u013288925/article/details/106154803

本文内容来源于网络,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。

作 者 | 郭志龙

编 辑 | 郭志龙校 对 | 郭志龙

愉快地学Java语言:第三章 选择

导读

本文适合Java入门,不太适合Java中高级软件工程师。

本文以《Java程序设计基础篇》第10版为蓝本,采用不断提出问题,然后解答问题的方式来讲述。

本篇文章只是这个系列中的一篇,如果你喜欢这种讲解方式,或者觉得从中能学到知识,可以关注我,以便查阅本系列其他文章。

让我们开始愉快地学习Java语言吧!

愉快地学Java语言:第三章 选择

生活中充满着选择,编程是将现实生活影射到虚拟世界的一种手段,必然也会有选择。

1boolean类型

boolean数据类型声明了一个具有值为true或者false的变量。

本篇文章讲的是选泽,那么为啥先讲boolean呢?

因为选择要要用到这种类型,可以说没有这种类型就没法选择。

那么,什么情况下会产生boolean类型的数据呢?

除了主动定义一个变量为boolean类型外,还有就是比较的结果也是boolean类型的。

比较操作符有那些呢?

共有6种,=,==,!=。给他们取个名,依次为小于,小于等于,大于,大于等与,相等,不等。

我们也称比较操作符为关系操作符(relational operator)。

注意,相等操作符是==,而不是=,=是赋值操作符。

2 if语句

我们可以使用if语句作选择!

我有一个很大梦想,就是以后难以作选择的事都交给电脑,让他使用if语句帮我选择,是不是很激动呢?(貌似只是个冷笑话,哈哈)

if语句可以分为几类:单分支 if 语句 、双分支if else语句 、嵌套if语句 、 多分支if else语句。

它们都是怎么定义的呢?

1)单分支 if 语句:

if (布尔表达式) { //一条或多条语句}

如果是一条语句,那么可以省去大括号(但注意书写格式,缩进)。

if (布尔表达式) //一条语句

2)双分支if else语句:

if (布尔表达式) { //一条或多条语句}else { //一条或多条语句}

到这里大家肯定有一个疑问,布尔表达式取值为true或fasle,我们称取true为真,取false为假,那么布尔表达式取值为true时进入if语句块还是为false时进入呢?

当然是为true时进入if语句块了。

3)嵌套if语句

将if语句放到其他if语句块内就构成嵌套if语句

例如,

if (布尔表达式) { if (布尔表达式) { //一条或多条语句 } //其他语句}else { //一条或多条语句}

或者,这样

if (布尔表达式) { //一条或多条语句}else { if (布尔表达式) { //一条或多条语句 } //其他语句}

嵌套的if语句有什么用呢?

大家不觉得它给我们二次选择的机会吗?如果生活中也总能有二次选择该多好!

3改正不良习惯和避开使用if的陷阱冗余测试

boolean b=...;if(b==true){ //其他语句}

这种测试是不必要的,我们应该采用的写法如下:

boolean b=...;if(b){ //其他语句}

但是,有些同学就想让b为false时进入if语句块,那么这样写

boolean b=...;if(b==false){ //其他语句}

其实b为false时进入else语句块里也能实现预期的功能呀!

重复代码

if (布尔表达式) { Syetem.out.println(“hello”)}else { Syetem.out.println(“hello”)}

既然每个选择分支都输出一样的东西,那么干脆不放在if语句块里,因为不论你怎么选都要输出同样的东西!

简化代码

看下面的代码

boolean b=...;boolean r = ...;if(b){ r = true;}else{ r = false;}

这么简单的功能也写这么多行代码,看着都闹心,对吧?

boolean b=...;

boolean r = b?true:false;

看,两条语句就搞定了。

不过,b?true:false还带有问号,这是哪种用法呢?

看下一节:条件表达式。

4条件表达式

boolean-expression ? expression1 : expression2?;

boolean-expression:布尔表达式

expression1:表达式1

expression2:表达式2

上述表达式翻译为:如果boolean-expression值为true,那么整个表达式的值为expression1的值,否则为expression2的值。

?和:构成了条件操作符,也是Java语言中唯一的一个三元操作符。

例如,存在一个变量x,计算

Int i = x>0?1:0;

也就是如果x大于0,i的值为1;x小于0,那么i的值为0;

5 switch

先来看看swich的定义:

switch(switch表达式){ case value1:语句(组); break; ... case valueN:语句(组); break; default:语句(组);}

规则:

1) switch表达式必须能够算出一个char,byte,short,int,String类型的值。

2) value1...valueN的值必须与switch表达式的值类型一样,且必须是常量。

3) default和break是可选的。

4) 第一个case语句开始,switch表达式的值逐个与case语句的值进行匹配,当进行到某个case语句时,发现与其值匹配上了,就从执行该case子句;如果所有的case都匹配不上就执行default语句(若有的话)。注意,遇到break语句就结束,switch语句就执行完了。

看着是不是比if语句复杂些,那么它有啥用呢?

譬如有下面的多分支if:

If(condition1){ ......}if else(condition2){ ......}......//其他 if else}if else(conditionn){ ......}else{ ......}

是不是被吓到了,有太多分支,有没有简化代码的方法呢?

当然有,那就是switch语句。若将上面的用switch重写,会是下面的样子:

switch(condition){ case condition1:.....; break; case condition2:......; break; ......//其他case子句 case condition[n-1]:.......; break; default:语句(组);}

看着是不是清爽、有条例呢。

上面没有解释case子句没有break语句的情形,那么如果不含break会怎样呢?

看下面的例子:

愉快地学Java语言:第三章 选择

居然两条输出语句都执行了。

我们得出的结论就是:如果case子句中没有break,那么下一个case子句也会执行,如果还是没遇到break,那么下下一个也会执行,依次类推。

你想看的这个系列的其他文章在这里:

愉快地学Java语言:第一章简介第1讲

愉快地学Java语言:第一章简介第2讲

愉快地学Java语言:第二章基本程序设计 第1讲

愉快地学Java语言:第二章基本程序设计 第2讲

Simulink仿真实践之经典建模思想(精华篇)

今天通过一个简单的仿真模型,跟大家分享一下MATLABSimulink建模的技巧,帮助大家快速掌握Simulink建模思想。

实现功能:限定一个输入信号的最大最小值。

简介:输入函数为y=sin x,要求输出值最大为0.5,最小值为-0.8。

首先介绍一个非常简单的方法,同时也介绍一个模块:

Simulink仿真实践之经典建模思想(精华篇)

Saturation Dynamic

如上图所示,想实现以上功能,直接调用Saturation Dynamic(Logic and Bit Operations)模块即可,该模块第1输入引脚输入最大限定值0.5,第2输入引脚为Sine模块输出值(y=sinx),第3输入引脚为最小限定值-0.8.输出值如上图所示,输出值最大为0.5,最小值为-0.8,以及-0.8~0.5之间的值。

上边实例可以很简单的实现以上想要的输出信号,下面给大家介绍另一种思路,主要是体会其中的建模思想,并可以顺便深度了解一些模块的用法:

Simulink仿真实践之经典建模思想(精华篇)

模型总览

该模块用到的模块有:

1. Constant(Sources):输入一个固定值,在该模型中主要输入最大值和最小值(均为固定值);

Simulink仿真实践之经典建模思想(精华篇)

Constant

2. Sine Wave(Sources):正弦波输入信号,在该模型总主要输入未经处理的正弦波信号;

Simulink仿真实践之经典建模思想(精华篇)

Sine Wave

3. Relational Operator(Logic and Bit Operations):关系运算模块,比较第一引脚输入和第二引脚输入的值,如果为真,则输出为1,如果为假,则输出0;

Simulink仿真实践之经典建模思想(精华篇)

Relational Operator

4. Switch(Signal Routing):开关模块,共有三个输入引脚,该模块第二引脚设有一个固定值,第二引脚输入值与该固定值做关系运算,如果为真,则Switch模块输出值为第一引脚输入值,如果为假,则Switch模块输出值为第三引脚出入值;

Simulink仿真实践之经典建模思想(精华篇)

Switch

5. Scope(Sinks):观察输入信号和输出信号之间的关系。

该模型就给大家介绍到这里,感兴趣的朋友们可根据模块功能的具体介绍分析一下上述模型是如何实现限定输出值得最大最小值得,该模型虽简单,但是用到的模块都是在Simulink建模过程中非常常用的模块,最主要的是提供给大家一个建模仿真的思路。看完这篇文章,你也算正式踏进仿真大门了。

怎么样,如果有不明白的地方可以私信我,小编会给您详细的解答。或者您有更好的思路也可联系我。

(需要该模型的源文件,请关注后私信我。)

26「研读分享」聊一聊Mathematica中的基本量

分享兴趣、传播快乐、增长见闻、留下美好,大家好,这里是LearningYard学苑,今天小编为大家带来文章:聊一聊Mathematica中的基本量。

26「研读分享」聊一聊Mathematica中的基本量

首先,自计算机语言诞生以来,人类使用计算机进行一些特定的工作时,往往需要将我们的需求转化为计算机能识别的语言。我们用Mathematica进行数学分析或者画图时,需要输入一段命令(指令),今天我们要讲的就是这些命令都是如何构成以及尝试分析构成命令的那些量的逻辑。

通过查看我导师分享的mathematica软件参考书籍,当我们讨论mathematica中有哪些基本量时,参考的回答如下:数、变量、函数、表、表达式、常用的符号。

附上今日推文的思维导图。

26「研读分享」聊一聊Mathematica中的基本量

图0

01 数

Mathematica中如果结果为整数,则在out中会尽数展示所有的数字。

对于有理数,mathematica一般用分数展示一个除不尽的数;对于实数,mathematica将其表示为小数形式或者指数形式;复数是由实部和虚部组成,实部和虚部可以用整数、实数、有理数表示。在Mathematica中,用I 表示虚数单位如。

26「研读分享」聊一聊Mathematica中的基本量

图1

要实现数据类型的转换,可以参考以下函数:

N[x] 将x转换成实数

N[x,n] 将x转换成近似实数,精度为n

Rationalize[x] 给出x的有理数近似值

Rationalize[x,dx] 给出x的有理数近似值,误差小于dx

建模中常用到的数学常数有:

26「研读分享」聊一聊Mathematica中的基本量

图2

02 变量

2.1有效的变量命名格式法

为了与mathematica自带的函数区别开来,变量名称首字母用小写,除了使用字母,还可以使用字母加数字等命名格式。

2.2如何给变量赋值

不管是单变量赋值还是多变量赋值,都可以采用“变量名=数、图片、公式等”的格式。特别的,对多个不同的变量赋值时,要先用{}将需要赋值的变量框起来。

26「研读分享」聊一聊Mathematica中的基本量

图3

当需要清除变量的值时,用用=.清除它的值,如果变量本身也要清除用函数Clear[],例如:

26「研读分享」聊一聊Mathematica中的基本量

图4

2.3对于一个给定的表达式,想要观察当变量x变化为不同的值时,表达式值的变化,可以用到变量替换的表达公式,例如:

26「研读分享」聊一聊Mathematica中的基本量

图5

03 函数

3.1mathematica中那些自带的函数

下面这张图列举了一般的函数,感兴趣的小伙伴可以自行到mathematica中的参考文档里查找更多。

26「研读分享」聊一聊Mathematica中的基本量

图6

3.2如何在mathematica中自定义函数

当我们求解一个具体的模型时,自定义函数可以说是必选项。首先,我们一起了解一下最基本最简单的函数定义法-即时定义函数法。

语法如下:f[x_]=expr函数名为f,自变量为x,expr是表达式。在执行时会把expr 中的x都换为f的自变量x (不是x_ )。函数的自变量具有局部性,只对所在的函数起作用。函数执行结束后也就没有了,不会改变其它全局定义的同名变量的值。

26「研读分享」聊一聊Mathematica中的基本量

图7

下面跟大家分享一下,延迟定义函数的方法。延迟定义函数从定义方法上与即时定义的区别为 “=“ 与”:=“延迟定义的格式为f[x_]:=expr其他操作基本相同。那么延迟定义和即时定义的主要区别是什么?即时定义函数在输入函数后立即定义函数并存放在内存中并可直接调用。延时定义只是在调用函数时才真正定义函数【1】。总之两种方法都可以,使用哪一种大家凭喜好决定。

上述定义的函数比较简单,只含有一个表达式。在日常的学习环境中,特别是饭饭现在在学运筹学,有很多分段函数。那么分段函数如何定义呢。根据分段函数的内涵,我们可以想到用if条件语定义。具体例子如下:

26「研读分享」聊一聊Mathematica中的基本量

图8

对于上面这个分段函数,我们先用if语句来构思一下。

If语句的调用格式为If[条件,值1,值2],如果条件成立取”值1”,否则取”值2”

运用到本案例中,我尝试做一做:

26「研读分享」聊一聊Mathematica中的基本量

图9

在点击enter+ctrl运行函数表达式时,三种方式展现的结果不一样。原因在于用=还是用:=,在运行时是有差别的。之后随便带入两个数,验证一下,结果是只有命令方式2可以输出正确的结果。

26「研读分享」聊一聊Mathematica中的基本量

图10

当然,我们也可以不用if语句来定义,可以在g[x_]上适当调整,达到我们想要的结果。操作如下图中的命令9:

参考教程,我们可以使用条件运算符号,基本格式为:f[x_]:=expr/;condition ,当condition条件满足时才把expr赋给f(x)。

26「研读分享」聊一聊Mathematica中的基本量

图11

碎碎念->结合变量的替换公式和分段函数的定义,我们可以看出在mathematica中使用“/”的场景通常是需要对某个表达式或者表达式中的变量设置运行条件或者用其他元素来代替原表达式中的某一部分(这一点,也可以在处理表达式命令格式中体现,具体信息请继续看下去)。

另外,在本次分段函数的定义中,我发现只有用:=定义的函数(如g2[x_])才能被mathematica正确计算。画图验证如下:

26「研读分享」聊一聊Mathematica中的基本量

图12

04 表

4.1新建一个表

首先,我们先了解一下mathematica中表的定义——将一些相互关联的元素放在一起,使它们成为一个整体。既可以对整体操作,也可以对整体中的一个元素单独进行操作。在Mathematica中这样的数据结构就称作表(List)。表{a,b,c}表示一个向量;表{{a,b},{c,d}}表示一个矩阵【1】。

对于元素比较少的表,可以采用直接输入法;对于元素比较多的表,可以用Table函数法

如:

26「研读分享」聊一聊Mathematica中的基本量

图13

4.2提取子表

这一步比较统一,直接用公式“表名[[提取的子表序号]]”。例子如下:

26「研读分享」聊一聊Mathematica中的基本量

图14

特别的,如果表被输出为矩阵格式,则无法用“表名[[提取的子表序号]]”完成提取子表工作。

26「研读分享」聊一聊Mathematica中的基本量

图15

需要提取矩阵中的元素时,参考下面这个案例。

26「研读分享」聊一聊Mathematica中的基本量

图16

05 表达式

表达式是由常量、变量、函数、命令、运算符和括号等组成,它最典型的形式是f[x,y]【1】。

我们在建模时需要用到大量表达式,如定义供应商的利润=总收益-总成本。需要设置三个变量,并且用表达式定义它们的关系。

5.1表达式的相关操作

首先,遇到复杂的表达式,常常需要化简。当我们需要展开某一个表达式时,可以输入相应的命令。

26「研读分享」聊一聊Mathematica中的基本量

图17

5.2关系表达式和逻辑表达式

关系表达式的一般形式是:表达式+关系算子+表达式。其中表达式可为数字表达式、字符表达式或意义更广泛的表达式,如一个图形表达式等。在我们实际运用中,这里的表达式常常是数字表达式或字符表达式【1】。

Mathematica中的关系算子有:

26「研读分享」聊一聊Mathematica中的基本量

图18

有了关系表达式,在循环语句中,我们常常需要用逻辑算子来连接两个关系表达式。这样就形成了一个逻辑表达式。Mathematica中的逻辑算子有:

26「研读分享」聊一聊Mathematica中的基本量

图19

案例如下

26「研读分享」聊一聊Mathematica中的基本量

图20

06 常用符号

这里不单独提供案例,大家可以参考前面的内容。

26「研读分享」聊一聊Mathematica中的基本量

图21

英文版

First of all, since the birth of computer languages, when humans use computers to perform certain tasks, they often need to translate our needs into languages that computers can recognize. When we use Mathematica for mathematical analysis or drawing, we need to enter a command (command). Today we are going to talk about how these commands are composed and try to analyze the logic of the quantities that constitute the command.

By looking at the mathematica software reference books shared by my instructor, when we discuss the basic quantities in mathematica, the answers for reference are as follows: numbers, variables, functions, tables, expressions, commonly used symbols.

Attach a mind map of today’s tweets.

01 number

In Mathematica, if the result is an integer, all numbers will be displayed in out.

For rational numbers, mathematica generally uses fractions to display an inexhaustible number; for real numbers, mathematica expresses it in decimal form or exponential form; complex numbers are composed of real and imaginary parts. The real and imaginary parts can be integers, real numbers, Rational number representation. In Mathematica, use I to represent the imaginary unit such as.

figure 1

To achieve data type conversion, you can refer to the following functions:

N[x] convert x to a real number

N[x,n] converts x into an approximate real number, with a precision of n

Rationalize[x] gives the rational approximation of x

Rationalize[x,dx] gives the rational approximation of x, the error is less than dx

The mathematical constants commonly used in modeling are:

figure 2

02 variables

2.1 Effective variable naming format

In order to distinguish it from the functions that come with mathematica, the first letter of the variable name is lowercase. In addition to using letters, you can also use the naming format such as letters and numbers.

2.2 How to assign values to variables

Regardless of single-variable assignment or multi-variable assignment, the format of "variable name=number, picture, formula, etc." can be used. In particular, when assigning values to multiple different variables, you must first use {} to frame the variables that need to be assigned.

image 3

When you need to clear the value of a variable, use =. to clear its value, if the variable itself must be cleared, use the function Clear[], for example:

Figure 4

2.3 For a given expression, if you want to observe the change of the expression value when the variable x changes to a different value, you can use the expression formula of variable substitution, for example:

Figure 5

03 function

3.1 those built-in functions in mathematica

The following picture lists general functions. Interested friends can find more in the reference documents in mathematica.

Image 6

3.2 How to customize functions in mathematica

When we solve a specific model, a custom function can be said to be a must. First of all, let's take a look at the most basic and simple function definition method-instant definition function method.

The syntax is as follows: f[x_]=expr The function name is f, the argument is x, and expr is an expression. During execution, all x in expr will be replaced by the argument x of f (not x_ ). The argument of a function is local and only affects the function in which it is located. After the function is executed, it will disappear, and will not change the value of other globally defined variables with the same name.

Figure 7

Let me share with you the method of delaying the function definition. The difference between the definition method of the delay definition function and the immediate definition is "=" and":=" The format of the delay definition is f[x_]:=expr and other operations are basically the same. So what is the main difference between the delay definition and the immediate definition? Immediately defined function Defines the function immediately after inputting the function and stores it in the memory and can be called directly. The delay definition only really defines the function when the function is called [1]. In short, both methods are okay. You can decide which one to use.

The function defined above is relatively simple and contains only one expression. In the daily learning environment, especially Fanfan is now learning operations research, there are many piecewise functions. So how to define the piecewise function. According to the connotation of the piecewise function, we can think of using the if conditional definition. Specific examples are as follows:

Figure 8

For the above piecewise function, let's first use the if statement to conceive it.

The calling format of the If statement is If[condition, value 1, value 2], if the condition is true, take "value 1", otherwise take "value 2"

In this case, I tried to do:

Picture 9

When you click enter+ctrl to run the function expression, the three ways show different results. The reason is whether to use = or :=, there is a difference at runtime. After that, just bring in two numbers and verify it. The result is that only command mode 2 can output the correct result.

Picture 10

Of course, we can also define without the if statement, and adjust it appropriately on g[x_] to achieve the result we want. Operate the command 9 in the following figure:

Refer to the tutorial, we can use conditional arithmetic symbols, the basic format is: f[x_]:=expr/;condition, expr is assigned to f(x) only when the condition is satisfied.

Picture 11

Sui Sui Nian -> Combining the variable substitution formula and the definition of the piecewise function, we can see that the use of "/" in mathematica usually requires setting operating conditions for a certain expression or variable in the expression or using other Element to replace a certain part of the original expression (this point can also be reflected in the processing expression command format, please continue to read for specific information).

In addition, in the definition of this piecewise function, I found that only functions defined with: = (such as g2[x_]) can be correctly calculated by mathematica. The drawing verification is as follows:

Picture 12

04 Table

4.1 Create a new table

First, let's first understand the definition of tables in mathematica-put some interrelated elements together to make them a whole. You can operate on the whole or on an element in the whole individually. In Mathematica, such a data structure is called a list (List). The table {a, b, c} represents a vector; the table {{a, b}, {c, d}} represents a matrix [1].

For tables with fewer elements, you can use the direct input method; for tables with more elements, you can use the Table function method

like:

Figure 13

4.2 Extract sub-table

This step is more uniform, directly use the formula "table name [[extracted sub-table number]]". Examples are as follows:

Picture 14

In particular, if the table is output in a matrix format, you cannot use "table name [[extracted sub-table number]]" to complete the sub-table extraction work.

Figure 15

When you need to extract elements in the matrix, refer to the following case.

Figure 16

05 expression

An expression is composed of constants, variables, functions, commands, operators, and parentheses. Its most typical form is f[x,y][1].

We need to use a lot of expressions when modeling, such as the definition of supplier profit = total revenue-total cost. Need to set up three variables, and use expressions to define their relationship.

5.1 Related operations of expressions

First of all, when encountering complex expressions, it is often necessary to simplify them.

When we need to expand a certain expression, we can enter the corresponding command.

Figure 17

5.2 Relational expressions and logical expressions

The general form of relational expression is: expression + relational operator + expression. The expression can be a numeric expression, a character expression, or an expression with a broader meaning, such as a graphic expression. In our actual application, the expressions here are often numeric expressions or character expressions [1].

The relational operators in Mathematica are:

Figure 18

With relational expressions, in loop statements, we often need to use logical operators to connect two relational expressions. This forms a logical expression. The logical operators in Mathematica are:

Figure 19

The case is as follows

Picture 20

06 Commonly used symbols in Mathematica

Picture 21

由于篇幅有限,今天的分享就到这里啦

Due to limited space, that's all for today's sharing

感兴趣的同学可以留言与小编交流,

咱们下周见!

往期精彩:

参考资料:

[1]Mathematica8教程.

[2]田琦师姐的学习文档.

英文翻译:Google翻译。

本文由LearningYard学苑整理并发出,如有侵权,请联系删除!

提问者:我的淫荡成就了你的高潮 relational operator


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3