Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need little coding help

Status
Not open for further replies.

catenaryworks

Structural
Jun 23, 2011
24
0
0
US

The code below plots 'k' along the y-axis and 'theta' along the x-axis.

I would like help on the following, for which I do not know how to write out the code in MATLAB:
1. I want to extract only those 'theta' values corresponding to integer values of 'k'
2. Then, use the above 'theta' values and solve for corresponding 'alpha' values from the equation:
d*sin(alpha) - r*sin(theta-alpha) = 0
3. I would also like to print an array of x and y coordinates corresponding to the above theta values
x = r*cos(theta) and y = r*sin(theta)

n=12;
d = 200;
r = 3200;
dbyr=d/r;
theta=0:0.0001:2*pi;
k=(n/(2*pi))*(theta+dbyr*sin(theta));
plot(theta,k)

Thanks.
 
Replies continue below

Recommended for you

So, this is for school?

TTFN
faq731-376
7ofakss

Need help writing a question or understanding a reply? forum1529
 

k does have integer values: 1,2,.......,12. The y-axis of the plot generated shows them.

n=12;
d = 200;
r = 3200;
dbyr=d/r;
theta=0:0.0001:2*pi;
k=(n/(2*pi))*(theta+dbyr*sin(theta));
plot(theta,k)

Thanks.
 
Did you not take trig in high school? If this is not homework, why must you do this in MATLAB, since the solutions for theta can be found by inspection?x`

TTFN
faq731-376
7ofakss

Need help writing a question or understanding a reply? forum1529
 
Unless his equation is incorrect, Theta as multiples of pi result in integer values of k, since the sin term gets zeroed, leaving n*m*pi/2*pi = 6m, since n=12.

TTFN
faq731-376
7ofakss

Need help writing a question or understanding a reply? forum1529
 
To be precise, there is exactly one integer value in k. You can find it like so:

for j=1:62832
if(round(k(j)) == k(j))
j
end
end

Unsurprisingly, it's the value for when theta is 0. As already stated, everything else gives you an expression with pi, which is never going to be exactly integer.

To suggest "k does have integer values: 1,2,.......,12. The y-axis of the plot generated shows them." is wrong - the plot shows a continuous line. The values of k are discrete, and are never exactly integer (except at the origin). If you want values that are "close enough" to integers, you need to use numerical methods. Time to rethink the problem you're trying to solve. Which is?
 
IRStuff, the input is never pi, 2*pi or 3*pi. The input is "0:0.0001:2*pi;". So 0 is in but the closest to pi you get is 3.1416 and oddly enough, even the last input is not 2*pi (Matlab rounds it to something a bit surprising - I can't remember exactly what).

I agree, if the input were exactly pi, 2*pi or 3*pi, the equation would give a rational result since the sin term goes to zero and the remaining theta term cancels with the pi on the denominator.
 
The OP is wandering aimlessly. Look at what he says he wants:
"I want to extract only those 'theta' values corresponding to integer values of 'k' " The fact that he can't even figure out his own Matlab code suggests that his vector for theta is arbitrarily constructed.

TTFN
faq731-376
7ofakss

Need help writing a question or understanding a reply? forum1529
 
Status
Not open for further replies.
Back
Top