plotting three functions in the same graph with a specification

I want to plot three functions (x,y1) (x, y2) (x, y3) at the same graph. The point is that I want to be able to see the curve of xlogx function (I dont want to have a flat (x,y2) function) while the other two remain flat. How can I do this?
x values will be from 1000 to 24001000 and increse by 10000.
x = [1000, 1001000, 2001000, 3001000, 4001000, 5001000, 6001000, 7001000, 8001000, 9001000, 10001000, 11001000, 12001000, 13001000, 14001000, 15001000, 16001000, 17001000, 18001000, 19001000, 20001000, 21001000, 22001000, 23001000, 24001000]
y1 values will be 1000 times each corresponding x.
y2 values will be x * log(x) (base of logarithm will be 2)
y3 values will be just equal to each corresponding x value

 Respuesta aceptada

x * log(x) should be
x .* log(x)
% ^
and then set the y axis scale to log.
set(gca, 'YScale', 'log')
% Or, if you have the axis handle,
ax.YScale = 'log';

6 comentarios

Thank you but how can I plot using these?
plot(x, x*___) %fill in the blanks
hold on
plot(x, x.*___) % fill in the blanks
plot(x, x)
Thank you very much but I get a flat xlogx line with your solution. I want to have it curved and only the linear functions flat.
Henry Gotjen's answer is virtually the same as my answer. The lines are curved on a log scale but not on a linear scale. The log scale and linear scale show the exact same data. Only the scaling is different.
Two alternatives of showing all three lines without using a log scale are,
1) two y axes.
yyaxis left
plot(x, x*___) %fill in the blanks
yyaxis right
hold on
plot(x, x.*___) % fill in the blanks
plot(x, x)
2) or use different axes,
subplot(1,3,1)
plot(x, x*___) %fill in the blanks
subplot(1,3,2)
plot(x, x.*___) % fill in the blanks
subplot(1,3,3)
plot(x, x)
Adam Danz
Adam Danz el 23 de Abr. de 2020
Editada: Adam Danz el 23 de Abr. de 2020
"I get a flat xlogx line with your solution. I want to have it curved and only the linear functions flat."
In that case, use the yyaxis solution I wrote above.
Put the linear lines on the left (or right) axis and the log line on the other axis. Then set the appropriate axis to log scale using the line of code from my answer. Just note that the two linear scaled lines are on vastly different sales so one of them will be at the bottom of the plot (see below).
If you run into problems, share your code so I can help straighten it out.
Thank you very much.

Iniciar sesión para comentar.

Más respuestas (1)

Your three function values fall in a range of orders of magnitude spanning 1e3 to 1e10. I think this code is the best visualization of the three functions in the same graph
x = (1000:1e5:24.001e6)'; % x values
% functions as you described
y1 = 1000*x;
y2 = x.*log(x);
y3 = x;
% plot(x,[y1 y2 y3]) %this doesn't work well because y2 and y3 appear near the x axis
semilogy(x,[y1 y2 y3]) % plots the three functions with a logscale y axis

2 comentarios

Thank you but can I plot such that linear functions are flat and only xlogx function is curved? Your solution makes all of them curved.
I understand. Adam's solution using left and right axes is best then. Cheers

Iniciar sesión para comentar.

Categorías

Preguntada:

el 23 de Abr. de 2020

Comentada:

el 29 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by