I love function handles in matlab, I can do this for example:
f = @(x,a,b) a*(x.^b);
plot(x,f(x,a,b));
This is so useful! I want to be able to plot log(y)=1+log(x), I tried:
f = @(x) 1+log10(x);
plot(x,f(log10(f(x));
It doesn't however give a straight line, so this syntax might be wrong! Please tell me how I can do it. Thanks.

답변 (3개)

John BG
John BG 2017년 2월 23일

2 개 추천

is this what you are after?
clc
close all
warning off
z = @(x) 10.^(-.3+(1.75*log10(x)));
y = @(x) (10.^-.3)*(x.^1.75);
x = -100:0.5:100;
plot(x,z(x),'-b','LineWidth',2)
grid on
figure
loglog(x,y(x),'-r','LineWidth',2)
grid on
.
John BG
John BG
John BG 2016년 5월 2일

0 개 추천

Ahmad
try this
f = @(x) 1+log10(x)
x=[-20:.1:20]
y=f(x)
plot(x,y)
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
John

댓글 수: 1

Ahmad
Ahmad 2016년 5월 2일
i appreciate ur help, but plz read my question again, i wanna plot logy=1+logx which should give a straight line

댓글을 달려면 로그인하십시오.

Roger Stafford
Roger Stafford 2016년 5월 2일

0 개 추천

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?

댓글 수: 5

John BG
John BG 2016년 5월 2일
편집: John BG 2016년 5월 2일
correct, don't expect a straight line unless you take power 10^ to 'bend back up' the log
Not correct, I'm afraid, I tried this, however obviously z and y are identical and nonlinear!
clc
close all
warning off
z = @(x) 10.^(-.3+(1.75*log10(x)));
y = @(x) (10.^-.3)*(x.^1.75);
x = -100:0.5:100;
plot(x,z(x),'-b','LineWidth',2)
grid on
figure
plot(x,y(x),'-r','LineWidth',2)
grid on
Again, I'm trying to do this in matlab, help!
Roger Stafford
Roger Stafford 2016년 5월 2일
편집: Roger Stafford 2016년 5월 2일
“Not correct, I'm afraid, I tried this, however obviously z and y are identical and nonlinear!”
Hey, no fair, Ahmad! You slipped in a factor of 1.75 on your trial. You have 10.^(-.3+1.75*log10(x)) instead of your original 1+log10(x). Naturally you won’t get a straight line with that. The -.3 doesn’t make it nonlinear, but the 1.75 does.
In any case you now know how to plot log10(y) = -.3+1.75*log(x). Just take ten to the power of each side of the equation. Of course, it won't be linear with the 1.75 factor present.
Ahmad
Ahmad 2016년 5월 2일
lol sorry i gave a simple example, but honestly are u telling me i can't plot that equation and get the same graph shown in matlab?
Roger Stafford
Roger Stafford 2016년 5월 2일
편집: Roger Stafford 2016년 5월 2일
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’.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2016년 5월 1일

답변:

2017년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by