Error in my code using syms+gradient function

Hello,
I am expriencing some difficulties with matlab. I am trying to make a program that calculates the values of a function f, and its corresponding gradient (the code is below). The problem is when I try to calculate the gradient of the function f, I get a message saying:
_Error using sym>notimplemented (line 2682) Function 'subsindex' is not implemented for MuPAD symbolic objects.
Error in sym/subsindex (line 1388) notimplemented('subsindex');_
What could be the problem?
Thank you,
d.
function [out,dnx,dny]=nr(x,y,param)
%function out=nr(x,y)
A=param.A;
B=param.B
C=param.C;
offx=param.offx;
offy=param.offy;
%%_HERE SOME CODE CHECK FOR THE VARIABLES TO ACTUALLY BE PRESENT, AND SETS THEM TO DEFAULT VALUE OTHERWISE_
switch expresion
case 'two suns symmetrical'
if flagx==1
offx=15;
if flagy==1
offy=0;
end
end
r1=(x-offx).^2 + (y-offy).^2;
r2=(x+offx).^2 + (y-offy).^2;
f=A.*(exp(-r1./B^2)+exp(-r2./B^2)) + C;
case 'two suns antisymmetrical'
if flagx==1
offx=15;
if flagy==1
offy=15;
end
end
r1=(x-offx).^2 + (y-offy).^2;
r2=(x+offx).^2 + (y+offy).^2;
f=A.*(exp(-r1./(B.^2))+exp(-r2./(B.^2))) + C;
end
if gradient==1;
g=gradient(f,[X,Y]);
dnx=subs(g(1),[X Y],{x, y});
dny=subs(g(2),[X Y],{x, y});
end
out=subs(f,[X Y], {x, y});

Answers (1)

There is no definition for X or Y and there is no reason for us to expect that they appear in f. Perhaps you should be taking the gradient in terms of x and y directly? x and y are implicitly symbolic, right?

Asked:

on 3 Apr 2012

Community Treasure Hunt

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

Start Hunting!