Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Question about plot a prime number function in matlab

Asked by Hao on 2 May 2013

im having problem on plot in matlab,

it asks me to plot prime numbers on a number line, and before this exercise i did wrote functions for to test if a number is prime, and find the prime numbers in a range,

and this is what i did,

test of Prime number:

Prime=true;
for TestVal=2:sqrt(InVal)
    %if InVal is divisible by TestVal
    if mod(InVal,TestVal) == 0
        Prime=false;
    end
end
end

find prime numbers in a range:

lowerlimit = input('Please enter a lower limit');
upperlimit = input('Please enter a upper limit');
for num=lowerlimit:upperlimit
    if PrimeNum(num) == 1
        disp(num2str(num))
    end
end

and now i need to use the previous function to plot prime number on a number line, and this is what ive got so far but i have no idea if its on the right track, can anyone help me please?

   xVec = length(xVec);
  yVec=zeros;
  plot(xVec, yVec,'o')
xVec=PrimeRange;
for
    if PrimeRange 
        is PrimeNum()
        xVec=[xVec currentval];
    end
end

there is error occured and im not sure what im doing...

regards

Hao

1 Comment

Jan Simon on 2 May 2013

Please post the error message. It is impossible to suggest an improvement based only on the fact that there is an error anywhere. What are the defintions of "PrimeRange" and "PrimeNum"? What is "currentval"? There is something missing in the line "for ". "xVec=PrimeRange" looks, like "PrimeRange" is a vector or a function, which replies a vector. Then "if Primerange" is not useful.

Hao

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 2 May 2013

Your line

xVec = length(xVec);

is wrong, as xVec has not been defined by that point.

0 Comments

Walter Roberson

Contact us