Passing a vector (array) into a differentiated function

2 views (last 30 days)
I'm new to Matlab, and I'm having a bit of a problem with a bit of my code, which I present below:
Essentially, I am trying to pass array values (which here is my domain x) into my functions, and the functions respective derivatives.
%I try to do this in the following way:
f = @(x) x^2 -1; %functions and its derivatives we are handling
Df1 = @(x) diff(sym(f))
Df2 = @(x) diff(sym(Df1))
x = (0:0.05:1);% our bounds, with intervals
f1 = arrayfun(f,x);
diff1 = arrayfun(Df1,x);
diff2 = arrayfun(Df2,x);
When doing this though, I encounter the following error code:
Error using arrayfun
First input must be a function handle.
Where have I gone wrong/what am I not understanding, as it seems pretty simple.

Accepted Answer

David Hill
David Hill on 9 Dec 2021
syms x;
f = x^2 -1;
Df1 = diff((f));
Df2 = diff((Df1));
y = (0:0.05:1);
F= double(subs(f,x,y));
DF1= double(subs(Df1,x,y));
DF2= double(subs(Df2,x,y));

More Answers (0)




Community Treasure Hunt

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

Start Hunting!