0

I learnt how to differentiate on octave through this. But I now want to plot graph of the same function on octave. I am not able to do so. I want to know what the right command for plotting the 'ffd' equation in the code is.

`f = @(x) x.^2 + 3*x - 1 + 5*x.*sin(x);
   pkg load symbolic
   syms x;
   ff = f(x);
   ffd = diff(ff, x)  `

What I have tried so far: I have tried adding these lines of code to the end and it didn't work. The graph was empty and I got an error.

 real

ffd = (sym) 5⋅x⋅cos(x) + 2⋅x + 5⋅sin(x) + 3
error: __go_line__: invalid value for array property "ydata", unable to create graphics handle
error: called from
__plt__\>__plt2vs__ at line 466 column 15
__plt__\>__plt2__ at line 245 column 14
__plt__ at line 112 column 18
plot at line 229 column 10
real at line 10 column 1\`

I was expecting it to plot the graph of (sym) 5⋅x⋅cos(x) + 2⋅x + 5⋅sin(x) + 3 i.e., ffd, but it didn't work

  • Your 'this' link just contains a link to stackoverflow.com homepage, is that intended? Do you necessarily must use the symbolic package? [Related](https://stackoverflow.com/questions/71570616/how-do-i-plot-the-graph-for-differentiation-of-a-function-on-octave/71573275#71573275). – Arc Mar 26 '22 at 15:16
  • no I am afraid not. And I asked the [related](https://stackoverflow.com/questions/71570616/how-do-i-plot-the-graph-for-differentiation-of-a-function-on-octave/71573275#71573275) question, but this is different. And that doesn't work properly. Did edit 'this' – Prajwal Sagar Mar 26 '22 at 15:41
  • Oh, sorry, you *did* ask the question! :) But then, you stated "it worked", and accepted the answer? Do you care to explain why it didn't work properly? – Arc Mar 26 '22 at 17:12
  • As to the plot in this question, I don't think you can plot `sym`s directly with `plot`. Try converting them first with `double`, before `plot` and after `diff`. Also, you might want to try the `ezplot` from [symbolic](https://octave.sourceforge.io/symbolic/function/@sym/ezplot.html). – Arc Mar 26 '22 at 17:14
  • can you please tell me where to learn octave properly? I just took a 90 min course. Maybe If I learned like everyone else, I won't have these stupid problems. – Prajwal Sagar Mar 27 '22 at 06:13
  • Your question is not an introductory level. I don't know any good sources except for Octave's manual, and the source code as a last resort. You are doing fine, keep coding, keep asking, and keep learning. – Arc Mar 27 '22 at 11:01

1 Answers1

1

In order to draw graphs of the differentiated functions we need to have code similar to this and in this case I am using pkg symbolic

  pkg load symbolic
  syms x;

  x = sym('x');
  y = @(x) x.^3;
  yy = y(x);
  ffd = diff(diff(yy,x));
  ffe = diff(yy,x);
  ez1=ezplot(y,[-10,10])
  hold on
  ez2=ezplot(ffe,[-10,10])
  hold on 
  ez3=ezplot(ffd,[-10,10])
  

note: hold on function is used to draw more than one graphs on the same screen. However, if you modify the program and run it again it doesn't clear the screen of the previous graphs, if anyone knows how to do this, please leave a comment.

  • 1
    +1 for the endurance, and self-answer. To clear the existing plot windows, use command `clf`, but this leaves plot window open. To close the active plot use `close`, and to close all open plots use `close all` (see [here](https://octave.org/doc/v6.4.0/Manipulation-of-Plot-Windows.html) for how to manipulate plot windows). – Arc Mar 28 '22 at 13:27