语法知识
函数M文件
- 定义: 编写MATLAB内装函数的文件。
- 组成:点击新建脚本(命令窗口edit+回车),即可打开M文件编辑器。函数定义行(左:由function开头+函数输出变量方括号括起逗号隔开//右:数名+输入变量,圆括号括起,逗号隔开 )、H1行、帮助文本内容、函数体、附注。
- 内联函数:对于一次性使用的函数,可以用内联函数inline来实现。编制宽松,但不进行函数存储,书写格式为:
- 文本M文件
- 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);
- 内联函数
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文件
- 定义: 编写MATLAB内装函数的文件。
- 组成:点击新建脚本(命令窗口edit+回车),即可打开M文件编辑器。函数定义行(左:由function开头+函数输出变量方括号括起逗号隔开//右:数名+输入变量,圆括号括起,逗号隔开 )、H1行、帮助文本内容、函数体、附注。
- 内联函数:对于一次性使用的函数,可以用内联函数inline来实现。编制宽松,但不进行函数存储,书写格式为:
- 文本M文件
- 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);
- 内联函数
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中符号使用以及基本函数