0

I was trying to learn trigonometry from the beginning. so I can plot a curve by giving these as commands.

x1 = linspace(-2*pi, 2*pi)
y1 = cos(x1)
plot(x1, y1)

But If I try to do this,

x1 = linspace(-2*pi, 2*pi)
y2 = diff(cos(x1))
plot(x1, y1) 

I receive an error stating

error: __plt2vv__: vector lengths must match error: called from     __plt__>__plt2vv__ at line 487 column 5     __plt__>__plt2__ at line 247 column 14     __plt__ at line 112 column 18     plot at line 229 column 10

However, I do get values for y2 = diff(cos(x1)), I can't plot anything because of this error.

Note: I know the differentiation of sin(x) is cos(x) and I can just use cos(x), but can't I do it this way?

Tried diff(cos(90)) I got

ans = \[\](0x0)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    `diff` is one element shorter than its input, because it it the differences `[v(2)-v(1), v(3)-v(2), ...]`. You need to do something like `y2 = [NaN, diff(cos(x1))];` to pad your array to be the same size as `x1`. – Wolfie Mar 22 '22 at 10:42
  • Please read the tag descriptions on [tag:octave] and [tag:matlab]. Do **not** use both tags unless you are explicitly asking about the similarities or differences between the two languages. They are not the same and by using both tags you're unnecessarily constraining potential answerers to validate that their code works in both languages. – Adriaan Mar 22 '22 at 10:47
  • @Wolfie Thank you very much for your answer and that line of code worked perfectly, now I can plot the graph without any problems. PS: It would have been awesome if you had used "Answer the question" since I can't even upvote you. Nonetheless, Thank you! – Prajwal Sagar Mar 22 '22 at 13:41
  • @Adriaan will take care of that from now one – Prajwal Sagar Mar 22 '22 at 13:42
  • In your second code block you switch from y1 to y2. the third line should also be y2. – Nick J Mar 23 '22 at 11:32

1 Answers1

2

diff is one element shorter than its input, because it it the differences [v(2)-v(1), v(3)-v(2), ...].

You need to do something like y2 = [NaN, diff(cos(x1))]; to pad your array to be the same size as x1.

Note that of course this causes an offset, you would need a central differencing method instead of a forward difference method (like diff) to keep things well-aligned.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • This technique worked for single differentiation but not for double. x1 = linspace(-2*pi, 2* pi); y1 = [NaN, (diff(sin(x1),2))]; length(x1) length(y1) plot(x1,y1,'r-') can you please tell me why it didn't work here? – Prajwal Sagar Mar 24 '22 at 04:54
  • Because you're taking the difference twice it loses two elements, add a NaN to the end too – Wolfie Mar 24 '22 at 06:44
  • thank you very much. It worked. But isn't there a better way? PS: I tried doing triple differentiation with three NaN, it worked too, but isn't there a better way? – Prajwal Sagar Mar 24 '22 at 07:01
  • Yes, use a higher order central differencing method as I linked to, you are using only the most basic of numeric differentiation methods but it's a whole subfield of maths – Wolfie Mar 24 '22 at 08:28