Why do I get a graph that is symmetrical based on x=-2 when I draw a graph of y1=4*log((x+2))?

1 回表示 (過去 30 日間)
정훈 안
정훈 안 2020 年 7 月 7 日
編集済み: John D'Errico 2020 年 7 月 7 日
Here is my code and the graph
x=(-10:10);
y1=4*log((x+2));
plot(x,y1)
hold on
plot([-2,2],[0,0],'k-')
plot([-10,6],[0,0],'k-')
Isn't it impossible to be symmetrical because the station is defined as {x|x>-2}?
Is my coding wrong or can you tell me how to draw the right graph?

回答 (2 件)

John D'Errico
John D'Errico 2020 年 7 月 7 日
編集済み: John D'Errico 2020 年 7 月 7 日
It is not clear what you wanted to plot.
However, I think you misunderstand. It is still possible to evaluate the log of a negative number. The problem is that log is a complex number.
log(-3)
ans =
1.09861228866811 + 3.14159265358979i
log(-4)
ans =
1.38629436111989 + 3.14159265358979i
MATLAB correctly extends the log function, so it can be evaluated for negative inputs too. That is, if you read the docs for log, you will see:
log(z) = log(abs(z)) + 1i*angle(z)
The function angle for a negative number always returns pi. That is the extension of the natural log function into the complex plane.
So, what happens when you plot a complex number, as you did with
plot(x,y1)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
MATLAB plots only the real part of y1. The imaginary part of y1 is ignored. In fact, you should have gotten a warning message, indicating that fact.
What then is the real part of log(z)?
real(log(z)) = log(abs(z))
You should then expect the log function to be plotted as a symmetric function around 0. Therefore, a plot of real(log(z+2)) would be symmetric around -2.
How can you draw the "correct" graph? That is difficult to know, since we don't know what you intend to do. The graph is correct, as MATLAB sees the problem.
If you want the plot to show nothing below -2, then x should start out at -2, not at -10.

Joost
Joost 2020 年 7 月 7 日
Hello,
You find the answer in the Warning that just made the picture...
The natural logarithm log(x) is only defined for values of x>0. For negative numbers Matlab gives complex numbers as outputs.
In plotting, Matlab ignores the imaginary part of complex numbers.
Best regards,
Joost

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by