How to take a symbolic derivative, and then change that symbolic variable to one assigned with linspace
3 views (last 30 days)
Show older comments
I've been working on a problem, and it needs four derivatives. The equation itself is very large.
It looks much like this
y = const*(x^4 - 2*L*x^3 + x^2) ----> Where const is a collection of constants.
To take the first derivative, I did the following.
syms x, const, L y = const*(x^4 - 2*L*x^3 + x^2)
dy = diff(y,x); d2y = diff(dy,x)*const; d3y = diff(dy2,x)*const;
Following this, I then assigned values to the constants, and x = linspace(0:L), where L is already assigned -- however, when I try to use this in a for-loop to see its graph, it simply prints out the symbolic equation. I could go back and manually place the assigned variables in there, but that would completely ruin the purpose of this program.
0 Comments
Answers (1)
Star Strider
on 19 Jan 2017
I’m not sure what you’re doing. If you want to do the plotting in the Symbolic Math Toolbox, use the fplot (or ezplot) functions:
figure(1)
fplot(dy, [0 L])
hold on
fplot(d2y, [0 L])
fplot(d3y, [0 L])
hold off
grid
Everything except ‘x’ has to be defined numerically before the fplot calls.
2 Comments
Star Strider
on 19 Jan 2017
To do that, use the matlabFunction function.
For example:
syms x const L
const = 3;
L = 10;
y = const*(x^4 - 2*L*x^3 + x^2)
dy = diff(y,x);
d2y = diff(dy,x)*const;
d3y = diff(d2y,x)*const;
y_fcn = matlabFunction(y)
dy_fcn = matlabFunction(dy)
d2y_fcn = matlabFunction(d2y)
d3y_fcn = matlabFunction(d3y)
x = linspace(0, L);
figure(1)
plot(x, y_fcn(x))
hold on
plot(x, dy_fcn(x))
plot(x, d2y_fcn(x))
plot(x, d3y_fcn(x))
hold off
grid
See Also
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!