Question about plot a prime number function in matlab

7 views (last 30 days)
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
Jan 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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 2 May 2013
Your line
xVec = length(xVec);
is wrong, as xVec has not been defined by that point.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!