how to write Derivative code ?

19 views (last 30 days)
sagi ash
sagi ash on 5 Oct 2016
Edited: James Tursa on 31 Jul 2018
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
sagi ash
sagi ash on 6 Oct 2016
there is nothing wrong with that
i just need it without that function.
arfa abdouramani hamadou
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')

Sign in to comment.

Answers (3)

KSSV
KSSV on 5 Oct 2016
How about symbolic method?
syms x
f = sin(x) ; % function
diff(f) % derivative
  1 Comment
sagi ash
sagi ash on 5 Oct 2016
thank you
but, i wrote in the question. not to use the "diff" function.

Sign in to comment.


John D'Errico
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.
  1 Comment
sagi ash
sagi ash on 5 Oct 2016
italic
Yes, I need a code for simple mathmatical function.(like polynomials, not trigonometry). and can use any code or function available but not the "diff" function.
i dont have much experience with matlab, so if there is any code for that anywhere or maybe it is not so much effot to write, i would like to get this for some analysis i need to do. If there any possiable way to help i will appreciate it.

Sign in to comment.


James Tursa
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

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!