MATLAB function and symbolic function give different answers
Show older comments
% 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?
Accepted Answer
More Answers (0)
Categories
Find more on Assumptions 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!