Derivative without diff function

11 visualizzazioni (ultimi 30 giorni)
Renzo Luna
Renzo Luna il 8 Gen 2021
Commentato: Walter Roberson il 15 Gen 2022
Hi everyone,
So I have this problem, my university is giving us an assignment where we have to find the derivative without using the diff function. So we have an array of positions(y) and an array of time(t) and we have to find the derivative in order to find the speed.
I know I have to use this function (f(x + h) - f(x - h)) / (2*h), so I came up with this code:
fun = @(y,t) y/t;
dx = t(2)-t(1);
x = y(1);
df = (fun(x + dx) - fun(x - dx)) / (2*dx);
But it is giving me errors, specifically "Not enough input arguments. Error in @(x,t)x/t". What is the error? How can I calculate the derivative in order to find the speed?
Thanks in advance.

Risposte (2)

KSSV
KSSV il 8 Gen 2021
You should proceed something like this:
fun = @(y,t) y/t;
t = linspace(0,60,500) ;
y = sin(t) ;
df = zeros(length(t)-1) ;
for i = 1:length(t)-1
dx = t(i+1)-t(i);
x = y(i);
df(i) = (fun(x + dx,t(i)) - fun(x - dx,t(i))) / (2*dx);
end
  1 Commento
SAYGIN ileri
SAYGIN ileri il 15 Gen 2022
Would you mind writing an example for derivation for chain rule, please..

Accedi per commentare.


Jan
Jan il 8 Gen 2021
"Not enough input arguments. Error in @(x,t)x/t"
You have defined fun to use 2 input arguments:
fun = @(y,t) y/t;
But in fun(x + dx) you provide 1 input only.
"So we have an array of positions(y) and an array of time(t)"
This means, that you cannot determine f(x+h), because all you have is the numerical values of y and t. Then:
dy = (y(3:end) - y(1:end - 2)) ./ (t(3:end) - t(1:end-2))

Community Treasure Hunt

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

Start Hunting!

Translated by