clc;
%% how to define a function (single line definition)

% f(x) = x*exp(x^2);
f = @(t) t*exp(t^2);
% want to get f(0.4)
disp(f(0.4))
fprintf(f(0.4))

% show that f(x) has a root in between a and b
% f = @(t) complicated functions
% fa = f(a); fb = f(b)
% fprintf("f(a)=%f, f(b)=%f, and they have different sign", fa, fb)
f = @(t) t^3+4.001*t^2-4.002*t+1.004;
a = -10; b = +10;
fa = f(a); fb = f(b);
fprintf("f(%f)=%f, f(%f)=%f, and they have different sign", a, fa, b, fb)

%% how to use the symbolic library 
syms x
% define symbolic function
sym_f = x*exp(x^2);
fprintf("f = %s\n", sym_f)
% convert symbolic function to regular function
reg_f = matlabFunction(sym_f);
target = 0.4;
fprintf("f(%f) = %f\n", target, reg_f(target))

deriv_f = diff(sym_f);
fprintf("f'(x) = %s\n", deriv_f)
sd_f = diff(sym_f, 2);
fprintf("f''(x) = %s\n", sd_f)

% taylor(yourfunction, variable, x0, 'Order', order)
x0 = 0;
p4 = taylor(sym_f, x, x0, 'Order', 4);
fprintf("P4(x) = %s\n", p4)

reg_p4 = matlabFunction(p4);
fprintf("True value f(%.2f) = %.6f\n", target, reg_f(target));
fprintf("Appx value P4(%.2f) = %.6f\n", target, reg_p4(target));
fprintf("Abs err |f(%.2f)-P4(%.2f)| = %.6f\n", target, target, abs(reg_f(target)-reg_p4(target)));

% Repeat this with f(x) = 2*x*cos(2*x)-(x-2)^2 <-- problem 16
% Show me three lines(True, Appx, Abs), then good to go.








