How Can a sym Vector have Elements that are Vectors Themselves?

I ran the following code on 2024a
syms x c1 c2 real
m1c(x) = c2 + c1*x + heaviside(x - 4)*(3*x - 2)*(x - 4);
This line causes an error on 2024b and can't run here
try
s = simplify(diff(m1c(x),x),'All',true)
catch ME
ME.message
end
ans = 'Invalid number of arguments.'
so I've uploaded the 2024a result.
load('sfile.mat')
At first glance, the result seems pretty reasonable
s
s = 
but those first two elements do look a bit odd.
As expected, s is a sym and is a 3 x 1 vector
class(s)
ans = 'sym'
size(s)
ans = 1×2
3 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But closer inspection of s reveals that its first two elements are themselves 1 x 2 vectors and the third is a scalar (which would show clearly in the command window, but since Answers uses LiveScript I use char here)
char(s)
ans = '[[c1 - 14*heaviside(x - 4) + 6*x*heaviside(x - 4), c1 - 14*heaviside(x - 4) + 6*x*heaviside(x - 4)]; [c1 - 14*heaviside(x - 4) + 6*x*heaviside(x - 4), c1 + 3*heaviside(x - 4)*(x - 4) + heaviside(x - 4)*(3*x - 2)]; c1 + 3*heaviside(x - 4)*(x - 4) + heaviside(x - 4)*(3*x - 2) + dirac(x - 4)*(3*x - 2)*(x - 4)]'
It looks like the first and second elements of the first element of s are the subexpression and expression respectively. Same for the second element of s.
We can verify the dimensions of each element
arrayfun(@(s) size(s),s,'uni',false)
ans = 3x1 cell array
{[1 2]} {[1 2]} {[1 1]}
So sym arrays seem more like cell arrays from this perspective, even though sym arrays use paren indexing and not curly brace indexing.
An element can be extracted from s
s1 = s(1)
s1 = 
class(s1)
ans = 'sym'
and we can index into that
s1(1)
ans = 
and operate on the result
s1(2)*2
ans = 
But we can't perform a basic operation on s1 itself
s1*2
Error using * (line 362)
Invalid return value '[2*c1 - 28*heaviside(x - 4) + 12*x*heaviside(x - 4), 2*c1 - 28*heaviside(x - ...'. It must be of type 'Type::MATLABOutput'.
Any idea what's going on here?
Also, any idea why @doc:simplify returns s in this form from the outset? I saw nothing on the doc page that indicated that this behavior is expected.

 Accepted Answer

I think it is a bug that simplify() returns this form.
I suspect that the result of the simplify() is a piecewise() construction, and what is being exposed is the children() of the piecewise construction.

1 Comment

What is happening is that the Symbolic Engine is returning an internal MuPAD data structure. Inside MuPAD, there is no problem forming nested components of vectors -- [[a,b],c,[d,[e,f],g]] is a valid internal data structure.

Sign in to comment.

More Answers (0)

Products

Release

R2024a

Asked:

on 14 Feb 2025

Commented:

on 19 Feb 2025

Community Treasure Hunt

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

Start Hunting!