Symbolic Piecewise Addition Issue
Show older comments
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
More Answers (1)
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)
4 Comments
Austin
on 16 Oct 2024
syms A B C L real
assumeAlso(L, 'positive');
testfunc = singz(-A, 0, 0) + singz(-B, 0, L)
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
Walter Roberson
on 16 Oct 2024
Edited: Walter Roberson
on 16 Oct 2024
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))
testfunc + singz(-C, 0, 2*L);
disp(char(ans))
testfunc2 = singz(-C, 0, 2*L);
disp(char(testfunc2))
testfunc + testfunc2;
disp(char(ans))
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
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!