MATLAB function and symbolic function give different answers

% Some values for the plot
A = 0.5
B = 2
% MATLAB and symbolic functions generated two ways
f = @(a, b, x) (a .* (x - b).^2) .* ((x - b) >= 0)
fsym = sym(f); % Uncommenting this line hangs Live Script
syms a b x
fsym2(a, b, x) = (a * (x - b)^2) * ((x - b) >= 0); % Uncommenting this line hangs Live Script
% Plot both functions
figure
X = (0:0.001:3*B);
subplot(1, 3, 1)
plot(X, f(A, B, X), '-r')
title('MATLAB function')
subplot(1, 3, 2)
plot(X, subs(subs(subs(fsym, sym('a'), A), sym('b'), B), sym('x'), X), '-b')
title('Function from sym')
subplot(1, 3, 3)
plot(X, fsym2(A, B, X), '-b')
title('Declared symbolic function')
I am trying to get better at using symbolic functions. I am struggling to understand why the two symbolic subplots do not appear identical (other than color) to the subplot generated by the MATLAB function. Can someone explain why, and what I would do to create a "working" symbolic version of f?
As an aside, both lines 7 and 9 hangs the Live Editor if uncommented...does that suggest a bug in what I am doing?

 채택된 답변

David Hill
David Hill 2022년 4월 5일
A = 0.5;
B = 2;
f = @(a, b, x) (a .* (x - b).^2) .* ((x - b) >= 0);
syms a b x;
fsym1 = piecewise((x-b)>=0,(a * (x - b)^2),(x-b)<0,0);
figure
X = (0:0.001:3*B);
subplot(1, 3, 1)
plot(X, f(A, B, X), '-r')
title('MATLAB function')
subplot(1, 3, 2)
fsym=subs(fsym1,[a b],[A B]);
fplot(fsym,[0,3*B]);
title('Function from sym')

댓글 수: 4

Thank you. I am able to make it work symbolically. My question is, why doesn't it work the way I attempted it? Why does the symbolic version produce an equation that gives a different answer?
I am not an expert here, but matlab (symbolic equation) is not liking your logical expression: (x-b)>=0
syms a b x
fsym1 = piecewise((x-b)>=0,(a * (x - b)^2),(x-b)<0,0);%using piecewise solves the problem
fsym2 = a*(x - b)^2*((x - b)>=0);%not sure why these are not the same.
Paul
Paul 2022년 4월 6일
편집: Paul 2022년 4월 6일
The result from fsym2 can be explained as follows. However, there might be something odd going on because the Live editor can't render the result, so I'll copy/paste from my local installation.
syms a z
fsym2 = a*(z>=0);
On the right hand side, 'a' is symbolic varibale and 'z >= 0' is an equation. So the the multiplication by 'a' distributes across the equation as it would in a standard mathematical sense. Recall that multiplying an inequality by a negative number reverses the direction of the inequality. Also, the sign of 'a' is unknown absent any additional assumptions, so multiplying through by 'a' has to account for the cases of a <= 0 and a > = 0. The result is
fsym2 = piecewise(0 <= a, 0 <= a*z, a <= 0, a*z <= 0)
The bottom line is that in Symbolic math 'z >= 0' is a math equation, whereas in base Matab 'z >= 0' is an expression to be evaluated to T/F and then converted to double.
James Akula
James Akula 2022년 4월 20일
편집: James Akula 2022년 4월 21일
I think this explains it. It is weird behavior though...might catagorize it as a bug. Certainly, the fact that piecewise seems to crash the live editor is a problem!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2021b

태그

질문:

2022년 4월 5일

편집:

2022년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by