MATLAB中M文件 Day1

语法知识

函数M文件

  1. 定义: 编写MATLAB内装函数的文件。
  2. 组成:点击新建脚本(命令窗口edit+回车),即可打开M文件编辑器。函数定义行(左:由function开头+函数输出变量方括号括起逗号隔开//右:数名+输入变量,圆括号括起,逗号隔开 )、H1行、帮助文本内容、函数体、附注。
  3. 内联函数:对于一次性使用的函数,可以用内联函数inline来实现。编制宽松,但不进行函数存储,书写格式为:在这里插入图片描述
  4. 文本M文件
  5. M文件的编辑和保存

实例演练

syms x
y=x^3+6*x^2+11*x+6;
y1=sym2poly(y)
y1 =
1     6    11     6
roots(y1)
ans =
-3.0000
-2.0000
-1.0000
function A= triarea(a,b,c)
if a<0||b<0||c<0
    disp('The a,b,c must be positive number .')
end
if a+b<c||a+c<b||b+c<a
    disp('这是不可能的')
end
s=(a+b+c)/2;
A=s*(s-a)*(s-b)*(s-c)^(1/2);
  1. 内联函数
y=inline('sin(x)+sin(x)^2','x')
y=
inline function :
y(x)=sin(x)+sin(x)^2
y(pi/4)
theat=-pi:pi/300:pi;
rho(1,:)=2*sin(5*theta).^2;
rho(2,:)=cos(10*theta).^3
rho(3,:)=sin(theta).^2;
for i=1:4
polar(theta,rho(i,:))
switch i
case 1
title 10-petal
case 2
title 20-petal
case 3
title two-petal
case 4
title 2-3-2unsymmetrical-petal
end
pause
end

总结思考:

  • 疑点:matlab中哪里用"‘",哪里用"."
  • 文件编辑器的使用。

语法知识

函数M文件

  1. 定义: 编写MATLAB内装函数的文件。
  2. 组成:点击新建脚本(命令窗口edit+回车),即可打开M文件编辑器。函数定义行(左:由function开头+函数输出变量方括号括起逗号隔开//右:数名+输入变量,圆括号括起,逗号隔开 )、H1行、帮助文本内容、函数体、附注。
  3. 内联函数:对于一次性使用的函数,可以用内联函数inline来实现。编制宽松,但不进行函数存储,书写格式为:在这里插入图片描述
  4. 文本M文件
  5. M文件的编辑和保存

实例演练

syms x
y=x^3+6*x^2+11*x+6;
y1=sym2poly(y)
y1 =
1     6    11     6
roots(y1)
ans =
-3.0000
-2.0000
-1.0000
function A= triarea(a,b,c)
if a<0||b<0||c<0
    disp('The a,b,c must be positive number .')
end
if a+b<c||a+c<b||b+c<a
    disp('这是不可能的')
end
s=(a+b+c)/2;
A=s*(s-a)*(s-b)*(s-c)^(1/2);
  1. 内联函数
y=inline('sin(x)+sin(x)^2','x')
y=
inline function :
y(x)=sin(x)+sin(x)^2
y(pi/4)
theat=-pi:pi/300:pi;
rho(1,:)=2*sin(5*theta).^2;
rho(2,:)=cos(10*theta).^3
rho(3,:)=sin(theta).^2;
for i=1:4
polar(theta,rho(i,:))
switch i
case 1
title 10-petal
case 2
title 20-petal
case 3
title two-petal
case 4
title 2-3-2unsymmetrical-petal
end
pause
end

总结思考:

  • 疑点:matlab中符号使用以及基本函数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值