-3

I have these 3 functions

function r = RingRead(n,p)
    r=( p * RingReadggg(n-1,p))+( (1-p)* RingReadggb(n-1,p));
end

function r = RingReadggb(n , p)
    if n <= 1
        r = 0;
    else
        r = ((1-p)* RingReadggb(n-1,p) )+ p^2 +( p(1-p)* RingReadggb(n-2,p));
    end
end

function r = RingReadggg(n , p)
    if n == 1
        r = p;
    else
        r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));
    end
end

Below given program uses above given functions.

for p = 0.50:0.05:1
    r =  RingRead(4,p);
    plot(p,r)
    hold on       
end

When i run this it gives error

??? Subscript indices must either be real positive integers or logicals.

Error in ==> RingRead>RingReadggg at 18 r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));

Error in ==> RingRead at 3 r=( p * RingReadggg(n-1,p))+( (1-p)* RingReadggb(n-1,p));

Error in ==> RingAvailability at 2 r = RingRead(4,p);

Amro
  • 123,847
  • 25
  • 243
  • 454
  • please give us some context and describe what you are trying to do with your functions. "Here code, you fix" are not exactly good questions... – Amro Sep 23 '11 at 21:11
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 15:44

2 Answers2

2

Your code crashes right here:

r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)))

n is 3 and p is 0.5

but what is

p(1-p) 

supposed to be? do you mean p * (1-p) ?

memyself
  • 11,907
  • 14
  • 61
  • 102
1

As the message says, the error occurs on this line:

r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));

By p(1-p), do you really mean p * (1-p)? Entered as p(1-p) is interpreted as indexing into p at the index 1-p. Try changing the line to:

r = (p+p*(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));

It looks like you have the same issue in RingReadggb as well.

b3.
  • 7,094
  • 2
  • 33
  • 48