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.
采纳的回答
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 个评论
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
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]))
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() )
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)
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 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
