Error: Conversion to double from function_handle is not possible. Error in homework4 (line 18) deltaf(k) = function_fd(f,2,h(k));
Show older comments
Function File:
function [df] = function_fd(f, x, h)
df = @(f,x,h) ((f(x+h)-f(x))/h);
Script File:
f = @(x) (cos(exp(1)))^x;
g = @(x) ((f(x+h)-f(x))/h);
h = zeros(1,31);
d=0;
for k=1:31
h(k) = 10^d;
d=d-0.5;
end
deltaf = zeros(1,31);
err = zeros(1,31);
deriv = function_fd(f,2,0.1);
for k=1:31
deltaf(k) = function_fd(f,2,h(k));
end
for t=1:31
err(t) = function_fd(f,2,h(t));
end
loglog(err,h,-s);
grid on
This is the assignment:
Create a new MATLAB function file that implements a function with the following signature: function [df] = forward_difference(f, x, h)
The inputs to this function are:
• f – this is a MATLAB function handle representing the function we want to differentiate. • x – this is a real-valued input where we want to compute the derivative. • h – this is a real-valued perturbation that we will use in approximating the derivative.
As you learned in Calculus the derivative of a function f at a point x is defined as lim f(x+h)−f(x), h→0 h
your MATLAB function does not actually take the limit, instead it should compute the forward finite-difference approximation δh+f(x) = f(x + h) − f(x), h for a fixed value of the increment h.
Note: I realize that this could be done in an anonymous function; however, I require that for this homework you do this in a separate function file.
Create a new MATLAB script with the name homework4.m that performs the following tasks: 1. Create a MATLAB anonymous function for the mathematical function f(x) = cos(ex). 2. Create a MATLAB anonymous function for the mathematical function f′(x) [you must take the derivative by hand]. 3. Create an array of values h = 100 10−0.5 10−1 . . . 10−15 4. Initialize two arrays deltaf and err, both row vectors with the same shape as h. 5. Using a MATLAB for loop, compute δh+ f(2) for each value hk in the array h; store these k results inside the corresponding indices of deltaf. 6. Either in a separate for loop or in the one above, compute δh+ f(2)−f′(2) for each value k hk in the array h, and store these results inside the corresponding indices of err. 7. Create a MATLAB loglog plot that displays err vs h. Ensure that the axes and title are labeled appropriately.
Suppress the output from all of the preceding commands. The only thing that should appear when running this script is the plot.
Answers (1)
James Tursa
on 23 Feb 2018
Edited: James Tursa
on 23 Feb 2018
deltaf is a double vector, but function_fd returns a function handle. You can't convert a function handle to a double, hence the error message.
Maybe just have your function_fd return a double calculation and work with that. E.g.,
df = (f(x+h)-f(x))/h;
3 Comments
Peyton Baker
on 23 Feb 2018
James Tursa
on 23 Feb 2018
By using the line that I posted. E.g., the function file function_fd.m will contain these two code lines:
function [df] = function_fd(f, x, h)
df = (f(x+h)-f(x))/h;
Peyton Baker
on 23 Feb 2018
Categories
Find more on Matrices and Arrays 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!