Calculating the gradient of a function

Hello. I want to calculate the gradient of this function at the point xc:
function MSE=mseFunction(alpha,beta,y,yS)
MSE = [alpha beta; y yS];
end
xc = [100; 102];
y = 20;
yS = 50;
how I should proceed. Thanks!

Respuestas (1)

Marco Morganti
Marco Morganti el 5 de En. de 2017
Editada: Marco Morganti el 6 de En. de 2017
Hi Amine,
you could use gradient() along with symbolic variables to find the gradient of your function MSE().
syms parameters;
f = mseFunction(parameters);
g = gradient(f);
at this point you can evaluate g() at the desired point:
g_xc = eval(subs(g,xc));
I hope this helps

4 comentarios

amine&&
amine&& el 5 de En. de 2017
Editada: amine&& el 5 de En. de 2017
Hello Marco, When I type the following code
syms x1 x2;
f = mseFunction(x1,x2,y,yS);
I get :
The following error occurred converting from sym to double:
Error using symengine (line 59)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in mseFunction (line 33)
LES(i)=alpha.*MoyenneMobileCentreeS(i)+(1-alpha).*LES(i-1);
Moreover I inform you that the code I already use is just a simplification. In fact y and yS are column vectors.
Walter Roberson
Walter Roberson el 5 de En. de 2017
You should never eval() a symbolic expression. Symbolic expressions are in a language that is slightly different than MATLAB. You can matlabFunction the result of gradient() and pass c to that.
Hi Walter, even typing :
f = @(x)mseFunction(x(1),x(2),y,yS)
g = gradient(f)
I get :
Undefined operator '.'' for input arguments of type 'function_handle'.
Error in gradient>parse_inputs (line 146)
f = f.';
Error in gradient (line 48)
[f,ndim,loc,rflag] = parse_inputs(f,varargin);
syms parameters;
f = mseFunction(parameters);
g = gradient(f);
gfun = matlabFunction(g); %rather than eval()
g_xc = gfun(xc);

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 5 de En. de 2017

Editada:

el 6 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by