This is Chinese Version
[中文版]
Content
1 Create a function
This is for defining complex functions
Click ‘New Script’ to create a new m file.
Present filename is ‘Untitled*’, ‘*’ means changes haven’t been saved.
To define a function, we need to start with function
, and end with end
, MATLAB will automatically show the structure of the function.
Contents between
function
andend
should end with;
in every row.
Filename must be the same as the function name, or an error will report
Function name 'f' is known to MATLAB by its file name:'Untitled'.
2 Elementary Functions
2.1 Trigonometric Function & Inverse Trigonometric Function
π in code is
pi
2.1.1. Trigonometric Functions (Radian Measure)
Sine y = sin(x);
Cosine y = cos(x);
Tangent y = tan(x);
Cotangent y = cot(x);
Secant y = sec(x);
Cosecant y = csc(x);
2.1.2 Inverse Trigonometric Functions (Radian Measure)
Arcsine y = asin(x);
Arccosine y = acos(x);
Arctangent y = atan(x);
Arccotangent y = acot(x);
Arcsecant y = asec(x);
Arccosecant y = acsc(x);
2.1.3 Trigonometric Functions (Degree Measure)
Sine y = sind(x);
Cosine y = cosd(x);
Tangent y = tand(x);
Cotangent y = cotd(x);
Secant y = secd(x);
Cosecant y = cscd(x);
2.1.4 Inverse Trigonometric Functions (Degree Measure)
Arcsine y = asind(x);
Arccosine y = acosd(x);
Arctangent y = atand(x);
Arccotangent y = acotd(x);
Arcsecant y = asecd(x);
Arccosecant y = acscd(x);
2.1.5 Hyperbolic Functions
Hyperbolic sine y = sinh(x);
Hyperbolic cosine y = cosh(x);
Hyperbolic tangent y = tanh(x);
Hyperbolic cotangent y = coth(x);
Hyperbolic secant y = sech(x);
Hyperbolic cosecant y = csch(x);
2.1.6 Inverse Hyperbolic Function (IHF)
Inverse hyperbolic sine y = asinh(x);
Inverse hyperbolic cosine y = acosh(x);
Inverse hyperbolic tangent y = atanh(x);
Inverse hyperbolic cotangent y = acoth(x);
Inverse hyperbolic secant y = asech(x);
Inverse hyperbolic cosecant y = acsch(x);
2.2 Others
Const function y = a;
Power function y = x^b;
Logarithm function y = logc(x);
or natural logarithm y = log(x);
Exponential function y = d^x;
parameter a, b, c or d is up to you.
3 Piecewise Function
Consider a piecewise function,
f
(
x
)
=
{
1
11
x
>
0.015
3
−
800
⋅
(
x
−
0.0125
)
11
0.0125
<
x
≤
0.015
5
−
800
⋅
(
x
−
0.01
)
11
0.01
<
x
≤
0.0125
7
−
800
⋅
(
x
−
0.0075
)
11
0.0075
<
x
≤
0.01
11
−
800
⋅
(
x
−
0.005
)
11
x
≤
0.0075
f(x)=\left\{\begin{matrix} \\\frac{1}{11} & x>0.015 \\ \\\frac{3-800\cdot(x-0.0125)}{11} & 0.0125<x\leq0.015 \\ \\\frac{5-800\cdot(x-0.01)}{11} & 0.01<x\leq0.0125 \\ \\\frac{7-800\cdot(x-0.0075)}{11} & 0.0075<x\leq0.01 \\ \\\frac{11-800\cdot(x-0.005)}{11} & x\leq0.0075 \end{matrix}\right.
f(x)=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧111113−800⋅(x−0.0125)115−800⋅(x−0.01)117−800⋅(x−0.0075)1111−800⋅(x−0.005)x>0.0150.0125<x≤0.0150.01<x≤0.01250.0075<x≤0.01x≤0.0075
how to define it?
Here are 2 ways.
3.1 Conditional Statement
function y=f(x)
if x>0.015
y=1/11;
elseif (x>0.0125)& (x<=0.015)
y=3/11-800/11* (x-0.0125);
elseif (x>0.01)&(x<=0.0125)
y=5/11-800/11* (x-0.01);
elseif (x>0.0075)&(x<=0.01)
y=7/11-800/11*(x-0.0075);
else
y=1-800/11*(x-0.005);
end
3.2 Logical Operation
After logical operating (like [and] (code&
) [or] (code|
) , etc.), it will return a 0 or 1. We can complete this task in only one row.
function y=f(x)
y=(1-800/11*(x-0.005)).*(x<=0.005)+(7-800/11*(x-0.0075)).*(x>0.0075&x<=0.01)+(5-800/11*(x-0.01)).*(x>0.01&x<=0.0125)+(3-800/11*(x-0.0125)/0.0025).*(x>0.0125&x<=0.015)+(1/9).*(x>0.015);
end