0

I am making a Mersenne Generator in MATLAB and I am using the code as follows:

%// Define parameters    
a=65539;    
c=0;    
x0=1;    
m=2^31;

%// Calculate sequence using recursion relation    
xn=zeros(20000,1);

for i=1:20000    
    xn(i)=mod(a*x0+c,m);
    x0=xn(i);    
end

%// Divide by m to give real numbers between 0 and 1    
un=xn/m;

%// Plot 3-tuples of the u_i in 3D space    
plot3(un(1:end-2),un(2:end-1),un(3:end),’b.’);    
xlabel(’u_i’); ylabel(’u_{i+1}’); zlabel(’u_{i+2}’); grid(’on’);

When I run it in my MATLAB, it gives me the following error:

plot3(un(1:end-2),un(2:end-1),un(3:end),’b.’);

Error: The input character is not valid in MATLAB statements or expressions.

Any help would be appreciated.

Community
  • 1
  • 1

1 Answers1

4

The single quotations at the end of the plot code are unicode versions that MATLAB does not accept. This is probably because you copied and pasted the code from Microsoft Word or some other text editor that transformed single quotations into those screwed up characters instead.

Actually use single quotation characters:

plot3(un(1:end-2),un(2:end-1),un(3:end),'b.');    
%//                                     ^  ^
xlabel('u_i'); ylabel('u_{i+1}'); zlabel('u_{i+2}'); grid('on');
%//    ^   ^          ^       ^          ^       ^        ^  ^
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • +rayryeng Thanks a lot, I also want to ask one more doubt, In MATLAB the Rand function uses mersenne Twister Generator, If i want to Implement my Own mersenne twister or Re-Make it than do you know any possible resources where i can Look at the MATLAB Code behind how the rand functions work ? – Hardik Shekhaliya May 09 '16 at 06:35
  • Sorry I don't. That theory is beyond me. I mainly answered to show you how to run your code. If you have no more questions about this particular error, please consider accepting this answer to let the community know you no longer need help. All the best and good luck. – rayryeng May 09 '16 at 06:36
  • No Issue regarding that, Do you know ny Way to convert a python code into a MATLAB code, is there any way ? – Hardik Shekhaliya May 09 '16 at 06:52
  • 2
    Nope. You have to do it yourself. There are no automated tools. If you have more questions, consider opening up another one. I believe I have addressed all of your issues. Take care. – rayryeng May 09 '16 at 06:54