Adam
Adam

Reputation: 60

Octave does not plot the function

I am preparing a graph of binding protein behavior.

x = linspace(0,1,101)
y = ( x.*2.2*(10^-4))/(( x.+6.25*(10^-2))*(x.+2.2*(10^-2)))
plot(x,y)

Should result a bell curve(maybe) or a curve but i am getting a linear graph. I had checked with other software and resulting curve that function. Any help please?

Upvotes: 1

Views: 283

Answers (1)

Andrew Janke
Andrew Janke

Reputation: 23908

What's wrong

You want to use ./ array division, not / matrix division.

How to debug this

First, get some spaces up in here so it's easier to read. And add semicolons to suppress that big output.

x = linspace(0, 1, 101);
y = (x.*2.2*(10^-4)) / ( ( x.+6.25*(10^-2)) * (x.+2.2*(10^-2)) );
plot(x, y)

Then stick it in a function for easier debugging:

function my_plot_of_whatever
x = linspace(0, 1, 101);
y = (x.*2.2*(10^-4)) / ( ( x.+6.25*(10^-2)) * (x.+2.2*(10^-2)) );
plot(x, y)

Now try it:

>> my_plot_of_whatever
error: my_plot_of_whatever: operator *: nonconformant arguments (op1 is 1x101, op2 is 1x101)
error: called from
    my_plot_of_whatever at line 3 column 3

When you get a complaint like that about * or /, it usually means you're doing matrix operations when you really want the elementwise "array" operations .* and ./. Fix that and try it again:

>> my_plot_of_whatever
>>

bad linear plot

So what's going on here? Let's use the debugger!

>> dbstop in my_plot_of_whatever at 4
ans =  4
>> my_plot_of_whatever
stopped in /Users/janke/Documents/octave/my_plot_of_whatever.m at line 4
4: plot(x, y)
debug> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        x           1x101                      808  double
        y           1x1                          8  double

Aha. Your y is scalar, so it's using the same Y value for every X value. That's because you're using / matrix division, when you really want ./ array division. Fix that:

function my_plot_of_whatever
x = linspace(0, 1, 101);
y = (x.*2.2*(10^-4)) ./ ( ( x.+6.25*(10^-2)) .* (x.+2.2*(10^-2)) );
plot(x, y)

Bingo.

enter image description here

Upvotes: 1

Related Questions