1.1 函數的定義及賦值
方式一,定義變量,創建函數(常用):
MATLAB代碼:
syms a
f(a)=2*a
f(2)
運行結果:
f(a) =
2*a
ans =
4
方式二,直接定義函數:
MATLAB代碼:
syms f(t)
f(t)=t^2
f(2)
運行結果:
f(t) =
t^2
ans =
4
方式三,內聯函數:
MATLAB代碼:
h_expr =@(x)(sin(x) + cos(x));
h_expr(0)
運行結果:
ans =
1
1.2 復合函數
MATLAB代碼:
clc;clear;
syms x t
f(x)=x^2;
g(t)=2*t;
subs(f(x),x,g(t))
運行結果:
ans =
4*t^2
Tip:
- subs(s, old, new)
s-第一個函數句柄
old-第一個函數中要替換的變量
new-第一個函數句柄
1.3 反函數
MATLAB代碼:
clc;clear;
syms x
f(x) =1/tan(x);
g = finverse(f)
運行結果:
g(x) =
atan(1/x)
1.4 取整函數
MATLAB代碼:
clc;clear;
x = sym(-5/2);
[fix(x) floor(x) round(x) ceil(x)]
運行結果:
ans =
[ -2, -3, -3, -2]
Tips:
- fix(x)-靠近0取整;
- floor(x)-靠近負無窮取整;
- round(x)-四舍五入取整,當在中間時,取遠離0的整數;
- ceil(x)-靠近正無窮取整;
1.5 判斷函數奇偶性
MATLAB代碼:
clc;clear;
syms x
f(x)=sin(x);
% 奇函數判斷
isequal(f(x),-f(-x))
% 偶函數判斷
isequal(f(x),f(-x))
運行結果:
ans =
1
ans =
0
1.6 常見的運算
1.6.1 關系運算符
相等:==或eq
大於等於:>=或ge
大於:>或gt
小於等於:<=或le
小於:<或lt
不相等:~=或ne
相等(對於表達式):isequal()
1.6.2 邏輯運算符
與:&或and
或:|或or
非:~或not
1.6.3 對數
In(x):log(x)
log10x:log10(x)
log2x:log2(x)
1.6.4 三角函數
正弦:sin
余弦:cos
正切:tan
余切:cot
正割:sec
余割:csc
反正弦:asin
反余弦:acos
反正切:atan
反余切:acot
反正割:asec
反余割acsc
1.6.5 雙曲函數
雙曲正弦:sinh
雙曲余弦:cosh
雙曲正切:tanh
雙曲余切:coth
雙曲正割:sech
雙曲余割:cach
反雙曲正弦:asinh
反雙曲余弦:acosh
反雙曲正切:atanh
反雙曲余切:acoth
反雙曲正割:asech
反雙曲余割:acsch
1.7 求極限
1.7.1 簡單示例
MATLAB代碼:
clc;clear;
syms n
f(n)=(n+(-1)^(n-1))/n;
limit(f(n),n,inf)
運行結果:
ans =
1
Tip:
limit(expr,x, a)
limit(expr, a)
limit(expr)
limit(expr,x, a, 'left')
limit(expr,x, a, 'right')
其中:
expr-待求極限表達式或函數
x-變量名
a-變量趨近的值,無窮為inf
左極限添加參數'left',右極限添加參數'right'。
````
1.7.2 兩個重要極限實驗
MATLAB代碼:
clc;clear;
syms x
f(x)=sin(x)/x;
g(x)=(1+1/x)^x;
limit(f(x),x,0)
limit(g(x),x,inf)
運行結果:
ans =
1
ans =
exp(1)
1.8 函數的連續性與間斷點
根據定義法limit函數求解,本質是求極限,不作實驗。