Log equation log(x) not plotting correctly

4 ビュー (過去 30 日間)
Taylor Latham
Taylor Latham 2018 年 10 月 16 日
回答済み: James Tursa 2018 年 10 月 17 日
Here is my code. The log(x) function is plotting with negative values.
x=-10:1:10;
y_1=x;
y_2=exp(x);
y_3=log(x);
plot(x,y_1,'k-',x,y_2,'r--',x,y_3,'b:')
hold on
plot([-10 10],[0 0],'k',[0 0],[-5 5],'k')
axis([-10 10 -5 5])
grid on
title('Comparison Between the Exponential and Natural Logarithm Functions')
xlabel('x-axis')
ylabel('y-label')
legend('y=x','y=exp(x)','y=ln(x)')

回答 (3 件)

Ryan Takatsuka
Ryan Takatsuka 2018 年 10 月 16 日
y_3 contains complex numbers with real and imaginary components, and the plot() function only plots the real component of the complex number. Imaginary components are ignored.

Steven Lord
Steven Lord 2018 年 10 月 16 日
Did you notice the warning you received in the Command Window when you ran this code?
Does real(y_3) look familiar in the context of your plot?
  1 件のコメント
Taylor Latham
Taylor Latham 2018 年 10 月 16 日
This is the warning I received.
Warning: Imaginary parts of complex X and/or Y arguments ignored >>

サインインしてコメントする。


James Tursa
James Tursa 2018 年 10 月 17 日
Since you are trying to graph the inverse of the functions, you should be switching the domains as well when you generate the points. So these lines:
y_3=log(x);
plot(x,y_1,'k-',x,y_2,'r--',x,y_3,'b:')
should be this instead:
y_3=log(y_2); % switched domain of log to y_2 instead of x
plot(x,y_1,'k-',x,y_2,'r--',y_2,y_3,'b:') % same change here

カテゴリ

Help Center および File ExchangeExponents and Logarithms についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by