Symbolic Piecewise Addition Issue

Hello, I'm working on a script that calculates the deflection of beams, and thus I'm using singularity functions, essentially piecewise polynomials that 'switch on' at a certain point. There is a very odd issue I'm finding when I add two together. Here is the function I call to create them in the first place:
function func = singz(coeff, order, center)
%%% Creates a singularity function (symbolic)
syms f(z)
f(z) = piecewise(z < center, 0, z >= center, coeff*(z - center)^order);
end
I've attached below the variables I'm about to use. For reference, A, B, and C are real symbolic variables, and L is a positive symbolic variable.
Here are the two initial functions:
>> testfunc
testfunc(z) =
piecewise(z < 0, 0, z < L, -A, symtrue, - A - B)
and
>> singz(-testmag, 0, testloc)
ans =
piecewise(z < 2*L, 0, symtrue, -C)
The first is actually a composite of two singularity functions, but their addition had no issue, for some reason.
Here's what happens when I add the two above:
>> testfunc + singz(-testmag, 0, testloc)
ans(z) =
piecewise(z < 0, 0, z < L & in(z, 'real'), -A, z < L, - A - C, z < 2*L, - A - B, symtrue, - A - B - C)
For some reason, I get an 'in(z,....' But the primary issue is this 'z < L, - A - C' term. It makes absolutely no sense in my mind. I am aware that I can assume z to be real and simplify. The result is:
> simplify(Sy)
ans(z) =
piecewise(z < 0, 0, z < L, -A, z < L, - A - C, z < 2*L, - A - B, symtrue, - A - B - C)
I know that the 'correct' '-A' term is first and will thus be evaluated, but the other term shouldn't be there in the first place. I don't trust that this order will magically work out every time that I want to run this program.
Thank you in advance; any help would be greatly appreciated.

 Accepted Answer

Austin
Austin on 17 Oct 2024
I figured it out. It was the 'singz' function:
function func = singz(coeff, order, center)
%%% Creates a singularity function (symbolic)
syms z real
func(z) = piecewise(z < center, 0, z >= center, coeff*(z - center)^order);
end
Apparently I needed to declare z within the function. I'm guessing this helped MATLAB 'see' the z's as the same dependent variable? Very odd issue, I'm sure returning a symbolic function gives issues in general.
Thank you for the help!

More Answers (1)

Paul
Paul on 16 Oct 2024
Edited: Paul on 16 Oct 2024
Seems to work here.
How does the singz function work insofar as the output argument, func, is not defined inside the function.
syms z A B C L real
assumeAlso(L,'positive');
testfunc(z) = piecewise(z < 0, 0, z < L, -A, symtrue, - A - B)
fsingz(z) = piecewise(z < 2*L, 0, symtrue, -C)
testfunc(z) + fsingz(z)
% in case symbolic output not rendering, long standing issue on Answers
char(ans)
ans = 'piecewise(z < 0, 0, z < L, -A, z < 2*L, - A - B, symtrue, - A - B - C)'

4 Comments

Apologies on the 'func' part, I think that I deleted that line while trying to fix something. I added back in 'func = f'. Regardless, I double-checked just now and the issue still persists.
>> syms A B C L real
>> assumeAlso(L, 'positive');
>> testfunc = singz(-A, 0, 0) + singz(-B, 0, L)
testfunc =
piecewise(z < 0, 0, z < L, -A, symtrue, - A - B)
>> testfunc + singz(-C, 0, 2*L)
ans =
piecewise(z < 0, 0, z < L & in(z, 'real'), -A, z < L, - A - C, z < 2*L, - A - B, symtrue, - A - B - C)
It happens, even if I separate them out:
>> testfunc2 = singz(-C, 0, 2*L)
testfunc2 =
piecewise(z < 2*L, 0, symtrue, -C)
>> testfunc + testfunc2
ans =
piecewise(z < 0, 0, z < L & in(z, 'real'), -A, z < L, - A - C, z < 2*L, - A - B, symtrue, - A - B - C)
syms A B C L real
assumeAlso(L, 'positive');
testfunc = singz(-A, 0, 0) + singz(-B, 0, L)
Output argument "func" (and possibly others) not assigned a value in the execution with "solution>singz" function.
testfunc + singz(-C, 0, 2*L)
testfunc2 = singz(-C, 0, 2*L)
testfunc + testfunc2
function func = singz(coeff, order, center)
%%% Creates a singularity function (symbolic)
syms f(z)
f(z) = piecewise(z < center, 0, z >= center, coeff*(z - center)^order);
end
R2024b does not have the in(z,'real')
syms A B C L real
assumeAlso(L, 'positive');
testfunc = singz(-A, 0, 0) + singz(-B, 0, L);
disp(char(testfunc))
piecewise(z < 0, 0, z < L & 0 <= z, -A, L <= z, - A - B)
testfunc + singz(-C, 0, 2*L);
disp(char(ans))
piecewise(z < 0, 0, z < L & 0 <= z, -A, L <= z & z < 2*L, - A - B, 2*L <= z, - A - B - C)
testfunc2 = singz(-C, 0, 2*L);
disp(char(testfunc2))
piecewise(z < 2*L, 0, 2*L <= z, -C)
testfunc + testfunc2;
disp(char(ans))
piecewise(z < 0, 0, z < L & 0 <= z, -A, L <= z & z < 2*L, - A - B, 2*L <= z, - A - B - C)
function func = singz(coeff, order, center)
%%% Creates a singularity function (symbolic)
syms f(z)
func(z) = piecewise(z < center, 0, z >= center, coeff*(z - center)^order);
end
Austin
Austin on 17 Oct 2024
Edited: Austin on 17 Oct 2024
I just updated to 2024b and it still didn't work. I tried what you did, but the results are slightly different for the domains:
>> syms A B C L real
assumeAlso(L, 'positive');
testfunc = singz(-A, 0, 0) + singz(-B, 0, L);
disp(char(testfunc))
piecewise(z < 0, 0, z < L, -A, symtrue, - A - B)
It defines -A at z<L, not z in [0,L]. Not sure what would cause this.
Maybe it isn't recognizing that they are the same 'z'?

Sign in to comment.

Products

Release

R2024a

Asked:

on 16 Oct 2024

Answered:

on 17 Oct 2024

Community Treasure Hunt

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

Start Hunting!