octaveoctave-gui

How do I plot this simple thing correctly in Octave?


I am a student trying to learn how to use Octave and I need help with plotting this thing right here the function I want to plot

The code looks like this:

x2 = 0:0.1:50;
y2 = power((1+(2.*x2/(exp(0.5.*x2)+(x2.^2)))), 0.5);
plot(x2, y2, '-b');

It feels like it should have plotted without problems but the plot in the figure appears to be completely empty, and I cannot wrap my head around why this might happen. Would love to know what am I doing wrong


Solution

  • If you inspect the value of y2 (simply type y2 and press enter) then you find that y2 is a single number rather than a vector.

    To find out out why y2 is a single number we type in the calculation power((1+(2.*x2/(exp(0.5.*x2)+(x2.^2)))), 0.5) and remove the outer functions/operators step by step. Once the result is a vector we know that the last thing removed ruined the result.

    In your case / turns out to be the culprit.
    From Octave, Arithmetic Ops (emphasis mine):

    x / y
    Right division. This is conceptually equivalent to the expression (inv (y') * x')' but it is computed without forming the inverse of y'. If the system is not square, or if the coefficient matrix is singular, a minimum norm solution is computed.

    x ./ y
    Element-by-element right division.

    Therefore, replace / by ./.

    x2 = 0:0.1:50;
    y2 = power(1 + 2 .* x2 ./ (exp(0.5 .* x2) + (x2 .^ 2)), 0.5);
    plot(x2, y2, '-b');
    

    plot