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 Comments

Error using load
Unable to find file or directory 'log_031.txt'.
You forgot to attach it.

Sign in to comment.

 Accepted Answer

They seem to be different to me (note the different y-limits)
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
Modifying the code to plot all in one figure for easier comparison confirms:
figure();
om31 = load("log_031.txt");
t31 = 1:size(om31);
hold on
plot(t31/250 - 13.536, -om31(:,4)/7200-0.3);
rho_label = {};
lines = [];
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
lines(m) = plot(t, omega);%, 'b');
rho_label{m} = [', \rho = ' num2str(rho)];
end
title('Numerical Integration')
txt = ['u = ' int2str(u) ', \chi = ' num2str(chi)];
subtitle(txt);
xlabel('t', 'FontSize', 15, 'FontAngle','italic')
ylabel('\omega', 'FontSize', 15)
grid on
legend(lines,strcat('\omega_{euler}',rho_label), 'FontSize', 11, 'Location','EastOutside')

More Answers (1)

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));

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!