graphoctavedifferentiation

Plotting a graph for the double differentiation of a function on OCTAVE


trying to plot the function for the double differentiation of a function on octave with no luck. I am trying to plot the double derivative of sin(x) in this case.

I know how to find the derivative of sin(x) on octave

x1 = linspace(-2*pi, 2* pi);
y1 = [NaN, diff(sin(x1))];
plot(x1,y1,'r-')

how can I similarly plot the graph for double differentiation of sin(x1)?

what I have tried so far:

x1 = linspace(-2pi, 2 pi); y1 = [NaN, (diff(sin(x1),2))]; length(x1) length(y1)
plot(x1,y1,'r-')

The length of x1 and y1 is different, but usually including NaN solved it for single differentiation. I can't get it right for double differentiation however.

ERROR:

>> tig

ans = 100
ans = 99
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
    tig at line 5 column 1

PS: Also, I haven't learnt Octave officially and I have just took a short course on Youtube, would appreciate if someone gave me links for learning it through videos and pdfs


Solution

  • The command would be

    x1 = linspace(-2*pi, 2* pi);
    y1 = [NaN, (diff(sin(x1),2)), NaN];
    length(x1)
    length(y1)
    plot(x1,y1,'r-')
    

    Since we are double differentiating, we would have two vacancies, so we have to insert two NaN s

    Source: comments on https://stackoverflow.com/a/71573275/18501521