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 Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by