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 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
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.
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!