%% Tutorial 2: Basic Functions & Derivatives
%
%% Question 1:  Find y^{\prime } for $(x-y)^2=x+y-1$ by using implicit differentiation.

syms y(x) DY;
eqn=(x-y)^2==x+y-1;
dy=diff(y);
deqn = diff(eqn,x);
Deqn = subs(deqn, dy, DY);
DYsol = simplify( solve(Deqn, DY) );
disp(DY == DYsol)

%
%% Question 2: Implicit differentiation to find an equation of the tangent line to the curve $5x^2+10xy^2+5y=20$ at point $(2,2)$.

syms y(x) DY c
eqn=5*x^2+10*x*y^2+5*y==20;
dy=diff(y);
deqn = diff(eqn,x);
Deqn = subs(deqn, dy, DY);
DYsol = simplify( solve(Deqn, DY) );
disp(DY == DYsol)
gradient = subs(subs(DYsol,x,2),y,2) %The gradient
intercept = solve(2==gradient*2+c) % Intersection point
disp(y==gradient*x+intercept)

%
%% Question 3: $y\cos{x}=1+\sin{(xy)}$. Find $\frac{dy}{dx}$ by implicit differentiation.

syms y(x) DY;
eqn=y*cos(x)==1+sin(x*y);
dy=diff(y);
deqn = diff(eqn,x);
Deqn = subs(deqn, dy, DY);
DYsol = simplify( solve(Deqn, DY) );
disp(DY == DYsol)

%
%% Question 4: Find $\frac{dy}{dx}$ by implicit differentiation $4\cos{x}\sin{y}=1$.

syms y(x) DY;
eqn=4*cos(x)*sin(y)==1;
dy=diff(y);
deqn = diff(eqn,x);
Deqn = subs(deqn, dy, DY);
DYsol = simplify( solve(Deqn, DY) );
disp(DY == DYsol)

%
%% Question 5: An open box is to be made from cutting a square from each corner of a 12 in by 12 in piece of metal and then folding up the sides. What size square should be cut from each corner to produce a box of maximum volume?

syms x;
eqn = 4*x^3-48*x^2+144*x;
solve(diff(eqn))
% When x = 2
subs(eqn,x,2)
% When x = 6
subs(eqn,x,6)

%
%% Question 6:  We are going to fence in a rectangular field. If we look at the field from above the cost of the vertical sides are RM 10/ft, the cost of the bottom is RM2/ft and the cost of the top is RM 7 /ft. If we have RM 700 determind the dimensions of the field that will maximize the enclosed area.

syms x;
eqn = x*(35-(9/20)*x)
ans_x = solve(diff(eqn))
ans_y = 35-(9/20)*ans_x