use of cosine in a function
1 view (last 30 days)
Show older comments
Hi everyone ! I had a little problem in cosine function.I use it in a small routine in order to calculate the circumscribed circle of a triangle parameters. I got this error message all the time : Attempted to access cos(0.523599); index must be a positive integer or logical.
The function is:
if true
% function [Xp,Yp,R] = Cercle_circonscrit(Xa,Ya,Xb,Yb,Xc,Yc)
%distances calculating:
AB=sqrt((Xb-Xa)^2+(Yb-Ya)^2);
BC=sqrt((Xb-Xc)^2+(Yb-Yc)^2);
CA=sqrt((Xc-Xa)^2+(Yc-Ya)^2);
%scalar product:
scal_1=(Xb-Xa)*(Xc-Xa)+(Yb-Ya)*(Yc-Ya);
%angle Â:
cosA=scal_1/(AB*CA);
sinA=sqrt(1-cosA^2);
%radius of the circumscribed circle
R=BC/(2*sinA);
%coordinate of the center:
cos=0.5*BC/R; sin=sqrt(1-cos*cos);
PP1=R*sin;
Xp1=0.5*(Xc+Xb);
Yp1=0.5*(Yc+Yb);
dx=Xc-Xb;
dy=Yc-Yb;
if dx==0
c=0;
if dy<=0
s=-1;
else
s=1;
end
else
c=cos(atan2(dx,dy)); s=sin(atan2(dx,dy));
end
Xps=Xp1-PP1*s;
Xpi=Xp1+PP1*s;
Yps=Yp1-PP1*c;
Ypi=Yp1+PP1*c;
APs=sqrt((Xps-Xa)^2+(Yps-Ya)^2);
if (APs-R)<=0.0001*R
Xp=Xps;
Yp=Yps;
else
Xp=Xpi;
Yp=Ypi;
end
end
It's like cosine is a table!
Thank's for your help !
0 Comments
Accepted Answer
Azzi Abdelmalek
on 1 Dec 2013
Edited: Azzi Abdelmalek
on 1 Dec 2013
Look at this line
cos=0.5*BC/R; sin=sqrt(1-cos*cos);
cos is used as a variable, then the function cos will not work. The same remark for sin
1 Comment
Azzi Abdelmalek
on 1 Dec 2013
Edited: Azzi Abdelmalek
on 1 Dec 2013
[Waddad commented]
Thank you ! It works!
More Answers (0)
See Also
Categories
Find more on Logical 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!