How to declare a function using the diff-command

2 vues (au cours des 30 derniers jours)
Luki
Luki le 28 Déc 2016
Modifié(e) : KSSV le 28 Déc 2016
In Matlab I am given a function whose declaration starts with this code:
function [v1, v2, v3, v4, v5, v6] = calculate_v(P1, P2, angle, difference)
dP1=P1(2) - P1(1);
dangledP1=diff(angle)/dP1;
dP2dP1=diff(P2)/dP1;
But I don't understand it. The input variables seem to be vectors. I know the diff-command returns a vector whose entries are the difference of adjacent entries. But what is happening in the first line:
dP1 = P1(2) - P1(1) ?
I mean does P1 only have two entries? I am not given any further information about the variables either.

Réponse acceptée

KSSV
KSSV le 28 Déc 2016
You have to enter at least two entries for P1, dP1=P1(2) - P1(1) is like calculating the difference/ step. If you enter more then two elements also, your code will not take the other values into consideration.
  2 commentaires
Luki
Luki le 28 Déc 2016
Ok. So the difference to diff(P1) is that P1(2)-P1(1) would only equal the first entry of diff(P1)
KSSV
KSSV le 28 Déc 2016
Modifié(e) : KSSV le 28 Déc 2016
No diff(P1) calculates the difference P(i)-P(i+1)..._diff_ calculates the difference among the successive elements and it's size would be one less then the input vector. In your code there is no diff(P1).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by