fplot glitch when plotting a square function
1 次查看(过去 30 天)
显示 更早的评论
Problem description
When plotting a custom function (based on the square() function) using the fplot() command, the plot leaves some gaps where none should be and when panning the plot, these gaps appear at different positions. To see this, just count the gaps (marked red) between 0 and 50 in the plots below - in the first plot, there are 4 gaps and in the second, there are 7 gaps.
I see 3 different sources of error for this:
- A bug in my implementation
- A hardware problem
- A MATLAB problem


MATLAB code:
% Time-conversion factors
seconds_per_minute = 60;
minutes_per_hour = 60;
hours_per_day = 24;
seconds_per_hour = seconds_per_minute*minutes_per_hour;
seconds_per_day = seconds_per_minute*minutes_per_hour*hours_per_day;
% Custom function
photoperiod = 16; % [hours/day]
duty = photoperiod/24*100; % [percent]
day_start = 6;
I_A_max = 150;
I_A = @(t) I_A_max/2*square(((t-day_start)*(2*pi)/24)/seconds_per_hour, duty)+I_A_max/2;
% Make the plot
fplot(@(t) I_A(t*seconds_per_day))
xlabel('time (days)')
ylabel({'Light';'[W/m^2]'})
xlim([0,50])
Can anyone comment on what is causing this behavior?
0 个评论
采纳的回答
Stephen23
2023-8-15
"Can anyone comment on what is causing this behavior?"
The main cause is missing from your list: mathematics. It essentially comes down to this:
You seem to expect MATLAB to take infinite samples within that range. Such a thing is not possible.
But you could specify the number of samples (following the nyquist-shannon sampling theorem, at more than double your data frequency):
% Time-conversion factors
seconds_per_minute = 60;
minutes_per_hour = 60;
hours_per_day = 24;
seconds_per_hour = seconds_per_minute*minutes_per_hour;
seconds_per_day = seconds_per_minute*minutes_per_hour*hours_per_day;
% Custom function
photoperiod = 16; % [hours/day]
duty = photoperiod/24*100; % [percent]
day_start = 6;
I_A_max = 150;
I_A = @(t) I_A_max/2*square(((t-day_start)*(2*pi)/24)/seconds_per_hour, duty)+I_A_max/2;
% Make the plot
md = 50*16; % a wild guess (you need to specify this)
fplot(@(t) I_A(t*seconds_per_day), 'MeshDensity',md)
xlim([0,50])
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

