MATLAB Answers

How to draw a log function?

147 views (last 30 days)
Ahmad on 1 May 2016
Answered: John BG on 23 Feb 2017
I love function handles in matlab, I can do this for example:
f = @(x,a,b) a*(x.^b);
This is so useful! I want to be able to plot log(y)=1+log(x), I tried:
f = @(x) 1+log10(x);
It doesn't however give a straight line, so this syntax might be wrong! Please tell me how I can do it. Thanks.

Answers (3)

John BG
John BG on 23 Feb 2017
is this what you are after?
close all
warning off
z = @(x) 10.^(-.3+(1.75*log10(x)));
y = @(x) (10.^-.3)*(x.^1.75);
x = -100:0.5:100;
grid on
grid on
John BG

John BG
John BG on 2 May 2016
try this
f = @(x) 1+log10(x)
plot(x,y);grid on
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
  1 Comment
Ahmad on 2 May 2016
i appreciate ur help, but plz read my question again, i wanna plot logy=1+logx which should give a straight line

Sign in to comment.

Roger Stafford
Roger Stafford on 2 May 2016
The quantity f(log10(f(x)) does not yield the solution to log10(y) = 1 + log10(x). It is actually equal to
f(log10(f(x)) = f(log10(1+log10(x))) = 1+log10(log10(1+log10(x)))
If you were to take the log10 of that, you certainly would not come back to 1+log10(x).
To solve for y, take 10 to the power of both sides of the equation
y = 10^(log10(y)) = 10^(1+log10(x)) = (10^1)*(10^log10(x)) = 10*x
What could be simpler?
Roger Stafford
Roger Stafford on 2 May 2016
The plot you show in your previous comment plots log(y) against log(x) or log10(y) against log10(x), I’m not sure which. With the equation log(y) = -.3+1.75*log(x) you will naturally get a straight line with this kind of plot. However, that is not the same thing as plotting y against x. For the equation log(y) = -.3+1.75*log(x) you will NOT get a straight line with y against x. For the equation log(y) = 1 + log(x), or log10’s either one, you WILL get a straight line with y against x. The present or absence of the factor 1.75 makes the difference.
Incidentally you should be careful to distinguish between logarithms base ten and natural logarithms with a base e. In matlab the natural logarithm is indicated by ‘log’ whereas logarithm base ten is indicated by ‘log10’.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!