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

# 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
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
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.

## Products

No products are associated with this question.

Answer by Walter Roberson on 2 May 2013

```xVec = length(xVec);