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

Learn moreOpportunities for recent engineering grads.

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

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

*No products are associated with this question.*

Answer by Walter Roberson on 2 May 2013

Your line

xVec = length(xVec);

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

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/74298#comment_146765

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.