fplot glitch when plotting a square function
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel
am 15 Aug. 2023
Beantwortet: Stephen23
am 15 Aug. 2023
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 Kommentare
Akzeptierte Antwort
Stephen23
am 15 Aug. 2023
"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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

