Saving figures in loop
Afficher commentaires plus anciens
I have such code. I should get plots for different parameters of rho. But I get the same picture each time
for m = 1:10
rho = 1 + m; % params
chi = 1;
u = 31;
f1 = @(t,i,omega) u - rho*i - chi*omega; % right part of 1st ODE
f2 = @(t,i,omega) chi*i - chi/rho*(5/6 - chi)*omega - chi/(6*rho)*sign(omega); % right part of the 2nd ODE
t0 = 0;
tn = 5;
h = 0.005; % step
n = (tn - t0)/h;
i0 = 0; % init i-value
omega0 = 0; % init omega-value
t(1) = t0; i(1) = i0; omega(1) = omega0;
for k=1:n
i(k+1) = i(k) + h*f1(t(k), i(k), omega(k));
omega(k+1) = omega(k) + h*f2(t(k), i(k), omega(k));
t(k+1) = t0 + k*h;
end
om31 = load("log_031.txt");
t31 = 1:size(om31);
h = figure(m);
hold on
plot(t31/250 - 13.536, -om31(:,4)/7200-0.3);
title('Numerical Integration')
txt = ['u = ' int2str(u) ', \rho = ' num2str(rho) ', \chi = ' num2str(chi)];
subtitle(txt);
plot(t, omega, 'b');
legend('\omega_{aist}', '\omega_{euler}', 'FontSize', 15)
xlabel('t', 'FontSize', 15, 'FontAngle','italic')
ylabel('\omega', 'FontSize', 15)
grid on
path_name = '/home/andrei/Desktop/курсач/ЧМы/31';
file_name = strcat('u_', num2str(u), ', rho_', num2str(rho), ', chi_', num2str(chi));
saveas(h, fullfile(path_name, file_name), 'png');
hold off;
close(h)
end
2 commentaires
Image Analyst
le 6 Fév 2022
Error using load
Unable to find file or directory 'log_031.txt'.
You forgot to attach it.
Evgeniy Barkalov
le 6 Fév 2022
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 6 Fév 2022
You can use exportgraphics() to save the figure
baseFileName = sprintf('u_%2.2d, rho_%2.2d, chi_%2.2d.png', u, rho, chi);
exportgraphics(h, fullfile(path_name, baseFileName));
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!










