Question: here is a code for bug 1 algorithm in matlab. the output of this code is shown in the first image which is not desired.

here is a code for bug 1 algorithm in matlab. the output of this code is shown in the first image which is not desired. the output graph should look like the second image. so rewrite the code make the changes so that the output looks like the 2nd image:
% Bug 1 Algorithm in MATLAB
clc; clear; close all;
% Start and goal positions
start =[2,2]; % Start point (x, y)
goal =[15,15]; % Goal point (x, y)
% Define obstacles as circles with center and radius
obstacles =[5,5,2; 10,8,3]; %[x_center, y_center, radius]
% Plot environment, start, and goal
figure; hold on; axis equal;
plot(start(1), start(2),'go', 'MarkerSize', 10, 'DisplayName', 'Start');
plot(goal(1), goal(2),'ro', 'MarkerSize', 10, 'DisplayName', 'Goal');
% Plot the obstacles
for i =1:size(obstacles,1)
viscircles(obstacles(i,1:2), obstacles(i,3), 'LineWidth', 1);
end
legend;
xlabel('X'); ylabel('Y'); title('Bug 1 Algorithm Path Planning');
% Initialize robot position at the start point
robot_pos = start;
path = robot_pos; % Store the robot's path
% Parameters
step_size =0.2; % Robot's step size
tolerance =0.1; % Distance threshold for reaching the goal
while norm(robot_pos - goal)> tolerance
% Compute the straight-line direction to the goal
direction =(goal - robot_pos)/ norm(goal - robot_pos);
% Predict next step
next_pos = robot_pos + step_size * direction;
% Check for collisions with any obstacle
collision = false;
for i =1:size(obstacles,1)
obs_center = obstacles(i,1:2);
obs_radius = obstacles(i,3);
% Check if the next step is inside an obstacle
if norm(next_pos - obs_center)= obs_radius
collision = true;
break;
end
end
if collision
% Move around the obstacle (following the edge)
fprintf('Obstacle encountered at (%.2f,%.2f)
', robot_pos(1), robot_pos(2));
% Follow the obstacle edge until free to move toward the goal again
while collision
theta = linspace(0,2* pi,100); % Points along the obstacle's edge
edge_x = obs_center(1)+ obs_radius * cos(theta);
edge_y = obs_center(2)+ obs_radius * sin(theta);
% Move along the edge (in small steps)
for k =1:length(edge_x)
robot_pos =[edge_x(k), edge_y(k)];
path =[path; robot_pos]; % Store the path
% Plot the robot's movement along the obstacle
plot(robot_pos(1), robot_pos(2),'b.', 'MarkerSize', 5);
pause(0.01); % Slow down for visualization
% Check if free path toward the goal is possible again
direction_to_goal =(goal - robot_pos)/ norm(goal - robot_pos);
test_pos = robot_pos + step_size * direction_to_goal;
if norm(test_pos - obs_center)> obs_radius
collision = false; % Free to move toward the goal
break;
end
end
end
else
% No collision, move towards the goal
robot_pos = next_pos;
path =[path; robot_pos]; % Store the path
% Plot the robot's movement
plot(robot_pos(1), robot_pos(2),'b.', 'MarkerSize', 5);
pause(0.01); % Slow down for visualization
end
end
fprintf('Goal reached at (%.2f,%.2f)
', robot_pos(1), robot_pos(2));
% Plot the final path
plot(path(:,1), path(:,2),'k-', 'LineWidth', 2, 'DisplayName', 'Path');
legend;
hold off;
here is a code for bug 1 algorithm in matlab. the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!