How to add markers to a plot after using fzero

1 view (last 30 days)
I am working on making a plot of y vs y'. I have constructed the plot and used the fzero function initialized at -4, -2, 0, 2, 4 to find all zeros of y'. I am trying to add these zeros to the plot already made as markers. Thanks for your help!
x=-2*pi : 2*pi;
y=3*x.*sin(x)-2*x;
y1=3*sin(x)+3*x.*cos(x)-2;
plot(x,y,'-',x,y1, '--')
xlabel('\bf\itx-axis\rm')
ylabel('\bf\ity-axis\rm')
legend('y=3xsin(x)-2x','y1=3sin(x)+3xcos(x)-2', 'Location', 'S')
x0 =-4; x1=-2; x2=0; x3=2; x4=4;
z0 = fzero('3*sin(x)+3*x.*cos(x)-2', x0)
z1 = fzero('3*sin(x)+3*x.*cos(x)-2', x1)
z2 = fzero('3*sin(x)+3*x.*cos(x)-2', x2)
z3 = fzero('3*sin(x)+3*x.*cos(x)-2', x3)
z4 = fzero('3*sin(x)+3*x.*cos(x)-2', x4)

Accepted Answer

Star Strider
Star Strider on 26 Oct 2017
I would use a loop to create a vector for ‘z’:
x=-2*pi : 2*pi;
y=3*x.*sin(x)-2*x;
y1=3*sin(x)+3*x.*cos(x)-2;
x0 = [-4, -2, 0, 2, 4];
for k1 = 1:numel(x0)
z(k1) = fzero('3*sin(x)+3*x.*cos(x)-2', x0(k1));
end
figure
plot(x,y,'-',x,y1, '--')
hold on
plot(z, zeros(size(z)), 'pg')
hold off
xlabel('\bf\itx-axis\rm')
ylabel('\bf\ity-axis\rm')
legend('y=3xsin(x)-2x','y1=3sin(x)+3xcos(x)-2', 'Location', 'S')

More Answers (0)

Categories

Find more on Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!