Plotting 1/x correctly
古いコメントを表示
I'm trying to plot a simple function, 1/x. However it does not seem to work as intended. I define a linspace for my variable x and then use the plot() function.

As seen in the picture, for X=3, Y= 4.95. How is this possible? For X=3, Y should be equal to 0.33?
Is there something I'm missing causing the function to break the laws of mathematics? Changing the linspace size, or simply step size results in large differences as well.
採用された回答
その他の回答 (2 件)
Jos (10584)
2015 年 5 月 26 日
Your plotting values against their index. And indeed, since x(3) is not equal to 3, but 0.2020, y(3) is not 1/3. You can use plot(x,1/.x)
Try this:
x = linspace(0,10,100) ;
y = 1./x ;
plot(x, y)
idx = 3, x(idx), y(idx)
Titus Edelhofer
2015 年 5 月 26 日
Adding to Thorsten's answer: you need to put x on the x axis while plotting:
plot(x, 1./x);
Titus
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!