Why does my graph come out wrong?
8 ビュー (過去 30 日間)
古いコメントを表示
I want to plot a simple function, ((x.^2)-1).^(2/3). But my plot comes out looking different from the mathematicly correct one. Why is that? To specify, the graph should not be able to go under the x=0 line, yet it does.
回答 (2 件)
Voss
2022 年 1 月 16 日
x = -10:0.01:10;
y = (x.^2-1).^(2/3);
plot(x,y)
Note the warning about complex values. The values of y where abs(x) < 1 are complex because x^2-1 < 0 where abs(x) < 1. To skip plotting this reagin, you can keep track of when y is real and only plot those elements:
idx = imag(y) == 0;
plot(x(idx),y(idx))
ylim([-5 25])
Note the lack of warning this time. You could also replace the complex y with NaNs and plot that:
y(~idx) = NaN;
plot(x,y)
ylim([-5 25])
2 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!