Plotting Peaks function in Octave doesn't produce correct graph - matlab

I am trying to use octave to do a data approximation project, but I am running into some trouble testing some basic things. I'm not very experienced with octave, so I was hoping someone could point me in the right direction.
I'm attempting to generate a list of x,y points using the Halton sequence. I want to plug those x,y points into the peaks function to get the z peaks values for those coordinates. Next, I want to plot those x,y,z points to be sure that my plot resembles the peak plot, but my plot looks wrong.
Here is the code:
%13 for this example to compare to meshgrid
N = 13;
%creates a halton sequence for N and scales it between -3 to 3
seq = haltonseq(N, 2) * 6 – 3;
%save the x,y coordinate from the halton sequence
X = seq(:,1);
Y = seq(:,2);
%convert the x,y vector in matrixes
X = repmat(X, 1, N)';
Y = repmat(Y,1,N);
%get the z values for x,y
Z = peaks(X, Y);
%plot my x,y,z values
surf(X,Y,Z);
%plot meshgrid to compare to
[mX mY] = meshgrid(-3:0.5:3);
mZ = peaks(mX, mY);
surf(mX, mY, mZ);
The Halton sequence is correct, but I will include it for verification. This is after it has been scaled to be between -3 and 3:
seq =
0.00000 -1.00000
-1.50000 1.00000
1.50000 -2.33333
-2.25000 -0.33333
0.75000 1.66667
-0.75000 -1.66667
2.25000 0.33333
-2.62500 2.33333
0.37500 -2.77778
-1.12500 -0.77778
1.87500 1.22222
-1.87500 -2.11111
1.12500 -0.11111
Mesh Graph:
Halton Graph N = 13:
Halton Graph N = 300:
Now, to the best of my understanding of this code, the Halton sequence plot should not look like it does, and as I increase N, it should resemble the meshgrid graph more, but it keeps its same deformed shape. I'm not sure where I am going wrong, can someone point me in the right direction?
Note: I am using this code with Octave now, but I will also be using it with Matlab later, so I tagged it as both.

A quick look at your X and Y reveals the problem:
octave:33> X
X =
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
octave:34> Y
Y =
-1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000
1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000
-2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333
-0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333
1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667
-1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667
0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333
2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333
-2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778
-0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778
1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222
-2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111
-0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111
As you can see, they are not sorted properly.
When plotting, order (usually) matters; see below for an explanation.
Adding the following two lines after the repmat fixes it:
X = sort(X,2);
Y = sort(Y,1);
Here's the result:
Explanation
The problem is that, when plotting graphs where lines have to be drawn between points, ordering matters.
To illustrate, consider this small example:
x1 = [1 2 3 4];
x2 = [3 1 4 2];
plot(x1,x1.^2);
plot(x2,x2.^2);
These two will plot the exact same points, but in a different order.
The lines joining them will hence look different:
Plot for x1:
Plot for x2:

Related

Substitute a vector of variables for syms when using linsolve for linear equations

I am trying to solve linear equations with many variables. So i used this to create multiple variables.
xvariables = sym('x', [n 1])
where n is the number of variables and it created a vector of variables x1 x2 x3...xn
So how do I use this vector beside syms when using linsolve instead of listing out all the variables?
% syms xvariables (something like that) instead of:
syms x2 x3 x4 x5 x6 x7 x8
eqn1 = 0.5*x2 + 0.12*x3 == 21.8;
eqn2 = 0.12*x2 + 0.5*x3 + 0.12*x4 == 21.9;
eqn3 = 0.12*x3 + 0.5*x4 + 0.12*x5 == 47.8;
eqn4 = 0.12*x4 + 0.5*x5 + 0.12*x6 == 37.6;
eqn5 = 0.12*x5 + 0.5*x6 + 0.12*x7 == 27.5;
eqn6 = 0.12*x6 + 0.5*x7 + 0.12*x8 == 52.5;
eqn7 = 0.12*x7 + 0.5*x8 == 59;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4, eqn5, eqn6, eqn7], [x2, x3, x4, x5, x6, x7, x8])
X = linsolve(A,B)
I hope that makes sense. Thanks!
When using sym('x',[n 1]) you are creating a symbolic vector with automatically generated elements [x1; x2; ...; xn]. The elements do not directly appear in the MATLAB workspace but they are accessible by using parentheses on the created variable (xvariables in your question, x in my answer). Therefore we use the same syntax as with a 'normal' vector/matrix. Thus xn can be accessed with x(n).
For the equations we can use a vector as well and store them in a single variable eqn. This additionally simplifies the call to equationsToMatrix.
This yields the following code:
x = sym('x', [7 1]);
eqn(1) = 0.50*x(1) + 0.12*x(2) == 21.8;
eqn(2) = 0.12*x(1) + 0.50*x(2) + 0.12*x(3) == 21.9;
eqn(3) = 0.12*x(2) + 0.50*x(3) + 0.12*x(4) == 47.8;
eqn(4) = 0.12*x(3) + 0.50*x(4) + 0.12*x(5) == 37.6;
eqn(5) = 0.12*x(4) + 0.50*x(5) + 0.12*x(6) == 27.5;
eqn(6) = 0.12*x(5) + 0.50*x(6) + 0.12*x(7) == 52.5;
eqn(7) = 0.12*x(6) + 0.50*x(7) == 59;
[A,B] = equationsToMatrix(eqn, x)
X = linsolve(A,B)
You have better to use the mldivideoperator. (symbol \) (in my opinion!)
You have your matrix P that contains the equation:
p =
[0.50000 0.12000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.12000 0.50000 0.12000 0.00000 0.00000 0.00000 0.00000;
0.00000 0.12000 0.50000 0.12000 0.00000 0.00000 0.00000;
0.00000 0.00000 0.12000 0.50000 0.12000 0.00000 0.00000;
0.00000 0.00000 0.00000 0.12000 0.50000 0.12000 0.00000;
0.00000 0.00000 0.00000 0.00000 0.12000 0.50000 0.12000;
0.00000 0.00000 0.00000 0.00000 0.00000 0.12000 0.50000]
or if you don't want to write the 0:
p = zeros(7)
p(1,1) = 0.5;
p(1,2) = 0.12;
p(2,1) = 0.12;
...
Your vector s that contains the solution
s = [21.8,21.9,47.8,37.6,27.5,52.5,59]'
And you can resolve your system with:
>> res = p\s
res =
40.0000 %x2
15.0000 %x3
80.0000 %x...
50.0000
25.0000
75.0000
100.0000

Combining columns of two matrices of different dimension in Matlab?

I have a matrix in Matlab A of dimension nx3, e.g. n=8
A=[ 0.3 2 2;
0.3 7 7;
0.3 10 10;
0 15 15;
0.3 18 2;
0.3 23 7;
0 26 10;
0.3 31 15]
and a matrix B of dimension mx4, e.g. m=17
B=[1 1 0.05 0.05;
2 2 0.22 0.22;
3 3 0.19 0.05;
5 5 0.02 0.02;
6 6 0.19 0 ;
7 7 0.30 0.11;
10 10 0.27 0.08;
11 11 0.19 0 ;
12 12 0.05 0.05;
18 2 0.25 0.08;
19 3 0.25 0.08;
21 5 0.02 0.02;
22 6 0.22 0.08;
23 7 0.22 0.08;
30 14 0.19 0.08;
31 15 0.19 0.08;
32 16 0.05 0.05]
I want to create a matrix C following these steps WITHOUT USING LOOPS:
1) Generate C=[];
2) Consider B(i,1). If there exists A(j,2)=B(i,1) [it can happen only once] report C=[ C; B(i,1) B(i,2) B(i,3) B(i,4) A(i,1)]. Do this for i=1,...,m.
3) Consider B(h,1) such that there is no j with A(j,2)=B(h,1). Report C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0]. Do this for h=1,...,m.
4) Consider A(h,2) such that there is no j with B(j,1)=A(h,2). Report C=[C; A(h,2) A(h,3) 0 0 A(h,1)]. Do this for h=1,...,n.
In the example above I want to get
C=[2 2 0.22 0.22 0.3;
7 7 0.30 0.11 0.3;
10 10 0.27 0.08 0.3;
18 2 0.25 0.08 0.3;
23 7 0.22 0.08 0.3;
31 15 0.19 0.08 0.3; %end step 2)
---------------------
1 1 0.05 0.05 0 ;
3 3 0.19 0.05 0 ;
5 5 0.02 0.02 0 ;
6 6 0.19 0 0 ;
11 11 0.19 0 0 ;
12 12 0.05 0.05 0 ;
19 3 0.25 0.08 0 ;
21 5 0.02 0.02 0 ;
22 6 0.22 0.08 0 ;
30 14 0.19 0.08 0 ;
32 16 0.05 0.05 0 ;
----------------------- %end step 3)
15 15 0 0 0 ;
26 10 0 0 0 ] %end step 4)
These code does what I want but it is too slow with bigger matrices
C=[];
%Step 1)
for l=1:size(B,1)
for h=1:size(A,1)
if B(l,1)==A(h,2)
C=[C; B(l,:) A(h,1)];
end
end
end
% Steps 2) and 3)
C=[C; ...
[B(logical(1-ismember(B(:,1), A(:,2))),:) zeros(size(B(logical(1-ismember(B(:,1), A(:,2))),:),1),1)];...
[A(logical(1-ismember(A(:,2), B(:,1))),2:3) ...
zeros(size(A(logical(1-ismember(A(:,2), B(:,1)))),1),2) ...
A(logical(1-ismember(A(:,2), B(:,1))),1)]];
Although it smells strongly like homework, here is some code. See it as a tutorial on matrix operations (tested with Octave).
% Step 1
[~,j,k] = intersect(B(:,1),A(:,2));
C = [B(j,:) A(k,1)];
% Step 2
[~,k] = setdiff(B(:,1),A(:,2));
C = [C; B(k,:) zeros(size(k,1),1)]
% Step 3
[~,k] = setdiff(A(:,2),B(:,1));
C = [C; A(k,[2 3]) zeros(size(k,1),2) A(k,1)]
C =
2.00000 2.00000 0.22000 0.22000 0.30000
7.00000 7.00000 0.30000 0.11000 0.30000
10.00000 10.00000 0.27000 0.08000 0.30000
18.00000 2.00000 0.25000 0.08000 0.30000
23.00000 7.00000 0.22000 0.08000 0.30000
31.00000 15.00000 0.19000 0.08000 0.30000
1.00000 1.00000 0.05000 0.05000 0.00000
3.00000 3.00000 0.19000 0.05000 0.00000
5.00000 5.00000 0.02000 0.02000 0.00000
6.00000 6.00000 0.19000 0.00000 0.00000
11.00000 11.00000 0.19000 0.00000 0.00000
12.00000 12.00000 0.05000 0.05000 0.00000
19.00000 3.00000 0.25000 0.08000 0.00000
21.00000 5.00000 0.02000 0.02000 0.00000
22.00000 6.00000 0.22000 0.08000 0.00000
30.00000 14.00000 0.19000 0.08000 0.00000
32.00000 16.00000 0.05000 0.05000 0.00000
15.00000 15.00000 0.00000 0.00000 0.00000
26.00000 10.00000 0.00000 0.00000 0.00000

Page rank MATLAB

trying to fix my code that does page rank. Not sure why the power method and the backslash are coming up with different answers and which is wrong and how to fix it.
p=rand(length(M),1);
pnorm=norm(p,1);
p=p/pnorm;
n=length(M);
d=0;
e=ones(n,1);
a=1/n;
Z=a*(e*e');
Mtilda=d*M+(1-d)*Z;
for k=1:n
tic;
prev_p = p;
z = Mtilda*p;
p = z/norm(z,2);
dl_p = norm(p-prev_p, 2);
t(k)=toc;
str=sprintf('for d=%g , iteration %d: time=%11.4g',d,k,t(k));
disp(str);
if dl_p < 1e-6; break; end
end
ReportPageRank(p,U,0.005)
d=1;
p2 =(eye(n)-d*M)\((((1-d)/n)*e));
You've figured it out, but I'll post my work anyways in case someone's interested in the implementation.
M = ...
[ 0.09091 0.00000 0.00000 0.50000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 1.00000 0.50000 0.33333 0.50000 0.50000 0.50000 0.50000 0.00000 0.00000;
0.09091 1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.33333 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.00000 0.50000 0.50000 0.50000 0.50000 1.00000 1.00000;
0.09091 0.00000 0.00000 0.00000 0.33333 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000;
0.09091 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000];
N = length(M);
PR = (1/N)*ones(length(M),1); %define PageRank vector for t = 0
d = 0.85; %define damping rate
iter = 1;
delta_PR = Inf; %set initial error to infinity
while delta_PR > 1e-6 %iterate until error is less than 1e-6
tic;
prev_PR = PR; %save previous PageRank vector (t-1)
PR = d*M*PR + ((1-d)/N)*ones(N,1); %calculate new PageRank (t)
delta_PR = norm(PR-prev_PR);%calculate new error
t(iter)=toc;
str=sprintf('for d=%g , iteration %d: time=%11.4g',delta_PR,iter,t(iter));
disp(str);
iter = iter + 1;
end
powerRank = pinv((eye(length(M)) - d*M))*(((1-d)/N)*ones(length(M),1));
PR = powerRank =
0.0328
0.3844
0.3429
0.0391
0.0809
0.0391
0.0162
0.0162
0.0162
0.0162
0.0162

Weird behavior from Octave's atan2

I'm trying to port some Octave code to Java. I'm trying out atan2 so I have the following tries
octave3.2:53> m1 = [1,2;3,4]
m1 =
1 2
3 4
octave3.2:54> m2=[5,6;7,8]
m2 =
5 6
7 8
octave3.2:55> atan2(m1,m2)
ans =
0.19740 0.32175
0.40489 0.46365
octave3.2:56> atan(1/5)
ans = 0.19740
octave3.2:57> atan(2/6)
ans = 0.32175
octave3.2:58> atan(3/7)
ans = 0.40489
octave3.2:59> atan(4/8)
ans = 0.46365
So it seems to me that atan2 applies atan cell-wise, to the quotient of the first argument divided by the second argument. I got this well in Java. However, the following piece of code gives off something weird...
y
x
theta = atan2(y,x)
For which I get a trace something like,
y =
-0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000
-0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000
-0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000
-0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000
-0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000
0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000
0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000
0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000
x =
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
theta =
-2.35619 -2.24554 -2.11122 -1.95130 -1.76819 -1.57080 -1.37340 -1.19029 -1.03038 -0.89606
-2.46685 -2.35619 -2.21430 -2.03444 -1.81577 -1.57080 -1.32582 -1.10715 -0.92730 -0.78540
-2.60117 -2.49809 -2.35619 -2.15880 -1.89255 -1.57080 -1.24905 -0.98279 -0.78540 -0.64350
-2.76109 -2.67795 -2.55359 -2.35619 -2.03444 -1.57080 -1.10715 -0.78540 -0.58800 -0.46365
-2.94420 -2.89661 -2.81984 -2.67795 -2.35619 -1.57080 -0.78540 -0.46365 -0.32175 -0.24498
3.14159 3.14159 3.14159 3.14159 3.14159 0.00000 0.00000 0.00000 0.00000 0.00000
2.94420 2.89661 2.81984 2.67795 2.35619 1.57080 0.78540 0.46365 0.32175 0.24498
2.76109 2.67795 2.55359 2.35619 2.03444 1.57080 1.10715 0.78540 0.58800 0.46365
2.60117 2.49809 2.35619 2.15880 1.89255 1.57080 1.24905 0.98279 0.78540 0.64350
2.46685 2.35619 2.21430 2.03444 1.81577 1.57080 1.32582 1.10715 0.92730 0.78540
What's with that? I think the first cell of theta should ~0.785398. The other cells seem wrong too. Is there anything I missed?
Found the answer on my own. atan2 isn't simply atan applied cell-wise. The sign of the operands matter as well.
For more info see: http://sunsite.univie.ac.at/textbooks/octave/octave_17.html
And then for how to implement atan2 by yourself (as I'm doing) see: http://en.wikipedia.org/wiki/Atan2
Atan2 is not quiet atan as it takes into account the quadrant.
see: http://en.wikipedia.org/wiki/Atan2
your first case is atan(x/y) - Pi

Curious behaviour with anonymous functions in Octave and MATLAB

Hi I am curious why I am getting the following behaviour with MATLAB and Octave
octave:7> pdf = #(x) (0<=x && x<1).* (x) + (1<=x && x <=2).* (2-x);
octave:8>
octave:8> t = 0:0.1:1;
octave:9>
octave:9> y = pdf(t)
y =
0 0 0 0 0 0 0 0 0 0 0
octave:10>
I get the same behavior with MATLAB i.e. y is a zero vector.
But if I add the following for loop
for i=1:size(t,1)
y(i) = pdf(t(i))
end
then I get the correct result.
Columns 1 through 19:
0.00000 0.10000 0.20000 0.30000 0.40000 0.50000 0.60000 0.70000 0.80000 0.90000 1.00000 0.90000 0.80000 0.70000 0.60000 0.50000 0.40000 0.30000 0.20000
Columns 20 and 21:
0.10000 0.00000
The && and || are the short circuit operators, meant for use with scalars. Replace with & or |. I get an error when executing the above (vectorized) code in Matlab (R2011B).
After replacing the && with & it seems to work as you expect.