Plotting a Semi-Log Plot as a Function of Time

I am fairly new to Matlab plotting and I can't figure out the semi-log plotting.
I would like to plot the following as a semi-log plot:
Y-axis
  • Y = 50 for t < 0.05
  • Y = 31.38 + 0.931/t for 0.05 ≤ t ≥ 7.758
  • The Y-axis will have intervals of 0, 10, 20, 30, 40, 50, 60.
X-axis (time axis)
  • The X-axis (semi-log) would range from [0.01, 0.1, 1, 10, 100). < -- logspace (-2,2)
  • A marker for 7 to be included on the x-axis.
Any help would be appreciated. Thank you.

 채택된 답변

Cris LaPierre
Cris LaPierre 2024년 12월 15일
I'd do something like this. Let me know if you have any questions
t = logspace(-2,2);
% one way to implement a piecewise function in MATLAB
y = (50*(t<0.05) + (31.38 + 0.931./t).*(t>=0.05));
% Creat the plot
semilogx(t,y)
ylim([0,60]) % set range of visible y values. Otherwise, plot collapses to range plotted
yticks(0:10:60) % set desired ytick locations
% by default, log tick lablels are exponenents (e.g 10^-2). This displays
% the equivalent numeric representation
xticklabels(string([0.01, 0.1, 1, 10, 100]))
xline(7,'-','t=7') % add a lins at x=7. Could also use this: plot(7,0,'rx')
xlabel('Time')

댓글 수: 7

Gus
Gus 2024년 12월 15일
편집: Gus 2024년 12월 16일
Cris,
Thank you very much. This will certainly help me.
Two questions:
1) In the third line, why is there a period following 0.93t and a period before the *?
2) I modifed iine 3 as follows: y = (50*(t<0.05) + (31.38 + 0.931./t).*(7.758>=t>=0.05)+(31.5).*(t>7.758)); But for some reason my plot starts at 60 instead of 50 at time 0.01 and I don't know why. All I did was just add an extra time interval for beyond t = 7.758.
3) Is there a way to make the text '7' appear along the time axis? From the examples I have seen I don't think so. I am using R2017a so when using 'xline', I get the followng error: ---> 'Undefined function or variable 'xline'. Using plot (7,0,'x'), returned a completely different looking plot. No problem, I can add it manually
Thanks, Gus
Walter Roberson
Walter Roberson 2024년 12월 16일
편집: Walter Roberson 2024년 12월 16일
1) Yes
2) The 0.931./t period is there because it is specifying that the operator named ./ is being used rather than the operator named / . The operator named / is formally known as mrdivide, / Matrix Right Divide, and A/B is approximately the same as A * inv(B) where * is the algebraic matrix multiplication operator. The operator named ./ is formally known as rdivide, ./ Right Divide and A./B is element-wide division, A(I,J) divided by B(I,J) . 0.931./t means [0.931 divided by t(1), 0.931 divided by t(2), 0.931 divided by t(3)] and so on instead of a pseudo-inverse calculation.
3) xticks(sort([xticks(), 7]))
Gus
Gus 2024년 12월 16일
편집: Gus 2024년 12월 16일
Thanks Walter.
Question:
I modifed iine 3 as follows: y = (50*(t<0.05) + (31.38 + 0.931./t).*(7.758>=t>=0.05)+(31.5*(t>7.758))); But for some reason my plot starts shows a vertical line at t-0.05 instead of 50 and then the plot begins, I have no idea why.
All I did was just add an extra time interval such that y = 31.5 for beyond t = 7.758.
Regards,
Gus
The (31.38 + 0.931./t).*(7.758>=t>=0.05) part of the expression will be parsed as
(31.38 + 0.931./t).*((7.758>=t)>=0.05)
The result of 7.758>=t is either 0 (false) or 1 (true). That 0 or 1 will then be compared to 0.05 which is unlikely to be what you want.
MATLAB does not normally support chaining comparison operators. (There is undocumented support for chaining comparison operators for symbolic expressions in the "cond" positions in piecewise() )
Gus
Gus 2024년 12월 16일
편집: Gus 2024년 12월 16일
So to correct this, I need to look in to the 'cond' command?
This what i want to achieve.
y = (50)*(t<0.05) + (31.38 + 0.931./t).*(7.758>=t & t>=0.05) + (31.5)*(t>7.758)
Gus
Gus 2024년 12월 16일
Thank you very much Walter. Very much appreciated.
So the logical 'AND' was needed in this case. Makes sense now.
Regards,
Gus

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

추가 답변 (0개)

카테고리

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

태그

질문:

Gus
2024년 12월 15일

댓글:

Gus
2024년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by