Write a function function fp f uncderiv func x h that takes
Write a function function [fp] = f uncderiv (func, x, h) that takes a function func and returns the approximate derivative fp at given loeation(s) x using the finite difference formula shown below, where h is a small number. 1^st-order finite difference approximation: f\'(x) 1/2h[f(x + h) - f(x - h)] Test your function with the following input (define w(t), y(x) as anonymous functions in the workspace): (a) w(t) = sin (20 middot t), t = 10, h= 10^-2 (b) w(t) = sin (20 middot t), t = 10, h = 10^-4 (c) y(x) = x^3, x = [-2 - 1 0 1 2], h = 10^-4
Solution
% THis function accept 3 parameters func=definition of function
% x= given location
% h= smallest value
function[fp]= funcderiv(func,x,h)
for i=1:numel(x) % For all value of the x
fp(i)=(1/2.*h).*(func(x(i)+h)-func(x(i)-h)); % Function definition
end
end
%Output:-
>> w=@(t)sin(20*t)
w =
@(t)sin(20*t)
>> y=@(x)(x^3)
y =
@(x)(x^3)
>>
funcderiv(w,10,0.01)
ans =
9.6789
>>
funcderiv(w,10,0.0001)
ans =
9.7437
>>
funcderiv(w,[-2 -1 0 1 2],0.0001)
ans =
-13.3388 8.1616 20.0000 8.1616 -13.3388
>>
![Write a function function [fp] = f uncderiv (func, x, h) that takes a function func and returns the approximate derivative fp at given loeation(s) x using the Write a function function [fp] = f uncderiv (func, x, h) that takes a function func and returns the approximate derivative fp at given loeation(s) x using the](/WebImages/38/write-a-function-function-fp-f-uncderiv-func-x-h-that-takes-1115308-1761592290-0.webp)