Force zeros to be be symbolic

8 views (last 30 days)
Leo Simon on 5 Jan 2022
Edited: Paul on 6 Jan 2022
In the following code,
syms x y
f{1,1} = @(x,y) x+y
f{1,2} = @(x,y) 0
else
f{1,2}=@(x,y) sym(0)
end
fMat = cellfun(@(z) feval(z,x,y),f)
the last line will throw an error.
The reason is that the first element of f is symbolic, but second is numeric, and cellfun doesn't like that.
A workaround is to change 0 to sym(0) as the example illustrates, when you set bad to 0.
Matlab's recommended solution is to add '
un',0
to the cellfun command. This works for this toy example, but in my complex application, 'un',0 creates all sorts of other problems.
My current code computes the matlab derivatives, and then, if they are zero, converts the 0's to sym(0) using strrep. This is incredibly awful and is likely to cause all sorts of problems in other settings.
The ideal solution for me would be to have the matlab symbolic diff command return everything as symbolic, including integers. Is there a way to impose that? Thanks for any suggestions.

Walter Roberson on 5 Jan 2022
"The reason I'm having problems is that I'm converting the output from diff to a character string and then writing it to a file. when I run the file"
Do not write it out plain. Write it out with a str2sym() wrapper. You need that anyhow in order to get constants back in with full accuracy.
You have another challenge as well: when you emit @(x,y)0 then you cannot integral() the handle. integral() requires that the output be the same size as the input but @(x,y)0 is scalar 0. To work with a constant C you need @(x,y)C*ones(size(x)) which is not something that matlabFunction() will emit automatically.
3 CommentsShow 1 older commentHide 1 older comment
Paul on 5 Jan 2022
Edited: Paul on 6 Jan 2022
I can't speak for @Walter Roberson, but it may be helpful to show what what you're actually doing, i.e., what is being written to the file and what you intend to do with the file after it's written.
Walter Roberson on 5 Jan 2022
filename = fullfile(tempdir, 'test_str2sym.m');
syms x y
z = x + y
z =
dz = diff(z,x)
dz =
1
d2z = diff(dz,x)
d2z =
0
names_to_write = {'z', 'dz', 'd2z'};
vars_to_write = {z, dz, d2z};
fid = fopen(filename, 'w');
for K = 1 : length(names_to_write)
fprintf(fid, "%s = str2sym('%s');\n", names_to_write{K}, char(vars_to_write{K}));
end
fclose(fid)
ans = 0
type(filename)
z = str2sym('x + y'); dz = str2sym('1'); d2z = str2sym('0');
clearvars x y z dz d2z
test_str2sym();
whos
Name Size Bytes Class Attributes K 1x1 8 double ans 1x1 8 double d2z 1x1 8 sym dz 1x1 8 sym fid 1x1 8 double filename 1x19 38 char names_to_write 1x3 324 cell vars_to_write 1x3 336 cell z 1x1 8 sym
z
z =
dz
dz =
1
d2z
d2z =
0

Paul on 5 Jan 2022
I'm not familiar with this?
un',0
Can you show that in a cellfun command?
In any case, diff() always returns a symbolic expression, doesn't it? (though the doc page does even have an Output section that says so explicitly).
syms x
d1 = diff(3*x)
d1 =
3
d2 = diff(0*x)
d2 =
0
whos
Name Size Bytes Class Attributes d1 1x1 8 sym d2 1x1 8 sym x 1x1 8 sym
Leo Simon on 5 Jan 2022
@Paul 'un',0 means Uniform output false, and you can create a cell array with some elements symbolic and others numeric. You're right about diff! I didn't realize that. The reason I'm having problems is that I'm converting the output from diff to a character string and then writing it to a file. when I run the file and an element of f encounters a symbolic variable, all is well, but if it encounters a 0, it treats it as numeric.
This makes my problem much harder to fix, it seems, I'm afraid.