Ho do I use symbolic expressions to define a new function?

1 view (last 30 days)
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).

Accepted Answer

Star Strider
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)
G = 7.6404
.
  2 Comments
Ronald Koelpin
Ronald Koelpin on 11 Jun 2021
Edited: Ronald Koelpin on 11 Jun 2021
That does work, thank you. I also found another way:
f = cos(t);
df = diff(matlabFunction(f, 'vars', t), t);
g = matlabFunction(sqrt(1+df.^2), 'vars', t);
ArcLen = integral(g, 0, 2*pi)
Star Strider
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.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!