%% Tutorial 1: Basic Functions & Derivatives
%
%% Limits
% The fundamental idea in calculus is to make calculations on functions as a variable “gets close to” a certain value.
% In MATLAB, we can use the Symbolic Math Toolbox software to calculate the limits of functions directly. The commands are:
% syms x
% limit((x^2-25)/(x^2+x-30), x, 5)
% which will return the answer, (see Question 1 below).
% From the command above, we can see there is two lines. The first line syms x tells MATLAB that the variable we are going to use is x. The second line limit((x^2-25)/(x^2+x-30), x, 5) tells MATLAB that we want to solve the limit for the equation, with x\to 5. The first parameter in limit function is the equation, the second parameter and third parameter is the variable and limit, respectively.
%
%
%% Question 1:
% $$\lim _{x \rightarrow 5} \frac{x^{2}-25}{x^{2}+x-30}$$

syms x
limit((x^2-25)/(x^2+x-30), x, 5)

%% Question 2: $$\lim _{x \rightarrow 9} \frac{\sqrt{x}-3}{x-9}$$

syms x
limit((sqrt(x)-3)/(x-9), x, 9)

%% Question 3: $$\lim _{x \rightarrow 0} \frac{\sqrt{2+x}-\sqrt{2}}{x}$$

syms x
limit((sqrt(2+x)-sqrt(2))/(x), x, 0)

%% Question 4: $$\lim _{\theta \rightarrow \frac{\pi}{2}} \frac{\tan{\theta}}{\sec{\theta}}$$

syms x
limit((tan(x))/(sec(x)), x, pi/2)

%% Question 5:$$\lim _{\theta \rightarrow 0} \frac{\cos{\theta}-1}{\sin{\theta}}$$

syms x
limit((cos(x)-1)/sin(x),x,0)

%% Question 6: If 2x\ge x^2 -x+2 for all x, evaluate $$\lim_{x \rightarrow 1} g(x)$$.

syms x
solve(limit(2*x,x,1)<=x,x<=limit(x^2-x+2,x,1))

%
%% Differentiation
% Example below shows the simple command for differentiation.
% syms x
% f=sin(x);
% diff(f)
% The command diff(f) differentiate f with respect to x, and produces:
% ans =
%
% cos(x)
% To take the second derivative of f, use:
% diff(f,2)
% or
% diff(diff(f))
% This will produce:
% ans =
%
% -sin(x)
%
%
%% Question 7: $$y=\sqrt{3x^2-2x+3}$$

syms x
diff(sqrt(3*x^2-2*x+3))

%% Question 8: $$y=5\sqrt[3]{x^2+\sqrt{x^3}}$$

syms x
diff(5*nthroot((x^2+sqrt(x^3)),3))

% Here, cube-root are being invoked through nthroot(x,3). You could use x^(1/3) too. The same applies for n-th root, with nthroot(x,n) and x^(1/n).
%% Question 9: $$y=\ln{\cos{x^2}}$$

syms x
diff(log(cos(x^2)))

% Here \ln \;x is being called as log(x) in MATLAB. In MATLAB, Y = log(X) returns the natural logarithm \ln \left(x\right) of each element in array X.
%% Question 10: $$y=\log{(4+\cos{x})}$$

syms x
diff(log10(4+cos(x)))

% Note: log10 is the common logarithm with base 10. Note that the log(10) is actually \ln \left(10\right), and \frac{1}{\ln \left(10\right)}=\log \left(e\right)
%% Question 11: $$10e^{2xy}=e^{15y}+e^{13x}$$

syms y(x) DY;
eqn=10*exp(2*x*y)==exp(15*y)+exp(13*x);
dy=diff(y);
deqn = diff(eqn,x);
Deqn = subs(deqn, dy, DY);
DYsol = simplify( solve(Deqn, DY) );
disp(DY == DYsol)

% Implicit differentiation takes more steps! We first define two symbol, y(x) and DY. Then, we type in the equation in eqn variable. Note that we can’t have two = in one line, so to store a equation, we replace the = with ==, where eqn now store the equation. Line 23 define dy as \frac{\mathrm{dy}}{\mathrm{dx}}, and line four differentiate the equation with respect to x. Line 25 substitute the y(x) in differentiated equation with DY, then simplified and solve the equation with respect to DY. Finally, we display the equation. Note that MATLAB might not be a better choice for some question, since it was not designed to handle such equation. Alternatively, you can use Mathematica, which is more suitable.
%% Question 12: $$f(x)=2x(\arctan{5x})^2+6\tan{(\cos{6x})}$$

syms x
diff(2*x*(atan(5*x))^2+6*tan(cos(6*x)))

%% Question 13: $$y=4x\sinh^{-1}{(\frac{x}{6})}+\tanh^{-1}{(\cos{10x})}$$

syms x
diff(4*x*asinh(x/6)+atanh(cos(10*x)))

%% Question 14: $$y=\frac{1}{\sin^{-1}{x}}$$

syms x
diff(1/(asin(x)))

%% Question 15: $$y=(x^3-1)^{100}$$

syms x
diff((x^3-1)^100)

%
% References
% https://www.mathworks.com/help/symbolic/limits.html
% https://www.mathworks.com/help/symbolic/differentiation.html
% https://www.mathworks.com/help/matlab/ref/log.html
% https://www.mathworks.com/help/matlab/ref/log10.html
% https://www.mathworks.com/help/symbolic/solve.html
% Created by Hong Vin | 🌐 kix1001.hongvin.xyz | 📧 math@hongvin.xyz