MATLAB displays a blank graph when I plot try to plot

조회 수: 3 (최근 30일)
Imraan Jabar
Imraan Jabar 2016년 2월 18일
답변: Renato Agurto 2016년 2월 18일

So I am working on an assignment where we are to calculate the slope (slope=y2-y1/x2-x1)of (x^3)*cos(x) at x=3 using x2= 3.001, 3.005, 3.01,3.05, 3.1, 3.5, and 4 So wrote a for loop to calculate all the slopes and included a plot within the loop so it can plot the change in x against slope.

x=3;
y=(x^3)*cos(x);
di(1)=0.001;
di(2)=0.005;
di(3)=0.01;
di(4)=0.05;
di(5)=0.1;
di(6)=0.5;
di(7)=1;
hold on
for ii=1:7
    xi(ii)=x+di(ii);
    yi=((xi(ii))^3)*cos(xi(ii));
    slope(ii)=(yi-y)/di(ii);
    plot(di(ii),slope(ii))
end
hold off

So the problem is the graph comes up as blank. I tried looking around for people having similar issue with plot but could find a good solution. Do you know what I am doing wrong?

채택된 답변

MHN
MHN 2016년 2월 18일
It is not empty, it has the points. Do the following change:
change the plot line to
plot(di(ii),slope(ii),'o')

추가 답변 (1개)

Renato Agurto
Renato Agurto 2016년 2월 18일

Since you are plotting single dots ( plot in a for loop) the dots aren't connected. Just try:

x=3;
y=(x^3)*cos(x);
di(1)=0.001;
di(2)=0.005;
di(3)=0.01;
di(4)=0.05;
di(5)=0.1;
di(6)=0.5;
di(7)=1;
for ii=1:7
    xi(ii)=x+di(ii);
    yi=((xi(ii))^3)*cos(xi(ii));
    slope(ii)=(yi-y)/di(ii);
end
plot(di,slope)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by