$\mathbf{Question:}$ Find the real numbers $a$ and $b$ such that the following function is differentiable at $x=0$
$$ f(x)= \begin{cases} x^{2}+1 &x≥0\\ a\sin x+b\cos x & x<0\\ \end{cases} $$
$\mathbf{My\ attempt:}$
$$ \begin{align} \lim_{x\to 0-}f(x) & =\lim_{x\to 0-}a\sin x +b\cos x \\ & = a\sin (0) + b\cos (0) = b \end{align} $$
$$ \begin{align} \lim_{x \to 0+}f(x) & = \lim_{x \to0+}x^{2}+1 =1 \end{align} $$
So if $f(x)$ is continuous, $\lim_{x \to0-}f(x) = \lim_{x \to0+}f(x)=b$
Therefore, $b=1$
To find $a$, we can set $f'(0)=f'(0)$:
$$ \begin{align} \frac {d}{dx}[x^{2}+1]&=\frac {d}{dx}[a\sin x +b\cos x]\\ 2x&=a\cos x-b\sin x\\ 0&=a(1)+b(0)\\ a&=0\\ \end{align} $$ Therefore, $a=0$
Thus, $ f(x)= \begin{cases} x^{2}+1 &x≥0\\ \cos x & x<0\\ \end{cases} $ is differentiable at $x=0$
This is exactly what one should not do while dealing with differentiability. Instead always use the definition of derivative.
The left derivative at $0$ is given by $$f_{-}'(0)=\lim_{x\to 0^{-}}\frac{f(x)-f(0)}{x}=\lim_{x\to 0^{-}}\frac{a\sin x +b\cos x - 1}{x} =\lim_{x\to 0^{-}}a\frac{\sin x} {x} - \frac{1-\cos x} {x} =a$$ and the right derivative of $f$ at $0$ is given by $$f_{+}' (0)=\lim_{x\to 0^{+}}\frac{f(x)-f(0)}{x}=\lim_{x\to 0^{+}}\frac{x^2+1-1}{x}=0$$ Since the derivative $f'(0)$ exists we must have these two limits same and therefore $a=0$.
Your approach is not at all about differentiability, but it is rather about continuity of the derivative $f'$. And the reason it works for some problems (like the current one) is because derivatives don't have jump discontinuity. And your technique will simply not work in case the derivative is discontinuous at point under consideration.
Try that approach with $$f(0)=0,f(x)=x^2\sin(1/x),x>0$$ and $$f(x) =-x^2\sin(1/x),x<0$$