In: Computer Science
using Matlab only please. function sail_boat x = linspace(0,100,2*pi); for x plot(x, sin(x),'b','LineWidth',3) axis([0,2*pi+.1,-1.05,1.05]) hold on boat(x,sin(x)) pause(.05) end function boat(x,y) xc = [x-.1,x+.1,x+.2,x-.2,x-.1]; yc = [y,y,y+.1,y+.1,y]; plot(xc,yc,'k')
The function is supposed to plot a sine curve and an animation of a representation of a boat moving on top of the curve, but the code has several errors. Notice that the program uses modulization and it calls another function inside sail_boat. Correct the code and submit it with a new name sail_boat_correct.m Note that the code uses the function linspace(). We have seen it before but if you are not familiar with it, make sure to look it up. The pause() function is used in the animation, causing the plotting to pause for 0.05 seconds, aiding in the visualization. Also, submit a Word document providing a quick description of what edits you had to make and how you went about finding the errors. Name the Word document Debugging.docx.
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
sail_boat_correct.m
function sail_boat
y = linspace(0,2*pi,100);
for i=(1:length(y))
x=y(i);
disp(x);
plot(x, sin(x),'b','LineWidth',3)
axis([0,2*pi+.1,-1.05,1.05])
hold on
boat(x,sin(x))
pause(.05)
end
Error documentation:
Error correction in boat.m
Boat.m
function boat(x,y)
xc = [x-.1,x+.1,x+.2,x-.2,x-.1];
yc = [y,y,y+.1,y+.1,y];
plot(xc,yc,'k')
end
end was missing in this.
Error correction in sail_boat.m
here we have to take 100 points between 0 to 2*pi but we are using wrong syntax.
We have to use x = linspace(0,2*pi,100);
loop is wrong we have to use to loop though each value:
for i=(1:length(y))
Output: