Write a function function [fp] = f uncderiv (func, x, h) that takes a function f
ID: 3793276 • Letter: W
Question
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^-4Explanation / Answer
% 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
>>
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.