take derivative of a function

3 visualizaciones (últimos 30 días)
Xin Zhao
Xin Zhao el 29 de Abr. de 2016
Comentada: Xin Zhao el 30 de Abr. de 2016
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Abr. de 2016
syms y
before the [f,c1,c2] assignment.
  2 comentarios
Xin CUI
Xin CUI el 29 de Abr. de 2016
Thanks. I got g as an expression in terms of y(independent variable) and a,b(parameters). Now the next step is to find the solution of g=0. I wrote the following x=fsolve(g,y0) but got error messages Error using lsqfcnchk (line 108) If FUN is a MATLAB object, it must have an feval method.
Error in fsolve (line 198) funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Could you see the problem?
Walter Roberson
Walter Roberson el 29 de Abr. de 2016
Use solve() rather than fsolve()

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 29 de Abr. de 2016
Use symbolic expression:
syms f a b
f = a*log(y*a)+b*log(y*b)
df = diff(f)
  2 comentarios
Xin CUI
Xin CUI el 29 de Abr. de 2016
Thanks. I got df as an expression in terms of y(independent variable) and a,b(parameters). Now the next step is to find the solution of df=0. I wrote the following x=fsolve(df,y0) but got error messages Error using lsqfcnchk (line 108) If FUN is a MATLAB object, it must have an feval method.
Error in fsolve (line 198) funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Could you see the problem?
Xin Zhao
Xin Zhao el 30 de Abr. de 2016
Thank you!

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by