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

here is a code for bug 2algorithm in matlab. the output of this code is shown in the 2nd image which is not desired. the output graph should look like the 1st image. so rewrite the code make the changes so that the output looks like the 1st image:
% BUG2 Algorithm Implementation in MATLAB
clear;
clc;
% Define initial and goal positions
init_pos =[0,0]; % Initial position
goal_pos =[10,10]; % Goal position
% Define obstacle as a boundary (for simplicity, a circular obstacle)
obstacle_center =[5,5]; % Center of the circular obstacle
obstacle_radius =2; % Radius of the circular obstacle
% Create figure
figure;
hold on;
% Plot obstacle
theta = linspace(0,2*pi,100);
x_obstacle = obstacle_center(1)+ obstacle_radius * cos(theta);
y_obstacle = obstacle_center(2)+ obstacle_radius * sin(theta);
plot(x_obstacle, y_obstacle, 'r', 'LineWidth', 2); % Red boundary for obstacle
% Plot initial and goal points
plot(init_pos(1), init_pos(2),'go', 'MarkerSize', 10, 'MarkerFaceColor', 'g');
plot(goal_pos(1), goal_pos(2),'bo', 'MarkerSize', 10, 'MarkerFaceColor', 'b');
% Set axis limits
axis equal;
xlim([-1,11]);
ylim([-1,11]);
% Start moving towards goal
current_pos = init_pos;
plot_path =[]; % To store the path of the robot
while norm(current_pos - goal_pos)>0.1% Continue until the goal is reached
% Check if line of sight is clear (no obstacle intersection)
if ~is_in_obstacle(current_pos, goal_pos, obstacle_center, obstacle_radius)
% Move straight towards the goal
current_pos = move_towards(current_pos, goal_pos);
else
% Encounter obstacle, follow boundary
current_pos = follow_obstacle(current_pos, goal_pos, obstacle_center, obstacle_radius);
end
% Plot the robot's path
plot_path =[plot_path; current_pos]; %#ok
plot(plot_path(:,1), plot_path(:,2),'k', 'LineWidth', 2);
pause(0.1); % Slow down the plotting for visualization
end
% Function to check if a path is within an obstacle
function is_obst = is_in_obstacle(pos1, pos2, center, radius)
% Check if the line between pos1 and pos2 intersects with the circular obstacle
% For simplicity, use distance to the obstacle center
midpoint =(pos1+ pos2)/2;
distance_to_center = norm(midpoint - center);
if distance_to_center radius
is_obst = true;
else
is_obst = false;
end
end
% Function to move the robot towards the goal
function new_pos = move_towards(current_pos, goal_pos)
step_size =0.5; % Define the step size
direction =(goal_pos - current_pos)/ norm(goal_pos - current_pos);
new_pos = current_pos + step_size * direction;
end
% Function to follow the obstacle boundary
function new_pos = follow_obstacle(current_pos, goal_pos, center, radius)
% For simplicity, move around the obstacle in a circular fashion
step_angle =0.1; % Step size around the obstacle
direction_vector = current_pos - center;
current_angle = atan2(direction_vector(2), direction_vector(1));
new_angle = current_angle + step_angle; % Move along the boundary
new_pos = center + radius *[cos(new_angle), sin(new_angle)];
end
here is a code for bug 2 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!