problem with my obstacle avoidance code

3 次查看(过去 30 天)
hi i am using this code for circle generation
function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
  1. The obstacles are defined as circles
  2. when i try to callt the function from another script file it seemed fine at once but the next time when i tried doing it i got this error
"Error in ci1 (line 2)
circle =ci(300,500,20);
function h = ci(x,y,r)
Error: Function definitions are not permitted in this context."
i have developed the logic for the collision avoidance that is
if %touching internally
if(c= = rless)
disp('there is collison');
% circles intersecting
else
if (c>=rless)
disp('there is collision');
%touching externally
else
if (c= = rsum)
disp('there is a collision');
% circle inside another circle
else
if (c< rmod)
disp('there is a collison');
% if there is no collision
if(c>rsum)
disp('there is no collision');
end
end
end
end
end
% where rless=|r1-r2|
% where rsum=r1+r2
%rmod=|r2-r1|
%where c is the distance between two circle centers (x1,y1) and(x2,y2)
  • # This is just the rough idea of the logic i intend to apply at the calling script file
  • # That is where the function of the circles are called can you please help me sort this out
thank you in advance

回答(2 个)

Image Analyst
Image Analyst 2015-6-6
What does line 1 say? It better say
function ci1()
or else you'd get that error. Also, you can't do this
if(c= = rless)
you cannot have a space in between two equal signs. You probably want
if c == rless

Walter Roberson
Walter Roberson 2015-6-6
Functions need to be stored in .m files to be called upon.
Also in any one .m file, you can have functions (the file starts with "function"), or class definitions (the file starts with "classdef"), or script instruction (the file starts with anything else), but you cannot mix those. A script file cannot also define a function. You can have multiple functions in one .m though:
function ci1
circle = ci(300,500,20);
end
function h = ci(x,y,r)
...
end
  3 个评论
Walter Roberson
Walter Roberson 2018-2-22
Testing a square is easy if it is aligned with the axes: left <= x & x <= right & bottom <= y & y <= top
Walter Roberson
Walter Roberson 2018-8-20
Note: effective R2016b it became possible to define functions at the bottom of script files.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by