MATLAB plots a graph correctly only when the program is stopped after the 'Plot' function. Is this normal?

2 views (last 30 days)
I wanted to plot two curves on the same figure and to block the auto axis limits after the first curve. A strange error occurred, i would like to know wheter is my code's fault or not.
This is the full working code. (I attached all the code so that you can try it on your own)
%---data input----------------------------------------------
%crosssections
A = 20.0;%mm^2
%alluminium
Aforce = [0, 772, 1398, 2772, 4076, 5309, 5919, 6140, 6223, 6291, 6328, 6385, 7017, 7329, 7459];%N
Astrain = [0, 0.0005, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.03, 0.05, 0.07];
%---Alluminium calculations----------------------------------
%stress [Mpa, N/mm^2]
Astress = Aforce./A;
%Tensile Strenght point value and position
[Atsvalue,AtsPositom] = max(Astress);
%young's modulus (mean value)
Lim = find(Astrain == 0.004, 1, 'first');
AE = zeros(0,Lim);
for i = 2:Lim
AE(i) = Astress(i)/Astrain(i);
end
AEavg = mean(AE); % [Mpa]
%---Plotting-------------------------------------------------
figure(2);
plot(Astrain,Astress,'r-')
pause(1);%-------------------------------WHY?
xlim manual;
ylim manual;
hold on;
%offset line
x = linspace(0,0.07);
y = AEavg.*(x - 0.002);
plot(x,y,'k-');
hold off;
%graphic properties
title('Conventional Stress-Strain Diagram of an Alluminium Specimen')
xlabel('Strain [mm/mm]')
ylabel('Stress [MPa]')
grid on;
As you can see the only way i had to make it work was to use the 'stop' function after the (first) plot command, (or by using the debugging tool with a breaking point after the 'plot' function).
The correct graph
If the code is executed without these 'gimmicks' the output graph is totally wrong.
The wrong Graph

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2019
Edited: Walter Roberson on 28 Feb 2019
You set ylim manual without having set specific ylim , so you get whatever ylim was active at the time. ylim is not recalculated after every graphics command , only when the graphics is rendered . Which happens for pause or drawnow.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!