how to write Derivative code ?
19 views (last 30 days)
Show older comments
Hi all.
Can anyone share a Derivative code with me? (not the "diff" function) I need a code that will do derivative to a mathematical function that the user will enter.
I want to use the GUI menu, so the mathematical function will enter in that menu, and i will show the graph of the function and the derivative of the function. and later will enter also a value, to the function.
Tthank you.
3 Comments
arfa abdouramani hamadou
on 31 Jul 2018
Edited: James Tursa
on 31 Jul 2018
clear all;
close all;
st = 0;
inc=0.001;
en =1;
t=st:inc:en;
y= sin(2*pi*2*t);
figure(1)
plot(t,y)
title('Actual graph')
x=zeros(1,(length(i)-1));
for i=1:(length(t)-1)
x(1,i)=(y(1,i+1)-y(1,i))/inc
end
t1=st:inc:(en-inc);
figure(2)
plot(t1,x)
title('After differentiation')
Answers (3)
John D'Errico
on 5 Oct 2016
Edited: John D'Errico
on 5 Oct 2016
Without writing the symbolic toolbox completely from scratch, you cannot solve this problem. If your goal is to allow any general function to be entered, and you want to form the symbolic derivative, without using diff, then you need to write diff, FROM SCRATCH, to work on any function you may see entered.
For example, if the user can enter x^2*sin(x), then you need code that will know how to differentiate it. If you are unwilling to use the symbolic toolbox function diff, then you need to supply code that is intelligent enough to differentiate a possibly complicated function. Not that easy to write, but surely doable.
I'm sorry, but you can want anything that you wish to have, but magic does not just happen.
If you are willing to restrict the set of possible functions that may be entered, for example, to polynomials, then you could in theory use my sympoly toolbox. Even that would take some effort to get working, because sympoly does not work on any general string input.
Another alternative is to do a numerical differentiation. But that won't give you a form you can display, just let you do a plot. The gradient function can help you with the differentiation, or you can do it yourself suing a simple finite difference approximation.
James Tursa
on 5 Oct 2016
If you can use anything except diff(), and you only want to allow simple polynomials, then use polyder() for the derivative (along with polyval() for evaluating the polynomials).
doc polyval
doc polyder
0 Comments
See Also
Categories
Find more on Calculus 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!