scatter plot in matlab

1 Ansicht (letzte 30 Tage)
Ying
Ying am 16 Jan. 2012
Hi,
I'm new to Matlab. And I need to write a congruential random number generator without using RAND().And then plot the Ui vs Ui+1
My code is:
>> syms m x a u z;
>> x=1;
>> a=16807;
>> m=2^31-1;
>> for i=1:10
y=a*x
x=mod(y,m)
u(i)=x/m %store u value
end;
>> for i=1:10
end;
>> scatter(u(i+1),u(i))
I got this"Index exceeds matrix dimensions." error message.
Anyone who can tell me how to plot my ui vs ui+1? I think my code does generate correct ui values.
I'm using it for tomrrow's class.
Thanks very much!

Akzeptierte Antwort

the cyclist
the cyclist am 16 Jan. 2012
I think you should be able to do the scatter plot you want using:
scatter(u(2:end),u(1:end-1))
The reason you got the error you did is that i is equal to 10, so in the scatter command you are trying to access the ( i+1 )th element of u. But u is only a 10-element vector, of course.
  1 Kommentar
Ying
Ying am 16 Jan. 2012
the cyclist,
Thanks a lot for answering my question! This looks identical to my excel scatter output. I think this should be fine for my class.
Ying

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by