MATLAB: Take derivative of a function

functionfunction of function

Hi, I have a function that I defined, and I would like to take the first derivative of this function. I tried different ways. But the program does not work. I must missed something. Below is my code.
%y is independent variable, a and b are parameters;
function [f,c1,c2]=fun(y,a,b)
f=a*log(c1)+b*log(c2);
c1=y*a;
c2=y*b;
end
Then I tried to take the derivative of f with respect to y. And I also want to check the value of c1 and c2 (in terms of y). So I wrote the following code.
[f,c1,c2]=fun(y,a,b);
g=diff(f,y)
I got the following error message
"Undefined function or variable 'y'"
Please help. Thanks.

Best Answer

syms y
before the [f,c1,c2] assignment.