How to insert equations in MATLAB? (Ⅰ)

This is Chinese Version
[中文版]


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 and end 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)=111113800(x0.0125)115800(x0.01)117800(x0.0075)1111800(x0.005)x>0.0150.0125<x0.0150.01<x0.01250.0075<x0.01x0.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
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值