how get second derivative of a function with one variable? I keep getting not enough input arguments?

Can someone help with this error? it seems I did everything according to tutorials
I was asked to use MATLAB function to calculate second derivative. I created the function but I cannot export the secnd derivative from it. i also tried to give it some value and it works fine. Just diff() does not work

Answers (1)

"it seems I did everything according to tutorials"
Sort of: you defined a function that works on numeric data, but then you have attempted to use this with symbolic differentiation (i.e. DIFF from the symbolic toolbox). That will not work. If you want to perform symbolic differentiation, then you will need to define a symbolic formula. See the examples here:
syms t
f = exp(5*t) + t^2
f = 
d = diff(f,t) % default = 1st derivative
d = 
d = diff(f,t,2) % 2nd derivative
d = 

6 Comments

Thank you for explaining. But the question is asking me to create a MATLAB funtion and solve it using function.
syms t
ft = f(t)
y = 
ft = 
d2f = diff(ft, t, t)
d2f = 
function [y] = f(t)
y= exp(5*t) + t^2
end
so, does this mean that this question is wrong?
  1. Create a MATLAB function with the name f for 𝑓(𝑡) = 𝑦 = 𝑒5𝑡 + 𝑡2 function and then use the MATLAB function diff() to obtain:
a- The second derivative of the function.
in another question I already used:
syms t
f = exp(5*t) + t^2
d = diff(f,t)
method
syms t
d2f = diff(@f, t, t)
Error using sym>funchandle2ref
Only anonymous functions and functions without arguments can be converted to sym.

Error in sym>tomupad (line 1614)
x = funchandle2ref(x);

Error in sym (line 397)
S.s = tomupad(x);

Error in sym/privResolveArgs (line 1184)
argout{k} = sym(arg);

Error in sym/diff (line 29)
args = privResolveArgs(S, invars{:});
function [y] = f(t)
y= exp(5*t) + t^2
end
syms t
d2f = diff(f, t, t)
Not enough input arguments.

Error in solution>f (line 4)
y= exp(5*t) + t^2
function [y] = f(t)
y= exp(5*t) + t^2
end
What those tell you is that you cannot use diff to find the derivative of a MATLAB function -- not unless you pass a symbolic variable into the function so that diff() is operating on a symbolic expression.
You can use diff() to find the derivative of an anonymous function or of a symbolic function
syms t
f1 = @(t) exp(5*t) + t^2
f1 = function_handle with value:
@(t)exp(5*t)+t^2
diff(f1, t, t)
ans = 
f2(t) = exp(5*t) + t^2
f2(t) = 
diff(f2, t, t)
ans(t) = 

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 23 Feb 2023

Commented:

on 23 Feb 2023

Community Treasure Hunt

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

Start Hunting!