Why won't my figure display a line?

15 views (last 30 days)
Why won't my line show up in my figure. If i don't use stars, it remains blank. Can someone explain to me what's wrong with my code? Thanks
function cosplots(x1,xN)
format compact %clear all, then formatting prevent OCD
i = 1; %intervals begin at x1
x(i) = x1; %create a vector based off of the intervals
hold on
for i = (1:xN) %loop N times to work N variables through cos
x(i+1) = cos(x(i)); % X(n+1) = cos(Xn), implemented through the loop
plot (i,x(i), '*')
end
hold off
end

Accepted Answer

Walter Roberson
Walter Roberson on 15 Oct 2015
You are only asking to draw one point at a time, so the plot has no information about which points should be connected.
If you want the points joined together then save the point locations and plot after the loop, which you can do by removing that
plot (i,x(i), '*')
line you have now and then after the loop
plot(x)

More Answers (1)

Joseph Cheng
Joseph Cheng on 15 Oct 2015
The line is there but since you are plotting it like plot(i,x(i)) you're only plotting one point of a line so nothing shows up as the default plot is line with no marker. So by setting a marker you're showing something. Now if you want matlab to plot connecting the dots you should plot after the for loop using
plot(1:xN,x)

Community Treasure Hunt

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

Start Hunting!