Question

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...

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.

Solutions

Expert Solution

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

  1. x = linspace(0,100,2*pi);

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);

  1. for x

loop is wrong we have to use to loop though each value:

                for i=(1:length(y))

Output:


Related Solutions

Please show MATLAB code to plot below function from x = −3 to x = 12:...
Please show MATLAB code to plot below function from x = −3 to x = 12: function f = piecewise(x) % implements piecewise function using if statements if x < 0     f = -x^3 - 2*x^2 + 3*x; elseif x <= 8     f = (12/pi) * sin(pi*x/4); else     f = (600*exp(x-8))/(7*(14 + 6*exp(x-8))) -30/7; end
USING MATLAB.... a.) Create anonymous functions: fa(x)=sin(x^2) fb(x)=sin^2(x) b.) Evaluate them both at x=1/2 pi c.)Evaluate...
USING MATLAB.... a.) Create anonymous functions: fa(x)=sin(x^2) fb(x)=sin^2(x) b.) Evaluate them both at x=1/2 pi c.)Evaluate them both at x=(0,1,2,...,10)^T d.) Calculate fa(fb(2)) and fb(fa(2))
Please use MATLAB. Write a program to plot the piecewise function using IF statements and a...
Please use MATLAB. Write a program to plot the piecewise function using IF statements and a FOR loop in MatLab. Use x as the input vector. Plot from -20<=x<=20, increment of 1.   y =   4 x + 10 ,             ? ≥ 9,                   y =      5? + 5,                   0 ≤ ? < 9,                    y =     ?2 + 6? + 8, ? < 0 Plot each segment of the fn with different shaped points and a different color, for...
S(x) is a cubic spline for the function f(x) = sin(pi x/2) + cos(pi x/2) at...
S(x) is a cubic spline for the function f(x) = sin(pi x/2) + cos(pi x/2) at the nodes x0 = 0 , x1 = 1 , x2 = 2 and satisfies the clamped boundary conditions. Determine the coefficient of x3 in S(x) on [0,1] ans. pi/2 -3/2
1) find all values of theta E [ 0,2 pi], where the curve r= 1- sin...
1) find all values of theta E [ 0,2 pi], where the curve r= 1- sin theta has a horizontal tangent line 2)find all values of theta E [ 0,2 pi] where the curve r=1- sin theta has a vertical tangent line 3) find the area of the region enclosed by the intersection of the curves" r= sin theta and r= cos theta
Find the real Fourier series of the piece-wise continuous periodic function f(x) = x+sin(x) -pi<=x<pi
Find the real Fourier series of the piece-wise continuous periodic function f(x) = x+sin(x) -pi<=x<pi
Write as a MatLab script For X=-2π~2π with intervals of π/100. a. Plot Y1=sin(X) as a...
Write as a MatLab script For X=-2π~2π with intervals of π/100. a. Plot Y1=sin(X) as a red line. Add a grid. b. Plot Y2=cos(X) as a black dotted line. Add a grid. c. Plot Y1 and Y2 in the same plot without using the hold on/off command. d. For Y3= Y1 times Y2, plot Y1, Y2 and Y3 in the same graph. Y3 will be green line. Add title, axis label and a legend.
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
I want to to calculate the area between the x-axis and the function f(x) = sin(x)...
I want to to calculate the area between the x-axis and the function f(x) = sin(x) on the interval [0, pi]. The area we seek is enclosed in a rectangle bounded by the curves. x = 0, x = pi, y = 0, y = 1. Since we know the area of the rectangle is pi, we can generate random points inside the rectangle and keep track of how many of those points lie below the curve y = sin(x)....
1. is sin(pi/4) causal? 2. is sin(pi/4) stable? 3. is delta(n+1) causal? 4.  = ? 5. If...
1. is sin(pi/4) causal? 2. is sin(pi/4) stable? 3. is delta(n+1) causal? 4.  = ? 5. If function w [ n ] is convolved with , what will the result be? 6. if a system with signal length 4 is convolved with its own system response, what will the length of that signal be? 7. In an LTI system, x[n] * h[n]= y[n]. What is x[n-3] * h[n-2] =?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT