Plotting 1/x correctly
조회 수: 50 (최근 30일)
이전 댓글 표시
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/150555/image.png)
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.
댓글 수: 0
채택된 답변
Thorsten
2015년 5월 26일
편집: Thorsten
2015년 5월 26일
Because you have not specified the x values in your plot, they run from 1 to the numel(s). So X is not an absolute value but the index into your x, defined as
x = linspace(0, 10, 100);
Then
x(3) = 0.2020
so y = 1/x = 4.95.
To get the right values, use
plot(x, 1./x)
댓글 수: 0
추가 답변 (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)
댓글 수: 0
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!