How to apply functions to symmatrix objects, in the same way as to sym objects.

6 Ansichten (letzte 30 Tage)
Hi
I work with symbolic matrix objects, defined as symmatrix.
Unfortunatly, symmatrix is not compatible with as many functions as a symbolic scalar (sym). In my case i would like to apply normcdf or equivalently erf to a symmatrix matrix (as an elementwise operation, like it could be done with a double matrix). This cant be done.
% works for scalars
syms y x;
a=erf(y*x);
b=normcdf(y*x);
diff(a,x)
ans = 
diff(b,x)
ans = 
% fails for symmatrix
Y = symmatrix('Y',[2,1]);
syms x
a=erf(Y*x)
Incorrect number or types of inputs or outputs for function erf.
b=normcdf(Y*x)
diff(a,x)
diff(b,x)
I am wondering if there is a workaround via an abstract function. I thought maybe symfunmatrix helps, but I couldnt get it to do what I want.
Importantly, I dont want any conversion to sym, because I want to later use the resulting matrix expressions for numeric evaluation for arbitrary n. Conversion to sym would turn matrix expressions into element by element operatons and make the numeric evaluation slow.
  6 Kommentare
dominik
dominik am 23 Apr. 2024
The above is just a simple example. Ultimatley, I want to be able to take the jacobian of a system of equations f(Y,x)=0 where Y is a matrix, x a scalar and f(Y,x) is a vector: diff(f,[x Y]) The funtional form of f(x,Y) should be general enough to accomodate matrix operations (such as *) and elementwise operations (such as .*, epx or erf). .* and exp work, but erf for example doesn't. Generic functions g(x,Y), which could serve as a workaround for the absence of functions such as erf, also don't seem to work.
Importantly, I want symmatrix matrices and not elementwise syms matrices such as in your example. Subs could have been a workaround but it fails too.
syms y x;
a = erf(y*x);
da = diff(a,x)
da = 
Y = symmatrix('Y',[2,1]); %this is the only line different from your example above
da = subs(da,y,Y)
Error using symmatrix (line 37)
Unable to convert the argument to class 'symmatrix'.

Error in symbolic.mixin.symbolicmatrix/subs (line 353)
Y = symmatrix(Y);
Paul
Paul am 24 Apr. 2024
o.k., now I better understand what you're trying to do
syms x
syms Y [2 1] matrix
z = exp(Y*x)
z = 
diff(z,x)
ans = 
diff(z,Y)
ans = 
I guess that works for exp but not for erf because the former is a method of the symmatrix class and the latter is not
ismember({'exp','erf'},methods(z))
ans = 1x2 logical array
1 0
I see what you're trying to do by defining everything with scalar sym variables, but as yet I don't see a way to "expand" a sym expression into a symmatrix. Maybe there's a way to do that by parsing the expression into its constituent operators and operands, subbing sym for symmatrix, and then recombining it all.
You may want to consider submitting an enhancement request to add erf to the symmatrix class.
Or maybe someone else can figure out a way to do this ....

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by