how to write Derivative code ?

22 vues (au cours des 30 derniers jours)
sagi ash
sagi ash le 5 Oct 2016
Modifié(e) : James Tursa le 31 Juil 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 commentaires
sagi ash
sagi ash le 6 Oct 2016
there is nothing wrong with that
i just need it without that function.
arfa abdouramani hamadou
arfa abdouramani hamadou le 31 Juil 2018
Modifié(e) : James Tursa le 31 Juil 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')

Connectez-vous pour commenter.

Réponses (3)

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

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 5 Oct 2016
Modifié(e) : John D'Errico le 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 commentaire
sagi ash
sagi ash le 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.

Connectez-vous pour commenter.


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

Translated by