Ho do I use symbolic expressions to define a new function?
1 view (last 30 days)
Show older comments
Ronald Koelpin
on 11 Jun 2021
Commented: Star Strider
on 11 Jun 2021
Hello everyone,
i have a problem when trying define a new function and integrate it. Say I have a continously differentiable function f:[a,b] -> R and want to calculate its arc length. I would define define a function e.g.
f = @(t) cos(t)
and then, attempting to use the formula L = integral_a^b(sqrt(1+f'(t)^2)dt i would use
df = diff(f, t)
to determine its (symbolic) differential and then define a new function
g = @(t) sqrt(1+df.^2)
and integrate g
integral(g, a, b)
At least thats what i want to do, because it doesnt work, because something is wrong with/MATLAB doesnt unterstand the way i define g. It does not substitute in the symbolic expression previously derived for df and hence throws errors at me when trying to calculate the integral. The problem would also arise if i simply tried to calculate the integral of the derivate. Hence my question: How do i use diff(f, t) to define a new function (which i can the integrate for example).
0 Comments
Accepted Answer
Star Strider
on 11 Jun 2021
See if this slight revision of the posted code does what you want:
f = @(t) cos(t);
% L = integral_a^b(sqrt(1+f'(t)^2)dt
df = @(f,t) (f(t+1E-8)-f(t-1E-8))/2E-8; % Numeric Approximation
g = @(t) sqrt(1+df(f,t).^2);
a = 0;
b = 2*pi;
G = integral(g, a, b)
.
2 Comments
Star Strider
on 11 Jun 2021
As always, my pleasure!
The code you posted uses anonymous functions, not symbolic functions, so I stayed with that. The Symbolic Math Toolbox is certainly appropriate here as well.
.
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox 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!