how to write Derivative code ?

조회 수: 23 (최근 30일)
sagi ash
sagi ash 2016년 10월 5일
편집: James Tursa 2018년 7월 31일
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
sagi ash
sagi ash 2016년 10월 6일
there is nothing wrong with that
i just need it without that function.
arfa abdouramani hamadou
arfa abdouramani hamadou 2018년 7월 31일
편집: James Tursa 2018년 7월 31일
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')

댓글을 달려면 로그인하십시오.

답변 (3개)

KSSV
KSSV 2016년 10월 5일
How about symbolic method?
syms x
f = sin(x) ; % function
diff(f) % derivative
  댓글 수: 1
sagi ash
sagi ash 2016년 10월 5일
thank you
but, i wrote in the question. not to use the "diff" function.

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2016년 10월 5일
편집: John D'Errico 2016년 10월 5일
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
sagi ash
sagi ash 2016년 10월 5일
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.

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2016년 10월 5일
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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by