how to write Derivative code ?

19 Ansichten (letzte 30 Tage)
sagi ash
sagi ash am 5 Okt. 2016
Bearbeitet: James Tursa am 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 Kommentare
sagi ash
sagi ash am 6 Okt. 2016
there is nothing wrong with that
i just need it without that function.
arfa abdouramani hamadou
arfa abdouramani hamadou am 31 Jul. 2018
Bearbeitet: James Tursa am 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')

Melden Sie sich an, um zu kommentieren.

Antworten (3)

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

Melden Sie sich an, um zu kommentieren.


John D'Errico
John D'Errico am 5 Okt. 2016
Bearbeitet: John D'Errico am 5 Okt. 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 Kommentar
sagi ash
sagi ash am 5 Okt. 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.

Melden Sie sich an, um zu kommentieren.


James Tursa
James Tursa am 5 Okt. 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