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

Thread Subject:
Roger Stafford help me(about matlab m-file)

Subject: Roger Stafford help me(about matlab m-file)

From: aassdd asdf

Date: 27 Nov, 2010 10:53:03

Message: 1 of 2

Hello, Roger Stafford .
I visit this site again because of problem.
Could you plz help me again??

I typing like this in matlab.
pro -(enter)> 0(center of coordinates x) ->0(center of coordinates y)->1(start point of x)->1(start point of y)->90 (degree)
but when I typing like this. Our graph didn`t start from start point. our graph only start (1,0).
T,.T

this is m-file...
===============================================
function pro(xc,yc,x1,y1,deg)

xc = input( ' center of coordinates x : ' );
yc = input( ' center of coordinates y : ' );
x1 = input( ' start point of x : ' );
y1 = input( ' start point of y : ' );
deg = input( ' degree : ' );

r=sqrt((x1-xc)^2+(y1-yc)^2);
deg1=atan((y1-yc)/(x1-xc));

if x1 >= xc && y1 > yc, deg1 = deg1;end
if x1 < xc && y1 >= yc, deg1 = 180 + deg1;end
if x1 <= xc && y1 < yc, deg1 = 180 + deg1;end
if x1 > xc && y1 <= yc, deg1 = 360 + deg1;end

deg3 = linspace(deg1,(deg1+deg));

x1 = xc + r*cos((deg1)*pi/180);
y1 = yc + r*sin((deg1)*pi/180);
x2 = xc + r*cos((deg+deg1)*pi/180);
y2 = yc + r*sin((deg+deg1)*pi/180);
xp = xc + r*cos(deg3*pi/180);
yp = yc + r*sin(deg3*pi/180);


d = linspace(0,2*pi);
plot(xc + r*cos(d), yc + r*sin(d),'.');
hold on


plot(xc,yc,'O');
hold on

plot(x1,y1,'O');
hold on

plot(x2,y2,'O');
hold on

plot(xp,yp,'.');
hold on

grid on;

Subject: Roger Stafford help me(about matlab m-file)

From: Roger Stafford

Date: 28 Nov, 2010 05:09:04

Message: 2 of 2

"aassdd asdf" <ts10004ok@hanmail.net> wrote in message <icqnuf$gui$1@fred.mathworks.com>...
> .......
> deg1=atan((y1-yc)/(x1-xc));
> ......
- - - - - - - -
  First, your trouble stems from the line

 deg1=atan((y1-yc)/(x1-xc));

The matlab function 'atan' returns its values in radians, not degrees, so this would have the effect of starting with the wrong angle for the vector from (xc,yc) to (x1,y1). You later converted this angle from what you supposed were degrees over to radians even though it was already in radians.

  Second, you really ought to use the 'atan2' function to save yourself all the trouble comparing xc with x1 and yc with y1. Just do

 ang1 = atan2(y1-yc,x1-xc);

The answer will return in radians somewhere between -pi and +pi, giving the correct quadrant for the starting angle. No need for comparing x1 with xc and y1 with yc. Also no need for converting to radians - atan2 is already in radians.

  Of course if you initially enter 'deg' in degrees you will have to promptly convert that to radians. Otherwise you will overrun your planned stopping point by a considerable amount.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us