Please provide working code that can be easily understood. Thanks! Derivative of
ID: 3535681 • Letter: P
Question
Please provide working code that can be easily understood.
Thanks!
Explanation / Answer
Save the following as derivative.m
function dy = derivative ( nsamp1,nsamp,dx ) % taking inputs of nsamp(i+1) and nsamp(i) at the same time
if(dx)>0
dy= (nsamp1 - nsamp)/(dx); % the given foemula for calculating derivative at a point
end
end
save this with any other file name in the same directory
clear all
clc
dx=0.05
x=0:dx:5 % 101 samples to obtain 100 derivatives
nsamp=sin(x) % sample values of sin
for i=1:100
dy(i)=derivative(nsamp(i+1) ,nsamp(i) ,dx); % derivative through formula
end
disp(dy)
dyo=cos(x) % original derivative
for i=1:100
error(i)=((dyo(i)-dy(i))/dyo(i))*100 ;
end
disp('Error is :') % Error in percent
disp( error)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.